From e0667855ee8d18d8bde11b960e9a38d2fffcf67a Mon Sep 17 00:00:00 2001
From: tangjinzhou <415800467@qq.com>
Date: Thu, 22 Oct 2020 00:30:57 +0800
Subject: [PATCH] fix: tree-select placeholder error
---
antdv-demo | 2 +-
components/_util/BaseInput.tsx | 51 +++++++++++
components/vc-tree-select/src/SearchInput.jsx | 91 +++++++++++--------
components/vc-tree-select/src/Select.jsx | 15 +--
.../src/Selector/SingleSelector.jsx | 35 ++++---
examples/App.vue | 2 +-
6 files changed, 138 insertions(+), 58 deletions(-)
create mode 100644 components/_util/BaseInput.tsx
diff --git a/antdv-demo b/antdv-demo
index ab46da93f..6cb2ff40c 160000
--- a/antdv-demo
+++ b/antdv-demo
@@ -1 +1 @@
-Subproject commit ab46da93f0bca77a66a25f0ef427fa106d6dff13
+Subproject commit 6cb2ff40c46114d4195edcacf82e56b6686ff8a7
diff --git a/components/_util/BaseInput.tsx b/components/_util/BaseInput.tsx
new file mode 100644
index 000000000..0c0e42f84
--- /dev/null
+++ b/components/_util/BaseInput.tsx
@@ -0,0 +1,51 @@
+import { defineComponent, ref, withDirectives } from 'vue';
+import antInput from './antInputDirective';
+import PropTypes from './vue-types';
+const BaseInput = defineComponent({
+ props: {
+ value: PropTypes.string.def(''),
+ },
+ emits: ['change', 'input'],
+ setup(_p, { emit }) {
+ const inputRef = ref(null);
+ const handleChange = (e: Event) => {
+ const { composing } = e.target as any;
+ if ((e as any).isComposing || composing) {
+ emit('input', e);
+ } else {
+ emit('input', e);
+ emit('change', e);
+ }
+ };
+ return {
+ inputRef,
+ focus: () => {
+ if (inputRef.value) {
+ inputRef.value.focus();
+ }
+ },
+ blur: () => {
+ if (inputRef.value) {
+ inputRef.value.blur();
+ }
+ },
+ handleChange,
+ };
+ },
+ render() {
+ return withDirectives(
+ (
+
+ ) as any,
+ [[antInput]],
+ );
+ },
+});
+
+export default BaseInput;
diff --git a/components/vc-tree-select/src/SearchInput.jsx b/components/vc-tree-select/src/SearchInput.jsx
index cdf453d38..03b29715c 100644
--- a/components/vc-tree-select/src/SearchInput.jsx
+++ b/components/vc-tree-select/src/SearchInput.jsx
@@ -4,8 +4,8 @@
* - multiple: in the selector
* Move the code as a SearchInput for easy management.
*/
-import { inject, withDirectives, ref, onMounted, computed, watch } from 'vue';
-import antInput from '../../_util/antInputDirective';
+import BaseInput from '../../_util/BaseInput';
+import { inject, ref, onMounted, computed, watch } from 'vue';
import PropTypes from '../../_util/vue-types';
import { createRef } from './util';
@@ -22,38 +22,42 @@ const SearchInput = {
ariaId: PropTypes.string,
isMultiple: PropTypes.looseBool.def(true),
},
- setup(props) {
+ setup(props, { emit }) {
const measureRef = ref();
const inputWidth = ref(0);
+ const mirrorSearchValue = ref(props.searchValue);
+ watch(
+ computed(() => props.searchValue),
+ () => {
+ mirrorSearchValue.value = props.searchValue;
+ },
+ );
+ watch(
+ mirrorSearchValue,
+ () => {
+ emit('mirrorSearchValueChange', mirrorSearchValue.value);
+ },
+ { immediate: true },
+ );
// We measure width and set to the input immediately
onMounted(() => {
- if(props.isMultiple) {
+ if (props.isMultiple) {
watch(
- computed(()=>props.searchValue),
+ mirrorSearchValue,
() => {
inputWidth.value = measureRef.value.scrollWidth;
},
{ flush: 'post', immediate: true },
);
}
-
});
return {
measureRef,
inputWidth,
vcTreeSelect: inject('vcTreeSelect', {}),
+ mirrorSearchValue,
};
},
- data() {
- return {
- mirrorSearchValue: this.searchValue,
- };
- },
- watch: {
- searchValue(val) {
- this.mirrorSearchValue = val;
- },
- },
created() {
this.inputRef = createRef();
this.prevProps = { ...this.$props };
@@ -80,7 +84,6 @@ const SearchInput = {
});
},
methods: {
-
/**
* Need additional timeout for focus cause parent dom is not ready when didMount trigger
*/
@@ -114,7 +117,15 @@ const SearchInput = {
},
render() {
- const { searchValue, prefixCls, disabled, renderPlaceholder, open, ariaId, isMultiple } = this.$props;
+ const {
+ searchValue,
+ prefixCls,
+ disabled,
+ renderPlaceholder,
+ open,
+ ariaId,
+ isMultiple,
+ } = this.$props;
const {
vcTreeSelect: { onSearchInputKeyDown },
handleInputChange,
@@ -123,27 +134,29 @@ const SearchInput = {
} = this;
return (
<>
-
- {withDirectives(
- ,
- [[antInput]],
- )}
- {isMultiple ?
- {mirrorSearchValue}
- : null}
+
+
+ {isMultiple ? (
+
+ {mirrorSearchValue}
+
+ ) : null}
{renderPlaceholder && !mirrorSearchValue ? renderPlaceholder() : null}
>
diff --git a/components/vc-tree-select/src/Select.jsx b/components/vc-tree-select/src/Select.jsx
index 8cd92746e..84cfc95c1 100644
--- a/components/vc-tree-select/src/Select.jsx
+++ b/components/vc-tree-select/src/Select.jsx
@@ -216,13 +216,19 @@ const Select = defineComponent({
this.setState(state);
this.needSyncKeys = {};
},
- '$data._valueList'() {
+ _valueList() {
this.$nextTick(() => {
this.forcePopupAlign();
});
},
- '$data._open'(open) {
+ _open(open) {
this.$nextTick(() => {
+ if (!open && !this.isSearchValueControlled()) {
+ this.setState({ _searchValue: '' });
+ }
+ if (open && !this.$data._searchValue) {
+ this.setState({ _filteredTreeNodes: null });
+ }
const { prefixCls } = this.$props;
const { _selectorValueList: selectorValueList, _valueEntities: valueEntities } = this.$data;
const isMultiple = this.isMultiple();
@@ -489,7 +495,6 @@ const Select = defineComponent({
newState._valueEntities || prevState._valueEntities,
);
});
-
return newState;
},
// ==================== Selector ====================
@@ -637,7 +642,6 @@ const Select = defineComponent({
disabled,
inputValue,
treeNodeLabelProp,
- multiple,
treeCheckable,
treeCheckStrictly,
autoClearSearchValue,
@@ -697,7 +701,7 @@ const Select = defineComponent({
// Clean up `searchValue` when this prop is set
if (autoClearSearchValue || inputValue === null) {
// Clean state `searchValue` if uncontrolled
- if (!this.isSearchValueControlled() && (multiple || treeCheckable)) {
+ if (!this.isSearchValueControlled()) {
this.setUncontrolledState({
_searchValue: '',
_filteredTreeNodes: null,
@@ -848,7 +852,6 @@ const Select = defineComponent({
if (isSet) {
// Do the search logic
const upperSearchValue = String(value).toUpperCase();
-
let filterTreeNodeFn = filterTreeNode;
if (filterTreeNode === false) {
filterTreeNodeFn = () => true;
diff --git a/components/vc-tree-select/src/Selector/SingleSelector.jsx b/components/vc-tree-select/src/Selector/SingleSelector.jsx
index fab71f15e..1f1ab4e0a 100644
--- a/components/vc-tree-select/src/Selector/SingleSelector.jsx
+++ b/components/vc-tree-select/src/Selector/SingleSelector.jsx
@@ -13,6 +13,16 @@ const SingleSelector = {
this.selectorRef = createRef();
this.inputRef = createRef();
},
+ data() {
+ return {
+ mirrorSearchValue: this.searchValue,
+ };
+ },
+ watch: {
+ searchValue(val) {
+ this.mirrorSearchValue = val;
+ },
+ },
methods: {
onPlaceholderClick() {
this.inputRef.current.focus();
@@ -24,19 +34,13 @@ const SingleSelector = {
this.selectorRef.current.blur();
},
_renderPlaceholder() {
- const {
- prefixCls,
- placeholder,
- searchPlaceholder,
- searchValue,
- selectorValueList,
- } = this.$props;
+ const { prefixCls, placeholder, searchPlaceholder, selectorValueList } = this.$props;
const currentPlaceholder = placeholder || searchPlaceholder;
if (!currentPlaceholder) return null;
- const hidden = searchValue || selectorValueList.length;
+ const hidden = this.mirrorSearchValue || selectorValueList.length;
// [Legacy] Not remove the placeholder
return (
@@ -51,12 +55,19 @@ const SingleSelector = {
);
},
+ onMirrorSearchValueChange(value) {
+ this.mirrorSearchValue = value;
+ },
renderSelection() {
const { selectorValueList, prefixCls } = this.$props;
const selectedValueNodes = [];
- if (selectorValueList.length) {
+ if (selectorValueList.length && !this.mirrorSearchValue) {
const { label, value } = selectorValueList[0];
- selectedValueNodes.push({label || value});
+ selectedValueNodes.push(
+
+ {label || value}
+ ,
+ );
}
selectedValueNodes.push(
);
+ onMirrorSearchValueChange={this.onMirrorSearchValueChange}
+ />,
+ );
return selectedValueNodes;
},
},
diff --git a/examples/App.vue b/examples/App.vue
index 9b4ccf68f..b8dfa18c9 100644
--- a/examples/App.vue
+++ b/examples/App.vue
@@ -5,7 +5,7 @@