Skip to content

Commit e066785

Browse files
committed
fix: tree-select placeholder error
1 parent 02c3af5 commit e066785

File tree

6 files changed

+138
-58
lines changed

6 files changed

+138
-58
lines changed

antdv-demo

components/_util/BaseInput.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { defineComponent, ref, withDirectives } from 'vue';
2+
import antInput from './antInputDirective';
3+
import PropTypes from './vue-types';
4+
const BaseInput = defineComponent({
5+
props: {
6+
value: PropTypes.string.def(''),
7+
},
8+
emits: ['change', 'input'],
9+
setup(_p, { emit }) {
10+
const inputRef = ref(null);
11+
const handleChange = (e: Event) => {
12+
const { composing } = e.target as any;
13+
if ((e as any).isComposing || composing) {
14+
emit('input', e);
15+
} else {
16+
emit('input', e);
17+
emit('change', e);
18+
}
19+
};
20+
return {
21+
inputRef,
22+
focus: () => {
23+
if (inputRef.value) {
24+
inputRef.value.focus();
25+
}
26+
},
27+
blur: () => {
28+
if (inputRef.value) {
29+
inputRef.value.blur();
30+
}
31+
},
32+
handleChange,
33+
};
34+
},
35+
render() {
36+
return withDirectives(
37+
(
38+
<input
39+
{...this.$props}
40+
{...this.$attrs}
41+
onInput={this.handleChange}
42+
onChange={this.handleChange}
43+
ref="inputRef"
44+
/>
45+
) as any,
46+
[[antInput]],
47+
);
48+
},
49+
});
50+
51+
export default BaseInput;

components/vc-tree-select/src/SearchInput.jsx

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* - multiple: in the selector
55
* Move the code as a SearchInput for easy management.
66
*/
7-
import { inject, withDirectives, ref, onMounted, computed, watch } from 'vue';
8-
import antInput from '../../_util/antInputDirective';
7+
import BaseInput from '../../_util/BaseInput';
8+
import { inject, ref, onMounted, computed, watch } from 'vue';
99
import PropTypes from '../../_util/vue-types';
1010
import { createRef } from './util';
1111

@@ -22,38 +22,42 @@ const SearchInput = {
2222
ariaId: PropTypes.string,
2323
isMultiple: PropTypes.looseBool.def(true),
2424
},
25-
setup(props) {
25+
setup(props, { emit }) {
2626
const measureRef = ref();
2727
const inputWidth = ref(0);
28+
const mirrorSearchValue = ref(props.searchValue);
29+
watch(
30+
computed(() => props.searchValue),
31+
() => {
32+
mirrorSearchValue.value = props.searchValue;
33+
},
34+
);
35+
watch(
36+
mirrorSearchValue,
37+
() => {
38+
emit('mirrorSearchValueChange', mirrorSearchValue.value);
39+
},
40+
{ immediate: true },
41+
);
2842
// We measure width and set to the input immediately
2943
onMounted(() => {
30-
if(props.isMultiple) {
44+
if (props.isMultiple) {
3145
watch(
32-
computed(()=>props.searchValue),
46+
mirrorSearchValue,
3347
() => {
3448
inputWidth.value = measureRef.value.scrollWidth;
3549
},
3650
{ flush: 'post', immediate: true },
3751
);
3852
}
39-
4053
});
4154
return {
4255
measureRef,
4356
inputWidth,
4457
vcTreeSelect: inject('vcTreeSelect', {}),
58+
mirrorSearchValue,
4559
};
4660
},
47-
data() {
48-
return {
49-
mirrorSearchValue: this.searchValue,
50-
};
51-
},
52-
watch: {
53-
searchValue(val) {
54-
this.mirrorSearchValue = val;
55-
},
56-
},
5761
created() {
5862
this.inputRef = createRef();
5963
this.prevProps = { ...this.$props };
@@ -80,7 +84,6 @@ const SearchInput = {
8084
});
8185
},
8286
methods: {
83-
8487
/**
8588
* Need additional timeout for focus cause parent dom is not ready when didMount trigger
8689
*/
@@ -114,7 +117,15 @@ const SearchInput = {
114117
},
115118

116119
render() {
117-
const { searchValue, prefixCls, disabled, renderPlaceholder, open, ariaId, isMultiple } = this.$props;
120+
const {
121+
searchValue,
122+
prefixCls,
123+
disabled,
124+
renderPlaceholder,
125+
open,
126+
ariaId,
127+
isMultiple,
128+
} = this.$props;
118129
const {
119130
vcTreeSelect: { onSearchInputKeyDown },
120131
handleInputChange,
@@ -123,27 +134,29 @@ const SearchInput = {
123134
} = this;
124135
return (
125136
<>
126-
<span class={`${prefixCls}-selection-search`} style={isMultiple ? { width: inputWidth + 'px' }:{}}>
127-
{withDirectives(
128-
<input
129-
type="text"
130-
ref={this.inputRef}
131-
onInput={handleInputChange}
132-
onChange={handleInputChange}
133-
onKeydown={onSearchInputKeyDown}
134-
value={searchValue}
135-
disabled={disabled}
136-
class={`${prefixCls}-selection-search-input`}
137-
aria-label="filter select"
138-
aria-autocomplete="list"
139-
aria-controls={open ? ariaId : undefined}
140-
aria-multiline="false"
141-
/>,
142-
[[antInput]],
143-
)}
144-
{isMultiple ? <span ref="measureRef" class={`${prefixCls}-selection-search-mirror`} aria-hidden>
145-
{mirrorSearchValue}&nbsp;
146-
</span> : null}
137+
<span
138+
class={`${prefixCls}-selection-search`}
139+
style={isMultiple ? { width: inputWidth + 'px' } : {}}
140+
>
141+
<BaseInput
142+
type="text"
143+
ref={this.inputRef}
144+
onInput={handleInputChange}
145+
onChange={handleInputChange}
146+
onKeydown={onSearchInputKeyDown}
147+
value={searchValue}
148+
disabled={disabled}
149+
class={`${prefixCls}-selection-search-input`}
150+
aria-label="filter select"
151+
aria-autocomplete="list"
152+
aria-controls={open ? ariaId : undefined}
153+
aria-multiline="false"
154+
/>
155+
{isMultiple ? (
156+
<span ref="measureRef" class={`${prefixCls}-selection-search-mirror`} aria-hidden>
157+
{mirrorSearchValue}&nbsp;
158+
</span>
159+
) : null}
147160
</span>
148161
{renderPlaceholder && !mirrorSearchValue ? renderPlaceholder() : null}
149162
</>

components/vc-tree-select/src/Select.jsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,19 @@ const Select = defineComponent({
216216
this.setState(state);
217217
this.needSyncKeys = {};
218218
},
219-
'$data._valueList'() {
219+
_valueList() {
220220
this.$nextTick(() => {
221221
this.forcePopupAlign();
222222
});
223223
},
224-
'$data._open'(open) {
224+
_open(open) {
225225
this.$nextTick(() => {
226+
if (!open && !this.isSearchValueControlled()) {
227+
this.setState({ _searchValue: '' });
228+
}
229+
if (open && !this.$data._searchValue) {
230+
this.setState({ _filteredTreeNodes: null });
231+
}
226232
const { prefixCls } = this.$props;
227233
const { _selectorValueList: selectorValueList, _valueEntities: valueEntities } = this.$data;
228234
const isMultiple = this.isMultiple();
@@ -489,7 +495,6 @@ const Select = defineComponent({
489495
newState._valueEntities || prevState._valueEntities,
490496
);
491497
});
492-
493498
return newState;
494499
},
495500
// ==================== Selector ====================
@@ -637,7 +642,6 @@ const Select = defineComponent({
637642
disabled,
638643
inputValue,
639644
treeNodeLabelProp,
640-
multiple,
641645
treeCheckable,
642646
treeCheckStrictly,
643647
autoClearSearchValue,
@@ -697,7 +701,7 @@ const Select = defineComponent({
697701
// Clean up `searchValue` when this prop is set
698702
if (autoClearSearchValue || inputValue === null) {
699703
// Clean state `searchValue` if uncontrolled
700-
if (!this.isSearchValueControlled() && (multiple || treeCheckable)) {
704+
if (!this.isSearchValueControlled()) {
701705
this.setUncontrolledState({
702706
_searchValue: '',
703707
_filteredTreeNodes: null,
@@ -848,7 +852,6 @@ const Select = defineComponent({
848852
if (isSet) {
849853
// Do the search logic
850854
const upperSearchValue = String(value).toUpperCase();
851-
852855
let filterTreeNodeFn = filterTreeNode;
853856
if (filterTreeNode === false) {
854857
filterTreeNodeFn = () => true;

components/vc-tree-select/src/Selector/SingleSelector.jsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ const SingleSelector = {
1313
this.selectorRef = createRef();
1414
this.inputRef = createRef();
1515
},
16+
data() {
17+
return {
18+
mirrorSearchValue: this.searchValue,
19+
};
20+
},
21+
watch: {
22+
searchValue(val) {
23+
this.mirrorSearchValue = val;
24+
},
25+
},
1626
methods: {
1727
onPlaceholderClick() {
1828
this.inputRef.current.focus();
@@ -24,19 +34,13 @@ const SingleSelector = {
2434
this.selectorRef.current.blur();
2535
},
2636
_renderPlaceholder() {
27-
const {
28-
prefixCls,
29-
placeholder,
30-
searchPlaceholder,
31-
searchValue,
32-
selectorValueList,
33-
} = this.$props;
37+
const { prefixCls, placeholder, searchPlaceholder, selectorValueList } = this.$props;
3438

3539
const currentPlaceholder = placeholder || searchPlaceholder;
3640

3741
if (!currentPlaceholder) return null;
3842

39-
const hidden = searchValue || selectorValueList.length;
43+
const hidden = this.mirrorSearchValue || selectorValueList.length;
4044

4145
// [Legacy] Not remove the placeholder
4246
return (
@@ -51,20 +55,29 @@ const SingleSelector = {
5155
</span>
5256
);
5357
},
58+
onMirrorSearchValueChange(value) {
59+
this.mirrorSearchValue = value;
60+
},
5461
renderSelection() {
5562
const { selectorValueList, prefixCls } = this.$props;
5663
const selectedValueNodes = [];
57-
if (selectorValueList.length) {
64+
if (selectorValueList.length && !this.mirrorSearchValue) {
5865
const { label, value } = selectorValueList[0];
59-
selectedValueNodes.push(<span key="value" title={toTitle(label)} class={`${prefixCls}-selection-item`}>{label || value}</span>);
66+
selectedValueNodes.push(
67+
<span key="value" title={toTitle(label)} class={`${prefixCls}-selection-item`}>
68+
{label || value}
69+
</span>,
70+
);
6071
}
6172
selectedValueNodes.push(
6273
<SearchInput
6374
{...this.$props}
6475
{...this.$attrs}
6576
ref={this.inputRef}
6677
isMultiple={false}
67-
/>);
78+
onMirrorSearchValueChange={this.onMirrorSearchValueChange}
79+
/>,
80+
);
6881
return selectedValueNodes;
6982
},
7083
},

examples/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</template>
66
<script>
77
import { defineComponent } from 'vue';
8-
import demo from '../antdv-demo/docs/select/demo';
8+
import demo from '../antdv-demo/docs/tree-select/demo';
99
// import Affix from '../components/affix';
1010
export default defineComponent({
1111
components: {

0 commit comments

Comments
 (0)