From 2ec311021e3c8914b4a0233bf34655bb0376a429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 18 Sep 2026 06:43:51 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A9=B9=20the=20note=20picker=20ope?= =?UTF-8?q?ns,=20toggles=20and=20closes=20on=20a=20field=20click?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Kobalte combobox behind SearchableSelect opened only from the chevron or on typing: its default triggerMode ("input") ignores a click on the field, and focus mode — which opens on click — can never close again, refocusing the input after every close so the list reopens by itself. The field now owns the open state (controlled open/onOpenChange plus an onClick toggle on the input): click opens, click again closes, a pick leaves it closed, outside-click and Escape still close, typing still filters, ArrowDown opens and the chevron still toggles. --- src/components/ui/searchable-select.test.tsx | 52 +++++++++++++++++--- src/components/ui/searchable-select.tsx | 22 ++++++--- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/components/ui/searchable-select.test.tsx b/src/components/ui/searchable-select.test.tsx index 0ff5686..5fc45ec 100644 --- a/src/components/ui/searchable-select.test.tsx +++ b/src/components/ui/searchable-select.test.tsx @@ -19,7 +19,7 @@ function renderSelect(onChange = vi.fn()) { return { input: screen.getByRole("combobox"), onChange }; } -it("opens the option list on a plain click on the field", async () => { +it("1. opens the option list on a plain click on the field", async () => { const { input } = renderSelect(); // No typing, no chevron: the field itself is the affordance. @@ -29,16 +29,45 @@ it("opens the option list on a plain click on the field", async () => { expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy(); }); -it("reports the picked value when an option is clicked", async () => { +it("2. closes again when the open field is clicked", async () => { + const { input } = renderSelect(); + + fireEvent.click(input); + await screen.findByRole("option", { name: "9-A · Matematik" }); + + fireEvent.click(input); + + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); +}); + +it("3. reports the picked value and stays closed after the pick", async () => { const { input, onChange } = renderSelect(); fireEvent.click(input); fireEvent.click(await screen.findByRole("option", { name: "10-B · Türkçe" })); await waitFor(() => expect(onChange).toHaveBeenCalledWith("t")); + // The close → refocus dance must not re-open the list by itself. + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); +}); + +it("4. closes when clicking outside the field", async () => { + const { input } = renderSelect(); + + fireEvent.click(input); + await screen.findByRole("option", { name: "9-A · Matematik" }); + + // Kobalte attaches its document-level outside listener one macrotask after + // it opens; a real click always lands long after that. + const tick = Promise.withResolvers(); + setTimeout(tick.resolve, 0); + await tick.promise; + fireEvent.pointerDown(document.body); + + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); }); -it("closes on Escape without changing the value", async () => { +it("5. closes on Escape without changing the value", async () => { const { input, onChange } = renderSelect(); fireEvent.click(input); @@ -50,18 +79,27 @@ it("closes on Escape without changing the value", async () => { expect(onChange).not.toHaveBeenCalled(); }); -it("closes the list after a pick and keeps the chevron working", async () => { +it("6. opens on ArrowDown from the closed field", async () => { + const { input } = renderSelect(); + + fireEvent.keyDown(input, { key: "ArrowDown" }); + + await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy()); +}); + +it("7. chevron toggles the list open and closed", async () => { const { onChange } = renderSelect(); // Opened from the chevron, not the field: the trigger must still open. // Kobalte toggles on pointerdown (a bare click event never reaches it). const trigger = screen.getByRole("button"); fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 }); - fireEvent.click(trigger); - fireEvent.click(await screen.findByRole("option", { name: "9-A · Matematik" })); + await screen.findByRole("option", { name: "9-A · Matematik" }); + + fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 }); await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); - expect(onChange).toHaveBeenCalledWith("m"); + expect(onChange).not.toHaveBeenCalled(); }); it("finds an option across a dash and word order", async () => { diff --git a/src/components/ui/searchable-select.tsx b/src/components/ui/searchable-select.tsx index 97252c9..ed05818 100644 --- a/src/components/ui/searchable-select.tsx +++ b/src/components/ui/searchable-select.tsx @@ -33,6 +33,7 @@ export function SearchableSelect(props: { }) { const t = useT(); const [query, setQuery] = createSignal(""); + const [open, setOpen] = createSignal(false); const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null); const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label); // Kobalte renders nothing when its filter keeps no option, which reads as a @@ -46,15 +47,18 @@ export function SearchableSelect(props: { return ( options={props.options} + // Open state is ours: Kobalte's click handler only ever OPENS (and only + // when triggerMode is "focus"), so with it a field click could not close + // the list again and a pick re-opened it on the focus-back. Owning the + // signal makes the field a real toggle: click opens, click again closes, + // picking an option leaves it closed. Everything else (typing, ArrowDown, + // Escape, the chevron, click-outside) still drives this same signal. + open={open()} + onOpenChange={setOpen} value={selected()} onChange={(option) => props.onChange(option?.value ?? "")} onInputChange={setQuery} defaultFilter={filter} - // Kobalte's default triggerMode "input" opens on typing or ArrowDown but - // ignores a click on the field itself, so the box only reacted to the - // chevron. "focus" makes the field the affordance: a plain click opens - // the whole list (focus does too), typing still filters. - triggerMode="focus" // Without this Kobalte closes the popover when its filter keeps nothing, // so the "no results" line below would never get a chance to render. allowsEmptyCollection @@ -72,7 +76,13 @@ export function SearchableSelect(props: { )} > - + setOpen((isOpen) => !isOpen)} + />