fix: shouldn't trigger onBlur when clear input (#31202)

close #31200
This commit is contained in:
afc163 2021-06-30 13:48:22 +08:00 committed by GitHub
parent 19cd3519dd
commit 14521d32d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -65,6 +65,9 @@ class ClearableLabeledInput extends React.Component<ClearableInputProps> {
return (
<CloseCircleFilled
onClick={handleReset}
// Do not trigger onBlur when clear input
// https://github.com/ant-design/ant-design/issues/31200
onMouseDown={e => e.preventDefault()}
className={classNames(
{
[`${className}-hidden`]: !needClear,

View File

@ -212,4 +212,20 @@ describe('Input allowClear', () => {
expect(wrapper.getDOMNode().className.includes('my-class-name')).toBe(true);
expect(wrapper.find('input').getDOMNode().className.includes('my-class-name')).toBe(false);
});
// https://github.com/ant-design/ant-design/issues/31200
it('should not lost focus when clear input', () => {
const onBlur = jest.fn();
const wrapper = mount(<Input allowClear defaultValue="value" onBlur={onBlur} />, {
attachTo: document.body,
});
wrapper.find('input').getDOMNode().focus();
wrapper.find('.ant-input-clear-icon').at(0).simulate('mouseDown');
wrapper.find('.ant-input-clear-icon').at(0).simulate('click');
wrapper.find('.ant-input-clear-icon').at(0).simulate('mouseUp');
wrapper.find('.ant-input-clear-icon').at(0).simulate('focus');
wrapper.find('.ant-input-clear-icon').at(0).getDOMNode().click();
expect(onBlur).not.toBeCalled();
wrapper.unmount();
});
});