Skip to content

Commit

Permalink
update decrement logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabricevladimir committed Jan 30, 2024
1 parent becce8d commit 8f7d147
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
8 changes: 0 additions & 8 deletions src/components/input/inputNumber/inputNumber.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ describe('<InputNumber /> component', () => {
testDecrementLogic({ step, min, expectedValue: min.toString() });
});

it('should decrement to maximum if the initial value minus the step is greater than the maximum', () => {
const value = '100';
const step = 2;
const min = 1;
const max = 50;
testDecrementLogic({ value, step, min, max, expectedValue: max.toString() });
});

it('should decrement to the closest multiple of the step smaller than the value', () => {
const value = '10';
const step = 3;
Expand Down
17 changes: 11 additions & 6 deletions src/components/input/inputNumber/inputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,24 @@ export const InputNumber: React.FC<IInputNumberProps> = (props) => {
// ensure value is multiple of step
const newValue = (Math.floor(parsedValue / step) + 1) * step;

// ensure the new value is than the
// ensure the new value is than the max
setValue(Math.min(max, newValue).toString());
};

const handleDecrement = () => {
const parsedValue = Number(unmaskedValue ?? 0);

// decrement value by the step
let newValue = parsedValue % step !== 0 ? Math.floor(parsedValue / step) * step : parsedValue - step;
// decrement directly to the maximum if value is greater than the maximum
if (parsedValue > max) {
setValue(max.toString());
return;
}

// ensure value is multiple of step
const newValue = (Math.ceil(parsedValue / step) - 1) * step;

// ensure the new value is within the min and max range
newValue = Math.max(min, Math.min(max, newValue));
setValue(newValue.toString());
// ensure the new value is than the max
setValue(Math.max(min, newValue).toString());
};

return (
Expand Down

0 comments on commit 8f7d147

Please sign in to comment.