Skip to content

Commit 6282569

Browse files
committed
fix(a11y): keep PhoneInput number-field keys out of the open country menu
The click-outside handler only dismisses on clicks outside the container, so focusing the still-visible number field while the menu is open let the listbox swallow its keys: arrows and Home / End were preventDefault-ed (caret movement stolen) and Enter selected the highlighted country. The container handler now skips those five keys when the target is the number field and the menu is open; Escape and everything else still reach the listbox. Caught by CodeRabbit on #44.
1 parent 6e1d4a2 commit 6282569

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,29 @@ export const PhoneInput = React.forwardRef<HTMLInputElement, PhoneInputProps>(fu
176176
onValueChange?.(newValue, selectedCountry);
177177
};
178178

179+
const handleKeyDown = (event: React.KeyboardEvent) => {
180+
// While the menu is open the number field is still focusable: arrows and
181+
// Home / End belong to the caret there, and Enter is not a request to pick
182+
// a country. Escape and everything else keep going to the listbox.
183+
if (
184+
open &&
185+
event.target === inputRef.current &&
186+
(event.key === "ArrowUp" ||
187+
event.key === "ArrowDown" ||
188+
event.key === "Home" ||
189+
event.key === "End" ||
190+
event.key === "Enter")
191+
) {
192+
return;
193+
}
194+
nav.onKeyDown(event);
195+
};
196+
179197
return (
180198
<div
181199
ref={ref}
182200
className={cn("relative inline-block text-sm", className)}
183-
onKeyDown={nav.onKeyDown}
201+
onKeyDown={handleKeyDown}
184202
>
185203
<div
186204
className={cn(

‎test/phone-input.test.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ describe("PhoneInput keyboard navigation", () => {
188188
expect(document.activeElement).toBe(trigger);
189189
});
190190

191+
it("keeps number-field keys out of the open country menu", async () => {
192+
const onValueChange = vi.fn();
193+
render(<PhoneInput onValueChange={onValueChange} />);
194+
const trigger = screen.getByRole("button", { name: "Select country" });
195+
const numberField = screen.getByRole("textbox", { name: "Phone number" });
196+
await userEvent.click(trigger);
197+
// The number field stays visible and focusable inside the container, so a
198+
// click there does not dismiss the menu.
199+
await userEvent.click(numberField);
200+
expect(screen.getByRole("listbox")).toBeInTheDocument();
201+
202+
// Arrows, Home / End, and Enter belong to the field, not the country list.
203+
await userEvent.keyboard("{ArrowDown}{End}{Enter}");
204+
expect(screen.getByRole("listbox")).toBeInTheDocument();
205+
expect(onValueChange).not.toHaveBeenCalled();
206+
207+
// Escape still closes the menu and returns to the trigger.
208+
await userEvent.keyboard("{Escape}");
209+
expect(screen.queryByRole("listbox")).toBeNull();
210+
expect(document.activeElement).toBe(trigger);
211+
});
212+
191213
it("restarts the highlight at the top when the filter changes", async () => {
192214
render(<PhoneInput />);
193215
await userEvent.click(screen.getByRole("button", { name: "Select country" }));

0 commit comments

Comments
 (0)