Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: onSearch be call onBlur #1085

Closed
4 changes: 2 additions & 2 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)

// Close will clean up single mode search text
React.useEffect(() => {
if (!mergedOpen && !multiple && mode !== 'combobox') {
if (!mergedOpen && !multiple && mode && mode !== 'combobox') {
su-muzhi marked this conversation as resolved.
Show resolved Hide resolved
onInternalSearch('', false, false);
}
}, [mergedOpen]);
Expand Down Expand Up @@ -603,7 +603,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
// `tags` mode should move `searchValue` into values
if (mode === 'tags') {
onSearch(mergedSearchValue, { source: 'submit' });
} else if (mode === 'multiple') {
} else if (!mode || mode === 'multiple') {
// `multiple` mode only clean the search value but not trigger event
onSearch('', {
source: 'blur',
Expand Down
47 changes: 47 additions & 0 deletions tests/BaseSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,51 @@ describe('BaseSelect', () => {

expect(container.querySelector('.rc-select-dropdown-placement-fallback')).toBeTruthy();
});

describe("Testing BaseSelect component's onContainerBlur params", () => {
su-muzhi marked this conversation as resolved.
Show resolved Hide resolved
it('mode with null, onBlur source is blur', () => {
const onSearch = jest.fn();
const { container } = render(
<BaseSelect
prefixCls="rc-select"
id="test"
displayValues={[]}
onDisplayValuesChange={() => {}}
searchValue="1"
showSearch
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' });
});

it('mode with multiple, onBlur source is blur', () => {
const onSearch = jest.fn();
const { container } = render(
<BaseSelect
prefixCls="rc-select"
mode="multiple"
id="test"
displayValues={[]}
onDisplayValuesChange={() => {}}
searchValue="1"
showSearch
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' });
});
});
});
28 changes: 26 additions & 2 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ describe('Select.Basic', () => {
selectItem(container);
expect(handleSearch).toHaveBeenCalledTimes(1);

// Should trigger onBlur
// Should not trigger onBlur
fireEvent.change(container.querySelector('input'), { target: { value: '3' } });
expect(handleSearch).toHaveBeenCalledTimes(2);
fireEvent.blur(container.querySelector('input'));
jest.runAllTimers();
expect(handleSearch).toHaveBeenCalledTimes(3);
expect(handleSearch).toHaveBeenCalledTimes(2);
su-muzhi marked this conversation as resolved.
Show resolved Hide resolved

jest.useRealTimers();
});
Expand Down Expand Up @@ -2367,4 +2367,28 @@ describe('Select.Basic', () => {
expect(element[0]).not.toHaveClass('rc-select-item-option-disabled');
expect(element[1]).toHaveClass('rc-select-item-option-disabled');
});

it('click item and blur should trigger onBlur but not trigger onSearch', () => {
const onSearch = jest.fn();
const onBlur = jest.fn();

const Demo = () => (
<Select onSearch={onSearch} showSearch onBlur={onBlur}>
<Option value="11">11</Option>
<Option value="22">22</Option>
<Option value="33">33</Option>
</Select>
);

const { container } = render(<Demo />);
const input = container.querySelector('input');
fireEvent.change(input, { target: { value: '1' } });
fireEvent.click(
container.querySelectorAll('.rc-select-dropdown .rc-select-item-option-content')[0],
);
fireEvent.blur(input);
expect(container.querySelector('.rc-select-dropdown-hidden')).toBeTruthy();
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onBlur).toHaveBeenCalledTimes(1);
});
});
Loading