diff --git a/components/input/Input.tsx b/components/input/Input.tsx index a8f22428d5..f329db6845 100644 --- a/components/input/Input.tsx +++ b/components/input/Input.tsx @@ -75,18 +75,30 @@ export function resolveOnChange { + // setQuery((prevStatus) => e.target.value); + // }} + // /> + + const currentTarget = target.cloneNode(true) as E; + + event.target = currentTarget; + event.currentTarget = currentTarget; + + currentTarget.value = ''; onChange(event as React.ChangeEvent); - // reset target ref value - target.value = originalInputValue; return; } diff --git a/components/input/__tests__/index.test.js b/components/input/__tests__/index.test.js index 4638dda0da..7535e91aef 100644 --- a/components/input/__tests__/index.test.js +++ b/components/input/__tests__/index.test.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { mount } from 'enzyme'; // eslint-disable-next-line import/no-unresolved import Form from '../../form'; @@ -228,4 +228,31 @@ describe('Input allowClear', () => { expect(onBlur).not.toBeCalled(); wrapper.unmount(); }); + + // https://github.com/ant-design/ant-design/issues/31927 + it('should correctly when useState', () => { + const App = () => { + const [query, setQuery] = useState(''); + return ( + { + setQuery(() => e.target.value); + }} + /> + ); + }; + + const wrapper = mount(); + + wrapper.find('input').getDOMNode().focus(); + wrapper.find('input').simulate('change', { target: { value: '111' } }); + expect(wrapper.find('input').getDOMNode().value).toEqual('111'); + + wrapper.find('.ant-input-clear-icon').at(0).simulate('click'); + expect(wrapper.find('input').getDOMNode().value).toEqual(''); + + wrapper.unmount(); + }); }); diff --git a/components/input/__tests__/textarea.test.js b/components/input/__tests__/textarea.test.js index 1bb392f70c..62f406e193 100644 --- a/components/input/__tests__/textarea.test.js +++ b/components/input/__tests__/textarea.test.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { mount } from 'enzyme'; import RcTextArea from 'rc-textarea'; import Input from '..'; @@ -380,14 +380,14 @@ describe('TextArea allowClear', () => { expect(wrapper.find('textarea').at(0).getDOMNode().value).toBe('Light'); }); - it('onChange event should return HTMLInputElement', () => { + it('onChange event should return HTMLTextAreaElement', () => { const onChange = jest.fn(); const wrapper = mount(); function isNativeElement() { expect(onChange).toHaveBeenCalledWith( expect.objectContaining({ - target: wrapper.find('textarea').instance(), + target: expect.any(HTMLTextAreaElement), }), ); @@ -411,4 +411,31 @@ describe('TextArea allowClear', () => { wrapper.find('.ant-input-clear-icon').first().simulate('click'); isNativeElement(); }); + + // https://github.com/ant-design/ant-design/issues/31927 + it('should correctly when useState', () => { + const App = () => { + const [query, setQuery] = useState(''); + return ( +