mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 22:36:31 +08:00
parent
98dfd2ac93
commit
6f695b050e
@ -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}
|
||||
/>
|
||||
|
@ -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(),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -253,6 +253,7 @@ exports[`Input.Search should support suffix 1`] = `
|
||||
>
|
||||
<Input
|
||||
className="ant-input-search"
|
||||
onChange={[Function]}
|
||||
onPressEnter={[Function]}
|
||||
prefixCls="ant-input"
|
||||
suffix={
|
||||
|
Loading…
Reference in New Issue
Block a user