Skip to content

Commit 3788b98

Browse files
authored
fix(16865): fixes step operations in NumberInput (#17048)
1 parent 6686fc3 commit 3788b98

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

packages/react/src/components/NumberInput/NumberInput.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,23 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(
345345

346346
function handleStepperClick(event, direction) {
347347
if (inputRef.current) {
348-
direction === 'up'
349-
? inputRef.current.stepUp()
350-
: inputRef.current.stepDown();
351-
348+
const currentValue = Number(inputRef.current.value);
349+
let newValue =
350+
direction === 'up' ? currentValue + step : currentValue - step;
351+
if (min !== undefined) {
352+
newValue = Math.max(newValue, min);
353+
}
354+
if (max !== undefined) {
355+
newValue = Math.min(newValue, max);
356+
}
357+
const currentInputValue = inputRef.current
358+
? inputRef.current.value
359+
: '';
352360
const state = {
353361
value:
354-
allowEmpty && inputRef.current.value === ''
362+
allowEmpty && currentInputValue === '' && step === 0
355363
? ''
356-
: Number(inputRef.current.value),
364+
: newValue,
357365
direction: direction,
358366
};
359367
setValue(state.value);

packages/react/src/components/NumberInput/__tests__/NumberInput-test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,45 @@ describe('NumberInput', () => {
332332
translateWithId={translateWithId}
333333
/>
334334
);
335-
336335
expect(screen.getByLabelText('test-label')).toHaveValue(5);
337-
338336
await userEvent.click(screen.getByLabelText('decrement'));
339337
expect(screen.getByLabelText('test-label')).toHaveValue(0);
340338
});
341339
});
340+
it('should increase by the value of large step', async () => {
341+
render(
342+
<NumberInput
343+
label="test-label"
344+
id="test"
345+
min={-9999}
346+
value={1000}
347+
max={10000}
348+
step={1000}
349+
translateWithId={translateWithId}
350+
/>
351+
);
352+
expect(screen.getByLabelText('test-label')).toHaveValue(1000);
353+
await userEvent.click(screen.getByLabelText('increment'));
354+
expect(screen.getByLabelText('test-label')).toHaveValue(2000);
355+
});
356+
it('should decrease by the value of large step', async () => {
357+
render(
358+
<NumberInput
359+
label="test-label"
360+
id="test"
361+
min={-9999}
362+
value={1000}
363+
max={10000}
364+
step={1000}
365+
translateWithId={translateWithId}
366+
/>
367+
);
368+
369+
expect(screen.getByLabelText('test-label')).toHaveValue(1000);
370+
371+
await userEvent.click(screen.getByLabelText('decrement'));
372+
expect(screen.getByLabelText('test-label')).toHaveValue(0);
373+
});
342374

343375
it('should respect readOnly prop', async () => {
344376
const onChange = jest.fn();

0 commit comments

Comments
 (0)