Skip to content

Commit

Permalink
fix(Search): press enter will call onSearch once when autoHighlightFi…
Browse files Browse the repository at this point in the history
…rstItem=false, close #4049 (#4524)

* fix(Search): onSearch run only once close #4049

* fix(Search): trigger a onsearch event close #4049

* fix(Search): press enter will call onSearch once when autoHighlightFirstItem=false, close #4049

---------

Co-authored-by: 珵之 <[email protected]>
  • Loading branch information
YunMeng99 and YSMJ1994 authored Nov 15, 2023
1 parent 26a08d8 commit 1eea155
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ class Search extends React.Component {
};

onPressEnter = () => {
if (this.highlightKey) {
// when autoHighlightFirstItem = false, onToggleHighlightItem will not trigger, cause this.highlightKey is empty
// and trigger onSearch twice
if (this.highlightKey || !this.props.autoHighlightFirstItem) {
return;
}
this.onSearch();
Expand Down
10 changes: 10 additions & 0 deletions test/search/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import enUS from '../../src/locale/en-us';

Enzyme.configure({ adapter: new Adapter() });

const sleep = duration => new Promise(resolve => setTimeout(resolve, duration));

/* eslint-disable no-undef, react/jsx-filename-extension */
describe('Search', () => {
describe('render', () => {
Expand Down Expand Up @@ -77,6 +79,14 @@ describe('Search', () => {
.text() === 'sc'
);
});
it.only('onSearch run only once', async () => {
const onSearch = sinon.spy();
const wrapper = mount(<Search dataSource={['a']} onSearch={onSearch} autoHighlightFirstItem={false} />);
wrapper.find('input').simulate('change', { target: { value: 'a' } });
wrapper.find('input').simulate('keydown', { keyCode: 13 });
assert(onSearch.calledOnce);
wrapper.unmount();
});
});

describe('behavior', () => {
Expand Down

0 comments on commit 1eea155

Please sign in to comment.