Skip to content

Commit 6e1d4a2

Browse files
committed
fix(a11y): return PhoneInput focus to the number field after picking a country
Picking a country used to focus the flag trigger, but typing the number is the next step - main focused the tel input and so should the picker. The hook gains restoreFocusOnSelect (default true) since its internal Enter path calls select() too and a per-call argument could not reach it; PhoneInput opts out and focuses the number field in onSelect. Escape still returns focus to the country trigger. Caught in review on #44.
1 parent fd5f23a commit 6e1d4a2

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎content/docs/components/inputs/phone-input.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ announced through `aria-activedescendant`.
193193
| `ArrowDown` / `ArrowUp` (closed) | Open with the first / last country highlighted |
194194
| `ArrowDown` / `ArrowUp` | Move the highlight, wrapping at both ends |
195195
| `Home` / `End` | Highlight the first / last country |
196-
| `Enter` | Select the highlighted country and return focus to the flag trigger |
196+
| `Enter` | Select the highlighted country; focus moves to the number field |
197197
| `Escape` | Close the menu and return focus to the flag trigger |
198198
| Typing | Filter by name, code, or dial code; the highlight restarts at the top |
199199

‎src/lib/use-listbox-navigation.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export interface UseListboxNavigationOptions {
3737
initialIndex?: number;
3838
/** Defaults to `"activedescendant"`. */
3939
strategy?: ListboxStrategy;
40+
/**
41+
* Whether picking an option returns focus to the trigger (default: true).
42+
* Opt out when the component moves focus elsewhere itself after a pick —
43+
* `PhoneInput` focuses its number field, since typing the number is the next
44+
* step. Escape still returns focus to the trigger.
45+
*/
46+
restoreFocusOnSelect?: boolean;
4047
/**
4148
* The element that opens the popup. Read through a getter because a component
4249
* may own more than one trigger (the NavBar's desktop and mobile bars).
@@ -72,7 +79,11 @@ export interface ListboxNavigation {
7279
optionProps: (index: number) => ListboxOptionProps;
7380
/** Attach to an ancestor of both the trigger and the options. */
7481
onKeyDown: (event: React.KeyboardEvent) => void;
75-
/** Highlights an option. The component's option `onClick` should call it. */
82+
/**
83+
* Highlights an option. The component's option `onClick` should call it.
84+
* Closes the popup and — unless `restoreFocusOnSelect` is false — returns
85+
* focus to the trigger.
86+
*/
7687
select: (index: number) => void;
7788
/** Restarts the highlight at the top — call it whenever a filter changes. */
7889
resetActive: () => void;
@@ -236,7 +247,10 @@ export function useListboxNavigation(options: UseListboxNavigationOptions): List
236247
const current = optionsRef.current;
237248
if (index < 0 || index >= current.count) return;
238249
current.onSelect?.(index);
239-
close(true);
250+
// A component that moves focus itself after a pick (PhoneInput focuses
251+
// its number field) opts out of the restore; Escape still returns to the
252+
// trigger through `close`.
253+
close(current.restoreFocusOnSelect ?? true);
240254
},
241255
[close],
242256
);

‎src/registry/components/phone-input.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ export const PhoneInput = React.forwardRef<HTMLInputElement, PhoneInputProps>(fu
142142
setSearch("");
143143
},
144144
onOpen: () => setOpen(true),
145+
// Picking a country is half the task — typing the number is the other
146+
// half, so focus lands in the number field instead of back on the trigger.
147+
restoreFocusOnSelect: false,
145148
onSelect: (index) => {
146149
const picked = filtered[index];
147150
if (!picked) return;
148151
setSelectedCountry(picked);
149152
onValueChange?.(inputValue, picked);
153+
inputRef.current?.focus();
150154
},
151155
triggerRef: () => triggerRef.current,
152156
});

‎test/phone-input.test.tsx‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ describe("PhoneInput", () => {
7373
expect(ref.current?.type).toBe("tel");
7474
});
7575

76-
it("returns focus to the country trigger after a country is picked", async () => {
76+
it("focuses the number field after a country is picked by click", async () => {
7777
render(<PhoneInput />);
78-
const trigger = screen.getByRole("button", { name: "Select country" });
79-
await userEvent.click(trigger);
78+
await userEvent.click(screen.getByRole("button", { name: "Select country" }));
8079
await userEvent.click(screen.getByRole("option", { name: /India/ }));
8180

82-
expect(document.activeElement).toBe(trigger);
81+
expect(screen.queryByRole("listbox")).toBeNull();
82+
expect(document.activeElement).toBe(screen.getByRole("textbox", { name: "Phone number" }));
8383
});
8484

8585
it("focuses the search box when the country list opens", async () => {
@@ -164,18 +164,17 @@ describe("PhoneInput keyboard navigation", () => {
164164
expect(activeOptionId()).toBe(screen.getByRole("option", { name: /United States/ }).id);
165165
});
166166

167-
it("selects the active country with Enter, closes, and returns focus to the trigger", async () => {
167+
it("selects the active country with Enter, closes, and focuses the number field", async () => {
168168
const onValueChange = vi.fn();
169169
render(<PhoneInput onValueChange={onValueChange} />);
170-
const trigger = screen.getByRole("button", { name: "Select country" });
171-
await userEvent.click(trigger);
170+
await userEvent.click(screen.getByRole("button", { name: "Select country" }));
172171

173172
await userEvent.keyboard("{ArrowDown}{Enter}");
174173

175174
expect(onValueChange).toHaveBeenCalledTimes(1);
176175
expect(onValueChange).toHaveBeenCalledWith("", expect.objectContaining({ code: "GB" }));
177176
expect(screen.queryByRole("listbox")).toBeNull();
178-
expect(document.activeElement).toBe(trigger);
177+
expect(document.activeElement).toBe(screen.getByRole("textbox", { name: "Phone number" }));
179178
});
180179

181180
it("closes on Escape and returns focus to the trigger", async () => {

0 commit comments

Comments
 (0)