Skip to content

Commit 3f8c624

Browse files
committed
fix: multiple 下清空时少触发一次
1 parent ca86130 commit 3f8c624

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/BaseSelect/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,15 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
605605
onSearch(mergedSearchValue, { source: 'submit' });
606606
} else if (mode === 'multiple') {
607607
// `multiple` mode only clean the search value but not trigger event
608-
onSearch('', {
609-
source: 'blur',
610-
});
608+
if (mergedShowSearch) {
609+
onSearch('', {
610+
source: 'effect',
611+
});
612+
} else {
613+
onSearch('', {
614+
source: 'blur',
615+
});
616+
}
611617
}
612618
}
613619

tests/Select.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,49 @@ describe('Select.Basic', () => {
605605
jest.useRealTimers();
606606
});
607607

608+
describe('should call handleSearch twice on search and blur', () => {
609+
injectRunAllTimers(jest);
610+
611+
beforeEach(() => {
612+
jest.useFakeTimers();
613+
});
614+
615+
afterEach(() => {
616+
jest.useRealTimers();
617+
});
618+
619+
it('multiple mode should call handleSearch twice on search and blur', async () => {
620+
const handleSearch = jest.fn();
621+
const { container } = render(
622+
<Select showSearch onSearch={handleSearch} mode="multiple">
623+
<Option value="1">1</Option>
624+
<Option value="2">2</Option>
625+
</Select>,
626+
);
627+
fireEvent.change(container.querySelector('input')!, { target: { value: '1' } });
628+
// 模拟失去焦点(blur)事件
629+
fireEvent.blur(container.querySelector('input'));
630+
jest.runAllTimers();
631+
expect(handleSearch).toHaveBeenCalledTimes(2);
632+
jest.useRealTimers();
633+
});
634+
635+
it('not multiple mode should call handleSearch twice on search and blur', async () => {
636+
const handleSearch = jest.fn();
637+
const { container } = render(
638+
<Select showSearch onSearch={handleSearch}>
639+
<Option value="1">1</Option>
640+
<Option value="2">2</Option>
641+
</Select>,
642+
);
643+
fireEvent.change(container.querySelector('input')!, { target: { value: '1' } });
644+
fireEvent.blur(container.querySelector('input'));
645+
jest.runAllTimers();
646+
expect(handleSearch).toHaveBeenCalledTimes(2);
647+
jest.useRealTimers();
648+
});
649+
});
650+
608651
it('should render 0 as text properly', () => {
609652
const data = [
610653
{ text: 0, value: '=0' },

0 commit comments

Comments
 (0)