diff --git a/src/BaseSelect/index.tsx b/src/BaseSelect/index.tsx index a2a15e8c..5a421c04 100644 --- a/src/BaseSelect/index.tsx +++ b/src/BaseSelect/index.tsx @@ -606,7 +606,7 @@ const BaseSelect = React.forwardRef((props, ref) } else if (mode === 'multiple') { // `multiple` mode only clean the search value but not trigger event onSearch('', { - source: 'blur', + source: mergedShowSearch ? 'effect' : 'blur', }); } } diff --git a/tests/BaseSelect.test.tsx b/tests/BaseSelect.test.tsx index 6a1fb514..e154711f 100644 --- a/tests/BaseSelect.test.tsx +++ b/tests/BaseSelect.test.tsx @@ -123,4 +123,54 @@ describe('BaseSelect', () => { expect(container.querySelector('.rc-select-dropdown-placement-fallback')).toBeTruthy(); }); + + describe("Testing BaseSelect component's onContainerBlur params", () => { + it('BaseSelect with showSearch, onContainerBlur params is effect', () => { + const onSearch = jest.fn(); + const { container } = render( + {}} + searchValue="1" + showSearch + open + onSearch={onSearch} + OptionList={OptionList} + emptyOptions + />, + ); + expect(container.querySelector('div.rc-select')).toBeTruthy(); + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); + fireEvent.blur(container.querySelector('div.rc-select')); + expect(onSearch).toHaveBeenCalledWith('', { source: 'effect' }); + }); + + it('BaseSelect without showSearch, onContainerBlur params is blur', () => { + const onSearch = jest.fn(); + const { container } = render( + {}} + searchValue="1" + showSearch={false} + open + onSearch={onSearch} + OptionList={OptionList} + emptyOptions + />, + ); + expect(container.querySelector('div.rc-select')).toBeTruthy(); + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); + fireEvent.blur(container.querySelector('div.rc-select')); + expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' }); + }); + }); }); diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index a37330dd..caca556c 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -605,6 +605,49 @@ describe('Select.Basic', () => { jest.useRealTimers(); }); + describe('should call handleSearch twice on search and blur', () => { + injectRunAllTimers(jest); + + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('multiple mode should call handleSearch twice on search and blur', async () => { + const handleSearch = jest.fn(); + const { container } = render( + , + ); + fireEvent.change(container.querySelector('input')!, { target: { value: '1' } }); + // 模拟失去焦点(blur)事件 + fireEvent.blur(container.querySelector('input')); + jest.runAllTimers(); + expect(handleSearch).toHaveBeenCalledTimes(2); + jest.useRealTimers(); + }); + + it('not multiple mode should call handleSearch twice on search and blur', async () => { + const handleSearch = jest.fn(); + const { container } = render( + , + ); + fireEvent.change(container.querySelector('input')!, { target: { value: '1' } }); + fireEvent.blur(container.querySelector('input')); + jest.runAllTimers(); + expect(handleSearch).toHaveBeenCalledTimes(2); + jest.useRealTimers(); + }); + }); + it('should render 0 as text properly', () => { const data = [ { text: 0, value: '=0' },