Skip to content

Commit

Permalink
fix: multiple 下清空时少触发一次
Browse files Browse the repository at this point in the history
  • Loading branch information
su-muzhi committed Nov 6, 2024
1 parent ca86130 commit 3f8c624
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,15 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
onSearch(mergedSearchValue, { source: 'submit' });
} else if (mode === 'multiple') {
// `multiple` mode only clean the search value but not trigger event
onSearch('', {
source: 'blur',
});
if (mergedShowSearch) {
onSearch('', {
source: 'effect',
});
} else {
onSearch('', {

Check warning on line 613 in src/BaseSelect/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/BaseSelect/index.tsx#L612-L613

Added lines #L612 - L613 were not covered by tests
source: 'blur',
});
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Select showSearch onSearch={handleSearch} mode="multiple">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
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(
<Select showSearch onSearch={handleSearch}>
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
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' },
Expand Down

0 comments on commit 3f8c624

Please sign in to comment.