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
  • Loading branch information
YSMJ1994 committed Nov 15, 2023
1 parent ab0314e commit 659ba46
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
8 changes: 3 additions & 5 deletions 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 Expand Up @@ -278,9 +280,6 @@ class Search extends React.Component {
});
this.props.onBlur && this.props.onBlur(...args);
}
changeHighlightKey = value => {
this.highlightKey = value;
};

render() {
const {
Expand Down Expand Up @@ -419,7 +418,6 @@ class Search extends React.Component {
ref={this.saveInputRef}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
changeHighlightKey={this.changeHighlightKey}
/>
</Group>
);
Expand Down
4 changes: 0 additions & 4 deletions src/select/auto-complete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class AutoComplete extends Base {
// 是否将当前高亮的选项作为 placeholder
highlightHolder: PropTypes.bool,
style: PropTypes.object,
changeHighlightKey: PropTypes.func,
};

static defaultProps = {
Expand Down Expand Up @@ -203,9 +202,6 @@ class AutoComplete extends Base {

// 不自动高亮的情况下, highlightKey 根据value精确值走,也就是被选中元素自动高亮,这样也不会影响不在选项内的用户搜索操作
if (!this.props.autoHighlightFirstItem) {
if (this.props.changeHighlightKey) {
this.props.changeHighlightKey(value);
}
this.setState({
highlightKey: value,
});
Expand Down
15 changes: 8 additions & 7 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,14 +79,13 @@ describe('Search', () => {
.text() === 'sc'
);
});
it('onSearch run only once', () => {
let isFlag = false;
const onSearch = () => {
isFlag = !isFlag;
assert(isFlag === true);
};
const wrapper = mount(<Search dataSource={[{ label: 'a', value: 'a' }]} value={'a'} onSearch={onSearch} />);
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();
});
});

Expand Down
1 change: 0 additions & 1 deletion types/select/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export interface AutoCompleteProps extends HTMLAttributesWeak, CommonProps {
* 默认高亮key
*/
defaultHighlightKey?: string;
changeHighlightKey?: (value: string) => void;
}

export class AutoComplete extends React.Component<AutoCompleteProps, any> {}
Expand Down

0 comments on commit 659ba46

Please sign in to comment.