Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(NumberInput): keep stepped values within fractional bounds (#5510)

Jiun Shinbe5278b0eefd
diff --git a/.changeset/numberinput-stepping-clamps-the-rounded-result-s-18rm.md b/.changeset/numberinput-stepping-clamps-the-rounded-result-s-18rm.md
new file mode 100644
index 0000000000..59be2acbea
--- /dev/null
+++ b/.changeset/numberinput-stepping-clamps-the-rounded-result-s-18rm.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] NumberInput stepping clamps the rounded result so fractional min and max bounds cannot be crossed (#5506)
+@jiunshinn
diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsx
index 1d4de5d1ec..e1e72d1062 100644
--- a/packages/core/src/NumberInput/NumberInput.test.tsx
+++ b/packages/core/src/NumberInput/NumberInput.test.tsx
@@ -10,7 +10,7 @@
*/
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
-import {useState} from 'react';
+import {act, useState} from 'react';
import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {TestIcon} from '../__tests__/TestIcon';
@@ -1760,6 +1760,34 @@ describe('NumberInput statusVariant forwarding', () => {
});
describe('NumberInput stepping', () => {
+ const stepInteractions = [
+ [
+ 'keyboard',
+ (input: HTMLElement, direction: 'up' | 'down') =>
+ fireEvent.keyDown(input, {
+ key: direction === 'up' ? 'ArrowUp' : 'ArrowDown',
+ }),
+ ],
+ [
+ 'wheel',
+ (input: HTMLElement, direction: 'up' | 'down') => {
+ input.focus();
+ act(() => {
+ fireEvent.wheel(input, {deltaY: direction === 'up' ? -100 : 100});
+ });
+ },
+ ],
+ [
+ 'number stepper',
+ (_input: HTMLElement, direction: 'up' | 'down') =>
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: direction === 'up' ? 'Increment Amount' : 'Decrement Amount',
+ }),
+ ),
+ ],
+ ] as const;
+
it('increments with ArrowUp and decrements with ArrowDown', () => {
const onChange = vi.fn();
render(<NumberInput label="Amount" value={5} onChange={onChange} />);
@@ -1894,6 +1922,76 @@ describe('NumberInput stepping', () => {
expect(onChange).not.toHaveBeenCalled();
});
+ it.each(stepInteractions)(
+ 'clamps at a fractional max after rounding via the %s',
+ (_name, interact) => {
+ const onChange = vi.fn();
+ render(
+ <NumberInput
+ label="Amount"
+ value={99}
+ onChange={onChange}
+ max={99.99}
+ hasNumberSteppers
+ />,
+ );
+
+ interact(screen.getByRole('spinbutton'), 'up');
+
+ expect(onChange).toHaveBeenCalledWith(99.99);
+ expect(onChange).not.toHaveBeenCalledWith(100);
+ },
+ );
+
+ it.each(stepInteractions)(
+ 'clamps at a fractional min after rounding via the %s',
+ (_name, interact) => {
+ const onChange = vi.fn();
+ const min = 4e-13;
+ render(
+ <NumberInput
+ label="Amount"
+ value={0.5}
+ onChange={onChange}
+ min={min}
+ hasNumberSteppers
+ />,
+ );
+
+ interact(screen.getByRole('spinbutton'), 'down');
+
+ expect(onChange).toHaveBeenCalledWith(min);
+ expect(onChange).not.toHaveBeenCalledWith(0);
+ },
+ );
+
+ it('reflects a reached fractional max in the spinbutton and stepper state', () => {
+ function ControlledNumberInput() {
+ const [value, setValue] = useState(99);
+ return (
+ <NumberInput
+ label="Amount"
+ value={value}
+ onChange={setValue}
+ max={99.99}
+ hasNumberSteppers
+ />
+ );
+ }
+ render(<ControlledNumberInput />);
+
+ const input = screen.getByRole('spinbutton');
+ const increment = screen.getByRole('button', {name: 'Increment Amount'});
+ expect(increment).toBeEnabled();
+
+ fireEvent.click(increment);
+
+ expect(input).toHaveValue('99.99');
+ expect(input).toHaveAttribute('aria-valuenow', '99.99');
+ expect(input).not.toHaveAttribute('aria-invalid');
+ expect(increment).toBeDisabled();
+ });
+
it('allows wheel stepping by default and consumes the focused gesture', () => {
const onScrollableWheel = vi.fn();
const onChange = vi.fn();
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index 7d9a3f8261..e8c3cd6a97 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -491,6 +491,14 @@ function getSteppedValue({
nextValue = stepBase + nextStepPosition * effectiveStep;
}
+ const precision = Math.min(
+ 12,
+ Math.max(getDecimalPlaces(effectiveStep), getDecimalPlaces(stepBase)),
+ );
+ nextValue = Number(nextValue.toFixed(precision));
+
+ // Clamp after rounding so a bound with finer precision than the step cannot
+ // be rounded back out of its own range.
if (min != null) {
nextValue = Math.max(min, nextValue);
}
@@ -501,16 +509,10 @@ function getSteppedValue({
if (!Number.isFinite(nextValue)) {
return currentValue;
}
-
- const precision = Math.min(
- 12,
- Math.max(getDecimalPlaces(effectiveStep), getDecimalPlaces(stepBase)),
- );
- const roundedValue = Number(nextValue.toFixed(precision));
- if (isIntegerOnly && !Number.isInteger(roundedValue)) {
+ if (isIntegerOnly && !Number.isInteger(nextValue)) {
return currentValue;
}
- return Object.is(roundedValue, -0) ? 0 : roundedValue;
+ return Object.is(nextValue, -0) ? 0 : nextValue;
}
/**