🐛 Input.Search should trigger onSearch when clear

close #18729
This commit is contained in:
afc163 2019-09-12 15:47:02 +08:00 committed by 偏右
parent 98dfd2ac93
commit 6f695b050e
3 changed files with 30 additions and 1 deletions

View File

@ -9,7 +9,10 @@ export interface SearchProps extends InputProps {
inputPrefixCls?: string;
onSearch?: (
value: string,
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLInputElement>,
event?:
| React.ChangeEvent<HTMLInputElement>
| React.MouseEvent<HTMLElement>
| React.KeyboardEvent<HTMLInputElement>,
) => void;
enterButton?: boolean | React.ReactNode;
}
@ -25,6 +28,16 @@ export default class Search extends React.Component<SearchProps, any> {
this.input = node;
};
onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { onChange, onSearch } = this.props;
if (e && e.target && e.type === 'click' && onSearch) {
onSearch((e as React.ChangeEvent<HTMLInputElement>).target.value, e);
}
if (onChange) {
onChange(e);
}
};
onSearch = (e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLInputElement>) => {
const { onSearch } = this.props;
if (onSearch) {
@ -141,6 +154,7 @@ export default class Search extends React.Component<SearchProps, any> {
prefixCls={inputPrefixCls}
addonAfter={this.renderAddonAfter(prefixCls)}
suffix={this.renderSuffix(prefixCls)}
onChange={this.onChange}
ref={this.saveInput}
className={inputClassName}
/>

View File

@ -137,4 +137,18 @@ describe('Input.Search', () => {
expect(wrapper.render()).toMatchSnapshot();
expect(wrapperWithEnterButton.render()).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/18729
it('should trigger onSearch when click clear icon', () => {
const onSearch = jest.fn();
const wrapper = mount(<Search allowClear defaultValue="value" onSearch={onSearch} />);
wrapper
.find('.ant-input-clear-icon')
.at(0)
.simulate('click');
expect(onSearch).toHaveBeenLastCalledWith(
'',
expect.anything(),
);
});
});

View File

@ -253,6 +253,7 @@ exports[`Input.Search should support suffix 1`] = `
>
<Input
className="ant-input-search"
onChange={[Function]}
onPressEnter={[Function]}
prefixCls="ant-input"
suffix={