Skip to content

Commit 68662b1

Browse files
authored
fix: no display of searchValue when autoClearSearchValue is false (#863)
* Fix clear of value bug when autoClearSearchValue is true * Add tests * Fix type * Formatting
1 parent 8f91ed3 commit 68662b1

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

src/BaseSelect.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export interface BaseSelectPrivateProps {
9898

9999
// >>> Search
100100
searchValue: string;
101+
autoClearSearchValue?: boolean;
101102
/** Trigger onSearch, return false to prevent trigger open event */
102103
onSearch: (
103104
searchValue: string,
@@ -245,6 +246,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
245246

246247
// Search
247248
searchValue,
249+
autoClearSearchValue,
248250
onSearch,
249251
onSearchSplit,
250252
tokenSeparators,
@@ -767,6 +769,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
767769
ref={selectorRef}
768770
id={id}
769771
showSearch={mergedShowSearch}
772+
autoClearSearchValue={autoClearSearchValue}
770773
mode={mode}
771774
activeDescendantId={activeDescendantId}
772775
tagRender={tagRender}

src/Select.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ const Select = React.forwardRef(
624624
// >>> Search
625625
searchValue={mergedSearchValue}
626626
onSearch={onInternalSearch}
627+
autoClearSearchValue={autoClearSearchValue}
627628
onSearchSplit={onInternalSearchSplit}
628629
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
629630
// >>> OptionList

src/Selector/MultipleSelector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const SelectSelector: React.FC<SelectorProps> = (props) => {
4545
values,
4646
open,
4747
searchValue,
48+
autoClearSearchValue,
4849
inputRef,
4950
placeholder,
5051
disabled,
@@ -79,8 +80,8 @@ const SelectSelector: React.FC<SelectorProps> = (props) => {
7980
const selectionPrefixCls = `${prefixCls}-selection`;
8081

8182
// ===================== Search ======================
82-
const inputValue = open || mode === 'tags' ? searchValue : '';
83-
const inputEditable: boolean = mode === 'tags' || (showSearch && (open || focused));
83+
const inputValue = open || (mode === "multiple" && autoClearSearchValue === false) || mode === 'tags' ? searchValue : '';
84+
const inputEditable: boolean = mode === 'tags' || (mode === "multiple" && autoClearSearchValue === false) || (showSearch && (open || focused));
8485

8586
// We measure width and set to the input immediately
8687
useLayoutEffect(() => {

src/Selector/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface InnerSelectorProps {
3131
values: DisplayValueType[];
3232
showSearch?: boolean;
3333
searchValue: string;
34+
autoClearSearchValue?: boolean;
3435
activeDescendantId?: string;
3536
open: boolean;
3637
tabIndex?: number;
@@ -60,6 +61,7 @@ export interface SelectorProps {
6061
mode: Mode;
6162
searchValue: string;
6263
activeValue: string;
64+
autoClearSearchValue: boolean;
6365
inputElement: JSX.Element;
6466
maxLength?: number;
6567

tests/Multiple.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,30 @@ describe('Select.Multiple', () => {
621621
expect(wrapper.find('.rc-select-item-option-state-icon').at(0).text()).toBe('');
622622
});
623623
});
624+
625+
describe("autoClearSearchValue", () => {
626+
it('search value should not show when autoClearSearchValue is undefined', () => {
627+
const wrapper = mount(
628+
<Select
629+
mode="multiple"
630+
open={false}
631+
showSearch={true}
632+
searchValue="test"
633+
/>,
634+
);
635+
expect(wrapper.find('input').props().value).toBe('');
636+
});
637+
it('search value should show when autoClearSearchValue is true', () => {
638+
const wrapper = mount(
639+
<Select
640+
mode="multiple"
641+
open={false}
642+
autoClearSearchValue={false}
643+
showSearch={true}
644+
searchValue="test"
645+
/>,
646+
);
647+
expect(wrapper.find('input').props().value).toBe('test');
648+
});
649+
});
624650
});

0 commit comments

Comments
 (0)