diff --git a/src/components/ui/searchable-select.test.tsx b/src/components/ui/searchable-select.test.tsx index b5a5114..0ff5686 100644 --- a/src/components/ui/searchable-select.test.tsx +++ b/src/components/ui/searchable-select.test.tsx @@ -19,6 +19,51 @@ function renderSelect(onChange = vi.fn()) { return { input: screen.getByRole("combobox"), onChange }; } +it("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. + fireEvent.click(input); + + await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy()); + expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy(); +}); + +it("reports the picked value when an option is clicked", 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")); +}); + +it("closes on Escape without changing the value", async () => { + const { input, onChange } = renderSelect(); + + fireEvent.click(input); + await screen.findByRole("option", { name: "9-A · Matematik" }); + + fireEvent.keyDown(input, { key: "Escape" }); + + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); + expect(onChange).not.toHaveBeenCalled(); +}); + +it("closes the list after a pick and keeps the chevron working", 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 waitFor(() => expect(screen.queryByRole("option")).toBeNull()); + expect(onChange).toHaveBeenCalledWith("m"); +}); + it("finds an option across a dash and word order", async () => { const { input } = renderSelect(); diff --git a/src/components/ui/searchable-select.tsx b/src/components/ui/searchable-select.tsx index 184ad40..97252c9 100644 --- a/src/components/ui/searchable-select.tsx +++ b/src/components/ui/searchable-select.tsx @@ -50,6 +50,11 @@ export function SearchableSelect(props: { 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