Skip to content

Commit b1c8e51

Browse files
authored
feat: support autoClearSearchValue (#422)
* feat: support autoClearSearchValue * docs: add autoClearSearchValue * docs: adjust * test: add case * fix: pass autoClearSearchValue * chore: adjust code
1 parent e707523 commit b1c8e51

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ React.render(
115115
</tr>
116116
</thead>
117117
<tbody>
118+
<tr>
119+
<td>autoClearSearchValue</td>
120+
<td>boolean</td>
121+
<td>true</td>
122+
<td>Whether the current search will be cleared on selecting an item. Only applies when checkable</td>
123+
</tr>
118124
<tr>
119125
<td>options</td>
120126
<td>Object</td>

src/Cascader.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionTyp
7777
showCheckedStrategy?: ShowCheckedStrategy;
7878

7979
// Search
80+
autoClearSearchValue?: boolean;
8081
showSearch?: boolean | ShowSearchType<OptionType>;
8182
searchValue?: string;
8283
onSearch?: (value: string) => void;
@@ -181,6 +182,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
181182
checkable,
182183

183184
// Search
185+
autoClearSearchValue = true,
184186
searchValue,
185187
onSearch,
186188
showSearch,
@@ -346,7 +348,9 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
346348

347349
// =========================== Select ===========================
348350
const onInternalSelect = useEvent((valuePath: SingleValueType) => {
349-
setSearchValue('');
351+
if (!multiple || autoClearSearchValue) {
352+
setSearchValue('');
353+
}
350354
if (!multiple) {
351355
triggerChange(valuePath);
352356
} else {
@@ -491,6 +495,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
491495
ref={ref as any}
492496
id={mergedId}
493497
prefixCls={prefixCls}
498+
autoClearSearchValue={autoClearSearchValue}
494499
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
495500
dropdownStyle={dropdownStyle}
496501
// Value

tests/search.spec.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,17 @@ describe('Cascader.Search', () => {
264264
'Label Bamboo / Label Little / Toy Fish',
265265
);
266266
});
267+
268+
it('autoClearSearchValue={false} should be worked', () => {
269+
const wrapper = mount(
270+
<Cascader options={options} showSearch checkable autoClearSearchValue={false} />,
271+
);
272+
273+
// Search
274+
wrapper.find('input').simulate('change', { target: { value: 'bamboo' } });
275+
276+
// Click
277+
wrapper.find('.rc-cascader-checkbox').first().simulate('click');
278+
expect(wrapper.find('input').prop('value')).toEqual('bamboo');
279+
});
267280
});

0 commit comments

Comments
 (0)