Skip to content

Commit

Permalink
Merge pull request #416 from Tencent/fix/input-number
Browse files Browse the repository at this point in the history
fix(input-number): 修复无初始值情况下点击增加存在小于min值的问题
  • Loading branch information
carolin913 authored Mar 3, 2022
2 parents cc8bc9f + d9d5fdb commit 930fd3d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/input-number/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const InputNumber = forwardRef((props: InputNumberProps, ref: React.Ref<HTMLInpu
onKeydown,
onKeyup,
onKeypress,

...restInputProps
} = props;

Expand Down Expand Up @@ -92,8 +91,8 @@ const InputNumber = forwardRef((props: InputNumberProps, ref: React.Ref<HTMLInpu
};

const [isError, setError] = useState<boolean>(false);
const disabledDecrease = disabled || isError || decimalValue - step < min;
const disabledIncrease = disabled || isError || decimalValue + step > max;
const disabledDecrease = disabled || isError || (decimalValue - step < min && internalInputValue !== '');
const disabledIncrease = disabled || isError || (decimalValue + step > max && internalInputValue !== '');

const isOutOfRange = (number: number): boolean => number > max || number < min;
const checkInput = (inputStr: InputNumberInternalValue): boolean => {
Expand Down Expand Up @@ -157,19 +156,21 @@ const InputNumber = forwardRef((props: InputNumberProps, ref: React.Ref<HTMLInpu
const currentValue = decimalValue || 0;
const precision = getPrecision(currentValue);

let updateValue;
let updateValue: number;
switch (type) {
case 'add': {
updateValue = Number((currentValue + step).toFixed(precision));
const addedVal = currentValue + step;
updateValue = Number(Math.max(addedVal, min).toFixed(precision));
break;
}
case 'reduce': {
updateValue = Number((currentValue - step).toFixed(precision));
const reducedVal = currentValue - step;
updateValue = Number(Math.max(reducedVal, min).toFixed(precision));
break;
}
}

setInputValue(updateValue);
setInputValue(String(updateValue));
triggerValueUpdate({ value: updateValue, type, e });
e.preventDefault();
};
Expand Down

0 comments on commit 930fd3d

Please sign in to comment.