diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index 2c0088fe..5a16cc01 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -283,14 +283,18 @@ const InputNumber = React.forwardRef( const triggerValueUpdate = (newValue: DecimalClass, userTyping: boolean): DecimalClass => { let updateValue = newValue; + let isRangeValidate = isInRange(updateValue) || updateValue.isEmpty(); + // Skip align value when trigger value is empty. // We just trigger onChange(null) - if (!updateValue.isEmpty()) { + // This should not block user typing + if (!updateValue.isEmpty() && !userTyping) { // Revert value in range if needed updateValue = getRangeValue(updateValue) || updateValue; + isRangeValidate = true; } - if (!readOnly && !disabled) { + if (!readOnly && !disabled && isRangeValidate) { const numStr = updateValue.toString(); const mergedPrecision = getPrecision(numStr, userTyping); if (mergedPrecision >= 0) { diff --git a/tests/github.test.tsx b/tests/github.test.tsx index 23b7f0f0..cd269cad 100644 --- a/tests/github.test.tsx +++ b/tests/github.test.tsx @@ -239,13 +239,13 @@ describe('InputNumber.Github', () => { wrapper.focusInput(); wrapper.changeValue('123'); - expect(onChange).toHaveBeenCalledTimes(1); - expect(onChange).toHaveBeenCalledWith(10); + expect(onChange).toHaveBeenCalledTimes(0); expect(onInput).toHaveBeenCalledTimes(1); expect(onInput).toHaveBeenCalledWith('123'); wrapper.blurInput(); expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange).toHaveBeenCalledWith(10); expect(onInput).toHaveBeenCalledTimes(1); // repeat it, it should works in same way @@ -260,6 +260,33 @@ describe('InputNumber.Github', () => { expect(onInput).toHaveBeenCalledTimes(2); }); + // https://github.com/ant-design/ant-design/issues/30465 + it('not block user input with min & max', () => { + const onChange = jest.fn(); + const wrapper = mount(); + + wrapper.focusInput(); + + wrapper.changeValue('2'); + expect(onChange).not.toHaveBeenCalled(); + + wrapper.changeValue('20'); + expect(onChange).not.toHaveBeenCalled(); + + wrapper.changeValue('200'); + expect(onChange).not.toHaveBeenCalled(); + + wrapper.changeValue('2000'); + expect(onChange).toHaveBeenCalledWith(2000); + onChange.mockRestore(); + + wrapper.changeValue('1'); + expect(onChange).not.toHaveBeenCalled(); + + wrapper.blurInput(); + expect(onChange).toHaveBeenCalledWith(1900); + }); + // https://github.com/ant-design/ant-design/issues/7867 it('focus should not cut precision of input value', () => { const Demo = () => {