Skip to content

Commit 02c3af5

Browse files
committed
fix: tree-select search value logic
1 parent f82fcad commit 02c3af5

File tree

5 files changed

+68
-51
lines changed

5 files changed

+68
-51
lines changed

components/vc-tree-select/src/Base/BaseSelector.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const selectorPropTypes = () => ({
3232
focused: PropTypes.looseBool,
3333
isMultiple: PropTypes.looseBool,
3434
showSearch: PropTypes.looseBool,
35+
searchValue: PropTypes.string,
3536
});
3637

3738
function noop() {}

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ const SearchInput = {
2020
renderPlaceholder: PropTypes.func,
2121
needAlign: PropTypes.looseBool,
2222
ariaId: PropTypes.string,
23+
isMultiple: PropTypes.looseBool.def(true),
2324
},
2425
setup(props) {
2526
const measureRef = ref();
2627
const inputWidth = ref(0);
2728
// We measure width and set to the input immediately
2829
onMounted(() => {
29-
watch(
30-
computed(()=>props.searchValue),
31-
() => {
32-
inputWidth.value = measureRef.value.scrollWidth;
33-
},
34-
{ flush: 'post' },
35-
);
30+
if(props.isMultiple) {
31+
watch(
32+
computed(()=>props.searchValue),
33+
() => {
34+
inputWidth.value = measureRef.value.scrollWidth;
35+
},
36+
{ flush: 'post', immediate: true },
37+
);
38+
}
39+
3640
});
3741
return {
3842
measureRef,
@@ -52,15 +56,11 @@ const SearchInput = {
5256
},
5357
created() {
5458
this.inputRef = createRef();
55-
// this.mirrorInputRef = createRef();
5659
this.prevProps = { ...this.$props };
5760
},
5861
mounted() {
5962
this.$nextTick(() => {
60-
const { open, needAlign } = this.$props;
61-
if (needAlign) {
62-
this.alignInputWidth();
63-
}
63+
const { open } = this.$props;
6464

6565
if (open) {
6666
this.focus(true);
@@ -75,22 +75,11 @@ const SearchInput = {
7575
if (open && prevProps.open !== open) {
7676
this.focus();
7777
}
78-
// if (needAlign && searchValue !== prevProps.searchValue) {
79-
// this.alignInputWidth();
80-
// }
78+
8179
this.prevProps = { ...this.$props };
8280
});
8381
},
8482
methods: {
85-
/**
86-
* `scrollWidth` is not correct in IE, do the workaround.
87-
* ref: https://github.com/react-component/tree-select/issues/65
88-
* clientWidth 0 when mounted in vue. why?
89-
*/
90-
// alignInputWidth() {
91-
// this.inputRef.current.style.width = `${this.mirrorInputRef.current.clientWidth ||
92-
// this.mirrorInputRef.current.offsetWidth}px`;
93-
// },
9483

9584
/**
9685
* Need additional timeout for focus cause parent dom is not ready when didMount trigger
@@ -125,7 +114,7 @@ const SearchInput = {
125114
},
126115

127116
render() {
128-
const { searchValue, prefixCls, disabled, renderPlaceholder, open, ariaId } = this.$props;
117+
const { searchValue, prefixCls, disabled, renderPlaceholder, open, ariaId, isMultiple } = this.$props;
129118
const {
130119
vcTreeSelect: { onSearchInputKeyDown },
131120
handleInputChange,
@@ -134,7 +123,7 @@ const SearchInput = {
134123
} = this;
135124
return (
136125
<>
137-
<span class={`${prefixCls}-selection-search`} style={{ width: inputWidth + 'px' }}>
126+
<span class={`${prefixCls}-selection-search`} style={isMultiple ? { width: inputWidth + 'px' }:{}}>
138127
{withDirectives(
139128
<input
140129
type="text"
@@ -152,9 +141,9 @@ const SearchInput = {
152141
/>,
153142
[[antInput]],
154143
)}
155-
<span ref="measureRef" class={`${prefixCls}-selection-search-mirror`} aria-hidden>
144+
{isMultiple ? <span ref="measureRef" class={`${prefixCls}-selection-search-mirror`} aria-hidden>
156145
{mirrorSearchValue}&nbsp;
157-
</span>
146+
</span> : null}
158147
</span>
159148
{renderPlaceholder && !mirrorSearchValue ? renderPlaceholder() : null}
160149
</>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import KeyCode from '../../_util/KeyCode';
2929
import SelectTrigger from './SelectTrigger';
3030
import SingleSelector from './Selector/SingleSelector';
3131
import MultipleSelector from './Selector/MultipleSelector';
32-
import SinglePopup from './Popup/SinglePopup';
33-
import MultiplePopup from './Popup/MultiplePopup';
3432

3533
import { SHOW_ALL, SHOW_PARENT, SHOW_CHILD } from './strategies';
3634
import BaseMixin from '../../_util/BaseMixin';
@@ -58,6 +56,7 @@ import {
5856
getPropsData,
5957
findDOMNode,
6058
} from '../../_util/props-util';
59+
import BasePopup from './Popup/MultiplePopup';
6160
function getWatch(keys = []) {
6261
const watch = {};
6362
keys.forEach(k => {
@@ -1092,8 +1091,7 @@ const Select = defineComponent({
10921091
ref: this.setPopupRef,
10931092
};
10941093

1095-
const Popup = isMultiple ? MultiplePopup : SinglePopup;
1096-
const $popup = <Popup {...popupProps} __propsSymbol__={[]} />;
1094+
const $popup = <BasePopup {...popupProps} __propsSymbol__={[]} />;
10971095

10981096
const Selector = isMultiple ? MultipleSelector : SingleSelector;
10991097
const $selector = <Selector {...passProps} isMultiple={isMultiple} ref={this.selectorRef} />;

components/vc-tree-select/src/Selector/MultipleSelector/index.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const MultipleSelector = {
2323
...SearchInput.props,
2424
selectorValueList: PropTypes.array,
2525
disabled: PropTypes.looseBool,
26-
searchValue: PropTypes.string,
2726
labelInValue: PropTypes.looseBool,
2827
maxTagCount: PropTypes.number,
2928
maxTagPlaceholder: PropTypes.any,
@@ -142,11 +141,9 @@ const MultipleSelector = {
142141

143142
selectedValueNodes.push(
144143
<SearchInput
145-
{...{
146-
...this.$props,
147-
...this.$attrs,
148-
needAlign: true,
149-
}}
144+
key="SearchInput"
145+
{...this.$props}
146+
{...this.$attrs}
150147
ref={this.inputRef}
151148
>
152149
{children}

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

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import generateSelector, { selectorPropTypes } from '../Base/BaseSelector';
22
import { toTitle } from '../util';
33
import { getOptionProps } from '../../../_util/props-util';
44
import { createRef } from '../util';
5+
import SearchInput from '../SearchInput';
56
const Selector = generateSelector('single');
67

78
const SingleSelector = {
@@ -10,31 +11,61 @@ const SingleSelector = {
1011
props: selectorPropTypes(),
1112
created() {
1213
this.selectorRef = createRef();
14+
this.inputRef = createRef();
1315
},
1416
methods: {
17+
onPlaceholderClick() {
18+
this.inputRef.current.focus();
19+
},
1520
focus() {
1621
this.selectorRef.current.focus();
1722
},
1823
blur() {
1924
this.selectorRef.current.blur();
2025
},
21-
renderSelection() {
22-
const { selectorValueList, placeholder, prefixCls } = this.$props;
26+
_renderPlaceholder() {
27+
const {
28+
prefixCls,
29+
placeholder,
30+
searchPlaceholder,
31+
searchValue,
32+
selectorValueList,
33+
} = this.$props;
34+
35+
const currentPlaceholder = placeholder || searchPlaceholder;
36+
37+
if (!currentPlaceholder) return null;
2338

39+
const hidden = searchValue || selectorValueList.length;
40+
41+
// [Legacy] Not remove the placeholder
42+
return (
43+
<span
44+
style={{
45+
display: hidden ? 'none' : 'block',
46+
}}
47+
onClick={this.onPlaceholderClick}
48+
class={`${prefixCls}-selection-placeholder`}
49+
>
50+
{currentPlaceholder}
51+
</span>
52+
);
53+
},
54+
renderSelection() {
55+
const { selectorValueList, prefixCls } = this.$props;
56+
const selectedValueNodes = [];
2457
if (selectorValueList.length) {
2558
const { label, value } = selectorValueList[0];
26-
return (
27-
<span key="value" title={toTitle(label)} class={`${prefixCls}-selection-item`}>
28-
{label || value}
29-
</span>
30-
);
31-
} else {
32-
return (
33-
<span key="placeholder" class={`${prefixCls}-selection-placeholder`}>
34-
{placeholder}
35-
</span>
36-
);
59+
selectedValueNodes.push(<span key="value" title={toTitle(label)} class={`${prefixCls}-selection-item`}>{label || value}</span>);
3760
}
61+
selectedValueNodes.push(
62+
<SearchInput
63+
{...this.$props}
64+
{...this.$attrs}
65+
ref={this.inputRef}
66+
isMultiple={false}
67+
/>);
68+
return selectedValueNodes;
3869
},
3970
},
4071

@@ -43,6 +74,7 @@ const SingleSelector = {
4374
...getOptionProps(this),
4475
...this.$attrs,
4576
renderSelection: this.renderSelection,
77+
renderPlaceholder: this._renderPlaceholder,
4678
ref: this.selectorRef,
4779
};
4880
return <Selector {...props} />;

0 commit comments

Comments
 (0)