Skip to content

Commit 8c7ea9d

Browse files
authored
fix: User typing should not block input (#330)
1 parent 18fab04 commit 8c7ea9d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/InputNumber.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,18 @@ const InputNumber = React.forwardRef(
283283
const triggerValueUpdate = (newValue: DecimalClass, userTyping: boolean): DecimalClass => {
284284
let updateValue = newValue;
285285

286+
let isRangeValidate = isInRange(updateValue) || updateValue.isEmpty();
287+
286288
// Skip align value when trigger value is empty.
287289
// We just trigger onChange(null)
288-
if (!updateValue.isEmpty()) {
290+
// This should not block user typing
291+
if (!updateValue.isEmpty() && !userTyping) {
289292
// Revert value in range if needed
290293
updateValue = getRangeValue(updateValue) || updateValue;
294+
isRangeValidate = true;
291295
}
292296

293-
if (!readOnly && !disabled) {
297+
if (!readOnly && !disabled && isRangeValidate) {
294298
const numStr = updateValue.toString();
295299
const mergedPrecision = getPrecision(numStr, userTyping);
296300
if (mergedPrecision >= 0) {

tests/github.test.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ describe('InputNumber.Github', () => {
239239

240240
wrapper.focusInput();
241241
wrapper.changeValue('123');
242-
expect(onChange).toHaveBeenCalledTimes(1);
243-
expect(onChange).toHaveBeenCalledWith(10);
242+
expect(onChange).toHaveBeenCalledTimes(0);
244243
expect(onInput).toHaveBeenCalledTimes(1);
245244
expect(onInput).toHaveBeenCalledWith('123');
246245

247246
wrapper.blurInput();
248247
expect(onChange).toHaveBeenCalledTimes(1);
248+
expect(onChange).toHaveBeenCalledWith(10);
249249
expect(onInput).toHaveBeenCalledTimes(1);
250250

251251
// repeat it, it should works in same way
@@ -260,6 +260,33 @@ describe('InputNumber.Github', () => {
260260
expect(onInput).toHaveBeenCalledTimes(2);
261261
});
262262

263+
// https://github.com/ant-design/ant-design/issues/30465
264+
it('not block user input with min & max', () => {
265+
const onChange = jest.fn();
266+
const wrapper = mount(<InputNumber min={1900} onChange={onChange} />);
267+
268+
wrapper.focusInput();
269+
270+
wrapper.changeValue('2');
271+
expect(onChange).not.toHaveBeenCalled();
272+
273+
wrapper.changeValue('20');
274+
expect(onChange).not.toHaveBeenCalled();
275+
276+
wrapper.changeValue('200');
277+
expect(onChange).not.toHaveBeenCalled();
278+
279+
wrapper.changeValue('2000');
280+
expect(onChange).toHaveBeenCalledWith(2000);
281+
onChange.mockRestore();
282+
283+
wrapper.changeValue('1');
284+
expect(onChange).not.toHaveBeenCalled();
285+
286+
wrapper.blurInput();
287+
expect(onChange).toHaveBeenCalledWith(1900);
288+
});
289+
263290
// https://github.com/ant-design/ant-design/issues/7867
264291
it('focus should not cut precision of input value', () => {
265292
const Demo = () => {

0 commit comments

Comments
 (0)