Skip to content

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

TypeScript

fix(TextInput,NumberInput): tapping the clear button brings up the iOS keyboard (#8183)

Christopher Chedeaucd53c2dbf0ee
diff --git a/.changeset/clear-button-keeps-the-keyboard.md b/.changeset/clear-button-keeps-the-keyboard.md
new file mode 100644
index 0000000000..b45411f0cd
--- /dev/null
+++ b/.changeset/clear-button-keeps-the-keyboard.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Tapping the clear button of a TextInput or NumberInput that is not focused now brings up the iOS keyboard. The field took focus a frame after the tap, and iOS Safari only raises its keyboard for a focus made while handling the touch, so the field ended up focused with no keyboard. The focus now happens inside the tap, still with `preventScroll`, so a scrolled page stays put.
+
+@vjeux
diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsx
index 0badae9747..1f28890ff0 100644
--- a/packages/core/src/NumberInput/NumberInput.test.tsx
+++ b/packages/core/src/NumberInput/NumberInput.test.tsx
@@ -1569,6 +1569,24 @@ describe('NumberInput', () => {
).toBeInTheDocument();
});
+ it('focuses the field inside the tap that clears it, without scrolling', () => {
+ // iOS Safari brings up its keyboard only for a focus made while
+ // handling the touch.
+ const onChange = vi.fn();
+ const focusSpy = vi.spyOn(HTMLElement.prototype, 'focus');
+ render(
+ <NumberInput label="Qty" value={5} onChange={onChange} hasClear />,
+ );
+ const input = screen.getByRole('spinbutton');
+ fireEvent.click(screen.getByRole('button', {name: 'Clear Qty'}), {
+ detail: 1,
+ });
+ expect(onChange).toHaveBeenCalledWith(null);
+ expect(document.activeElement).toBe(input);
+ expect(focusSpy).toHaveBeenLastCalledWith({preventScroll: true});
+ focusSpy.mockRestore();
+ });
+
it('does not show clear button when value is null', () => {
render(
<NumberInput label="Qty" value={null} onChange={() => {}} hasClear />,
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index e3e8fc0f9f..49d40b1710 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -892,11 +892,12 @@ export function NumberInput({
if (!e || e.detail === 0) {
inputRef.current?.focus();
} else {
- // Defer focus restoration past the button's unmount task so iOS Safari
- // and touch browsers don't jump the page scroll to 0 on tap.
- requestAnimationFrame(() => {
- inputRef.current?.focus({preventScroll: true});
- });
+ // Focus inside the tap: iOS Safari brings up its keyboard only for a
+ // focus made while handling the touch, so a field the tap reaches
+ // unfocused gets its keyboard, as UIKit's clear button makes the
+ // field first responder. `preventScroll` keeps a scrolled page where
+ // it is.
+ inputRef.current?.focus({preventScroll: true});
}
},
[hasClear, onChange],
diff --git a/packages/core/src/TextInput/TextInput.test.tsx b/packages/core/src/TextInput/TextInput.test.tsx
index 69acce3ec2..7bc939bbc6 100644
--- a/packages/core/src/TextInput/TextInput.test.tsx
+++ b/packages/core/src/TextInput/TextInput.test.tsx
@@ -1159,8 +1159,12 @@ describe('TextInput clear button focus behavior', () => {
expect(document.activeElement).toBe(input);
});
- it('defers focus restoration via requestAnimationFrame on pointer activation', () => {
+ it('focuses the input inside the tap on pointer activation, without scrolling', () => {
+ // iOS Safari brings up its keyboard only for a focus made while handling
+ // the touch: a deferred focus would leave an unfocused field focused but
+ // keyboardless.
const handleChange = vi.fn();
+ const focusSpy = vi.spyOn(HTMLElement.prototype, 'focus');
const rafSpy = vi.spyOn(window, 'requestAnimationFrame');
render(
<TextInput
@@ -1170,14 +1174,17 @@ describe('TextInput clear button focus behavior', () => {
onChange={handleChange}
/>,
);
-
+ const input = screen.getByRole('textbox');
const clearButton = screen.getByRole('button', {name: /clear/i});
+ expect(document.activeElement).not.toBe(input);
- // Pointer activation (detail > 0)
fireEvent.click(clearButton, {detail: 1});
expect(handleChange).toHaveBeenCalledWith('', expect.any(Object));
- expect(rafSpy).toHaveBeenCalled();
+ expect(document.activeElement).toBe(input);
+ expect(focusSpy).toHaveBeenLastCalledWith({preventScroll: true});
+ expect(rafSpy).not.toHaveBeenCalled();
+ focusSpy.mockRestore();
rafSpy.mockRestore();
});
});
diff --git a/packages/core/src/TextInput/TextInput.tsx b/packages/core/src/TextInput/TextInput.tsx
index 75f858daba..9656e12021 100644
--- a/packages/core/src/TextInput/TextInput.tsx
+++ b/packages/core/src/TextInput/TextInput.tsx
@@ -422,11 +422,12 @@ export function TextInput({
if (!e || e.detail === 0) {
inputRef.current?.focus();
} else {
- // Defer focus restoration past the button's unmount task so iOS Safari
- // and touch browsers don't jump the page scroll to 0 on tap.
- requestAnimationFrame(() => {
- inputRef.current?.focus({preventScroll: true});
- });
+ // Focus inside the tap: iOS Safari brings up its keyboard only for a
+ // focus made while handling the touch, so a field the tap reaches
+ // unfocused gets its keyboard, as UIKit's clear button makes the
+ // field first responder. `preventScroll` keeps a scrolled page where
+ // it is.
+ inputRef.current?.focus({preventScroll: true});
}
},
[onChange],