Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 29 additions & 2 deletions tests/github.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(<InputNumber min={1900} onChange={onChange} />);

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 = () => {
Expand Down