Skip to content

Commit

Permalink
fix: onBlur should trigger blur when mode is null
Browse files Browse the repository at this point in the history
  • Loading branch information
su-muzhi committed Nov 12, 2024
1 parent a47fa76 commit c4fd947
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)

if (onSearch && mergedSearchValue !== newSearchText) {
onSearch(newSearchText, {
source: fromTyping ? 'typing' : mergedShowSearch ? 'blur' : 'effect',
source: fromTyping ? 'typing' : 'effect',
});
}

Expand All @@ -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
6 changes: 2 additions & 4 deletions tests/BaseSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('BaseSelect', () => {
});

describe("Testing BaseSelect component's onContainerBlur params", () => {
it('mode with null, onContainerBlur params is blur', () => {
it('mode with null, onBlur source is blur', () => {
const onSearch = jest.fn();
const { container } = render(
<BaseSelect
Expand All @@ -135,7 +135,6 @@ describe('BaseSelect', () => {
onDisplayValuesChange={() => {}}
searchValue="1"
showSearch
open
onSearch={onSearch}
OptionList={OptionList}
emptyOptions
Expand All @@ -148,7 +147,7 @@ describe('BaseSelect', () => {
expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' });
});

it('mode with multiple, onContainerBlur params is blur', () => {
it('mode with multiple, onBlur source is blur', () => {
const onSearch = jest.fn();
const { container } = render(
<BaseSelect
Expand All @@ -159,7 +158,6 @@ describe('BaseSelect', () => {
onDisplayValuesChange={() => {}}
searchValue="1"
showSearch
open
onSearch={onSearch}
OptionList={OptionList}
emptyOptions
Expand Down
7 changes: 7 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ describe('Select.Basic', () => {
selectItem(container);
expect(handleSearch).toHaveBeenCalledTimes(1);

// 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(2);

jest.useRealTimers();
});

Expand Down

0 comments on commit c4fd947

Please sign in to comment.