Skip to content

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

TypeScript

fix(NumberInput): keep Tab's select-all when formatValue swaps the text (#9033)

Ojas Mutrejad7ecbcb48e03
diff --git a/.changeset/numberinput-tab-keeps-select-all.md b/.changeset/numberinput-tab-keeps-select-all.md
new file mode 100644
index 0000000000..88ff33eb57
--- /dev/null
+++ b/.changeset/numberinput-tab-keeps-select-all.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] NumberInput: with `formatValue`, tabbing into the field keeps its text selected, so typing replaces the value. Before, the editable text replaced the formatted text after focus and collapsed Tab's selection to the end, so with `1,500,000` shown, Tab then `7` gave `15000007` and committed it on blur. Now `1,500,000` becomes a fully selected `1500000` and `7` replaces it. A click still places the caret where it landed. Fields without `formatValue` are unchanged. No API changes.
+
+@HelloOjasMutreja
diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsx
index 64dd2b6127..1630864ff0 100644
--- a/packages/core/src/NumberInput/NumberInput.test.tsx
+++ b/packages/core/src/NumberInput/NumberInput.test.tsx
@@ -358,6 +358,67 @@ describe('NumberInput', () => {
expect(input).toHaveAttribute('aria-valuenow', '1234');
});
+ it('keeps the whole text selected when focus arrives with it selected, as Tab does', () => {
+ render(
+ <NumberInput
+ label="Amount"
+ value={1500000}
+ onChange={() => {}}
+ formatValue={number => number.toLocaleString('en-US')}
+ />,
+ );
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
+ expect(input).toHaveValue('1,500,000');
+
+ // Tab into a text field selects its text before the focus event.
+ input.focus();
+ input.setSelectionRange(0, input.value.length);
+ fireEvent.focus(input);
+
+ // The editable text replaces the formatted text, and stays selected.
+ expect(input).toHaveValue('1500000');
+ expect(input.selectionStart).toBe(0);
+ expect(input.selectionEnd).toBe('1500000'.length);
+ });
+
+ it('does not swallow keystrokes when focus swaps no text', async () => {
+ const user = userEvent.setup();
+ function Controlled() {
+ const [value, setValue] = useState<number | null>(1);
+ return <NumberInput label="Amount" value={value} onChange={setValue} />;
+ }
+ render(
+ <>
+ <button type="button">Before</button>
+ <Controlled />
+ </>,
+ );
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
+ // Tab into the field with its text selected, as a browser does. The
+ // text is the same at rest and focused, so nothing is swapped.
+ input.focus();
+ input.setSelectionRange(0, input.value.length);
+ fireEvent.focus(input);
+ await user.keyboard('75');
+ expect(input).toHaveValue('75');
+ });
+
+ it('leaves the caret where a click put it', () => {
+ render(
+ <NumberInput
+ label="Amount"
+ value={1500000}
+ onChange={() => {}}
+ formatValue={number => number.toLocaleString('en-US')}
+ />,
+ );
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
+ input.focus();
+ input.setSelectionRange(3, 3);
+ fireEvent.focus(input);
+ expect(input.selectionStart).toBe(input.selectionEnd);
+ });
+
it('keeps ARIA value text on the committed value while an edit is pending', () => {
function ControlledNumberInput() {
const [controlledValue, setControlledValue] = useState(1234);
@@ -1505,7 +1566,7 @@ describe('NumberInput', () => {
isReadOnly
/>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
await user.keyboard('{ArrowUp}{ArrowDown}');
expect(handleChange).not.toHaveBeenCalled();
@@ -1525,7 +1586,7 @@ describe('NumberInput', () => {
/>
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
const event = new WheelEvent('wheel', {
bubbles: true,
@@ -2174,7 +2235,7 @@ describe('NumberInput stepping', () => {
<NumberInput label="Amount" value={5} onChange={onChange} />
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
const event = new WheelEvent('wheel', {
bubbles: true,
@@ -2201,7 +2262,7 @@ describe('NumberInput stepping', () => {
/>
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
const event = new WheelEvent('wheel', {
bubbles: true,
@@ -2228,7 +2289,7 @@ describe('NumberInput stepping', () => {
/>
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
rerender(
@@ -2290,7 +2351,7 @@ describe('NumberInput stepping', () => {
<NumberInput label="Amount" value={5} onChange={onChange} />
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
fireEvent.wheel(input, {deltaY: -100, ctrlKey: true});
@@ -2312,7 +2373,7 @@ describe('NumberInput stepping', () => {
/>
</div>,
);
- const input = screen.getByRole('spinbutton');
+ const input = screen.getByRole<HTMLInputElement>('spinbutton');
input.focus();
fireEvent.wheel(input, {deltaY: -100});
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index 0c648c36cf..81e208dc0b 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -24,6 +24,7 @@ import {
useState,
useMemo,
useCallback,
+ useLayoutEffect,
useRef,
type FocusEvent,
type KeyboardEvent,
@@ -718,14 +719,43 @@ export function NumberInput({
);
// Handle focus
+ // Tab into the field selects its text so typing replaces it. With
+ // `formatValue` the text at rest differs from the editable text, and putting
+ // the editable text in the input after focus collapses that selection to the
+ // end, so typing would append. When focus arrives with the text selected,
+ // select the swapped-in text too.
+ const reselectOnFocusRef = useRef(false);
+
const handleFocus = useCallback(
(e: FocusEvent<HTMLInputElement>) => {
+ const input = e.currentTarget;
+ // Only when focusing swaps the text: otherwise no render follows to
+ // consume the flag, and the first keystroke would.
+ const editableText =
+ pendingInput ??
+ (value == null ? '' : formatEditableNumber(value, locale));
+ reselectOnFocusRef.current =
+ input.value.length > 0 &&
+ input.value !== editableText &&
+ input.selectionStart === 0 &&
+ input.selectionEnd === input.value.length;
setIsFocused(true);
onFocus?.(e);
},
- [onFocus],
+ [onFocus, pendingInput, value, locale],
);
+ useLayoutEffect(() => {
+ if (!reselectOnFocusRef.current) {
+ return;
+ }
+ reselectOnFocusRef.current = false;
+ const input = inputRef.current;
+ if (input != null && input === document.activeElement) {
+ input.setSelectionRange(0, input.value.length);
+ }
+ }, [displayValue]);
+
const commitPendingInput = useCallback(
(trigger: 'blur' | 'Enter') => {
if (pendingInput === null) {