fix: Input.Search enterButton don't trigger click event (#32999)

close #32993
This commit is contained in:
afc163 2021-11-24 13:23:51 +08:00 committed by GitHub
parent 3886ad6462
commit 34d23c8850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

@ -68,10 +68,7 @@ const Search = React.forwardRef<Input, SearchProps>((props, ref) => {
const prefixCls = getPrefixCls('input-search', customizePrefixCls);
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
const searchIcon =
typeof enterButton === 'boolean' ? (
<SearchOutlined />
) : null;
const searchIcon = typeof enterButton === 'boolean' ? <SearchOutlined /> : null;
const btnClassName = `${prefixCls}-button`;
let button: React.ReactNode;
@ -81,7 +78,10 @@ const Search = React.forwardRef<Input, SearchProps>((props, ref) => {
if (isAntdButton || enterButtonAsElement.type === 'button') {
button = cloneElement(enterButtonAsElement, {
onMouseDown,
onClick: onSearch,
onClick: (e: React.MouseEvent<HTMLButtonElement>) => {
enterButtonAsElement?.props?.onClick?.(e);
onSearch(e);
},
key: 'enterButton',
...(isAntdButton
? {

View File

@ -110,10 +110,15 @@ describe('Input.Search', () => {
it('should trigger onSearch when click search button of native', () => {
const onSearch = jest.fn();
const onButtonClick = jest.fn();
const wrapper = mount(
<Search
defaultValue="search text"
enterButton={<button type="button">antd button</button>}
enterButton={
<button type="button" onClick={onButtonClick}>
antd button
</button>
}
onSearch={onSearch}
/>,
);
@ -126,6 +131,7 @@ describe('Input.Search', () => {
preventDefault: expect.any(Function),
}),
);
expect(onButtonClick).toHaveBeenCalledTimes(1);
});
it('should trigger onSearch when press enter', () => {