merge: 🩹 the picker opens on a click anywhere on the field
Browse files at this commit2 changed files +50 -0
src/components/ui/searchable-select.test.tsxmodified+45 -0
| @@ -19,6 +19,51 @@ function renderSelect(onChange = vi.fn()) { | ||
| 19 | 19 | return { input: screen.getByRole("combobox"), onChange }; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | +it("opens the option list on a plain click on the field", async () => { | |
| 23 | + const { input } = renderSelect(); | |
| 24 | + | |
| 25 | + // No typing, no chevron: the field itself is the affordance. | |
| 26 | + fireEvent.click(input); | |
| 27 | + | |
| 28 | + await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy()); | |
| 29 | + expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy(); | |
| 30 | +}); | |
| 31 | + | |
| 32 | +it("reports the picked value when an option is clicked", async () => { | |
| 33 | + const { input, onChange } = renderSelect(); | |
| 34 | + | |
| 35 | + fireEvent.click(input); | |
| 36 | + fireEvent.click(await screen.findByRole("option", { name: "10-B · Türkçe" })); | |
| 37 | + | |
| 38 | + await waitFor(() => expect(onChange).toHaveBeenCalledWith("t")); | |
| 39 | +}); | |
| 40 | + | |
| 41 | +it("closes on Escape without changing the value", async () => { | |
| 42 | + const { input, onChange } = renderSelect(); | |
| 43 | + | |
| 44 | + fireEvent.click(input); | |
| 45 | + await screen.findByRole("option", { name: "9-A · Matematik" }); | |
| 46 | + | |
| 47 | + fireEvent.keyDown(input, { key: "Escape" }); | |
| 48 | + | |
| 49 | + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); | |
| 50 | + expect(onChange).not.toHaveBeenCalled(); | |
| 51 | +}); | |
| 52 | + | |
| 53 | +it("closes the list after a pick and keeps the chevron working", async () => { | |
| 54 | + const { onChange } = renderSelect(); | |
| 55 | + | |
| 56 | + // Opened from the chevron, not the field: the trigger must still open. | |
| 57 | + // Kobalte toggles on pointerdown (a bare click event never reaches it). | |
| 58 | + const trigger = screen.getByRole("button"); | |
| 59 | + fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 }); | |
| 60 | + fireEvent.click(trigger); | |
| 61 | + fireEvent.click(await screen.findByRole("option", { name: "9-A · Matematik" })); | |
| 62 | + | |
| 63 | + await waitFor(() => expect(screen.queryByRole("option")).toBeNull()); | |
| 64 | + expect(onChange).toHaveBeenCalledWith("m"); | |
| 65 | +}); | |
| 66 | + | |
| 22 | 67 | it("finds an option across a dash and word order", async () => { |
| 23 | 68 | const { input } = renderSelect(); |
| 24 | 69 |
src/components/ui/searchable-select.tsxmodified+5 -0
| @@ -50,6 +50,11 @@ export function SearchableSelect(props: { | ||
| 50 | 50 | onChange={(option) => props.onChange(option?.value ?? "")} |
| 51 | 51 | onInputChange={setQuery} |
| 52 | 52 | defaultFilter={filter} |
| 53 | + // Kobalte's default triggerMode "input" opens on typing or ArrowDown but | |
| 54 | + // ignores a click on the field itself, so the box only reacted to the | |
| 55 | + // chevron. "focus" makes the field the affordance: a plain click opens | |
| 56 | + // the whole list (focus does too), typing still filters. | |
| 57 | + triggerMode="focus" | |
| 53 | 58 | // Without this Kobalte closes the popover when its filter keeps nothing, |
| 54 | 59 | // so the "no results" line below would never get a chance to render. |
| 55 | 60 | allowsEmptyCollection |