Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') {
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", () => {
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);

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