🔎 clear searches when reopening pickers
Browse files at this commit6e2339eb2858af9ca80dfebcbf8d87e557a1c316Parent: 0b3228b- fix
- 🔎 clear searches when reopening pickers
4 changed files +151 -6
- src/components/ui/searchable-select.test.tsx +30 -0
- src/components/ui/searchable-select.tsx +12 -2
- src/components/users/user-search-select.test.tsx +82 -0
- src/components/users/user-search-select.tsx +27 -4
src/components/ui/searchable-select.test.tsxmodified+30 -0
| @@ -1,4 +1,5 @@ | |||
| 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | +import { createSignal } from "solid-js"; | ||
| 2 | import { afterEach, expect, it, vi } from "vitest"; | 3 | import { afterEach, expect, it, vi } from "vitest"; |
| 3 | import { SearchableSelect } from "@/components/ui/searchable-select"; | 4 | import { SearchableSelect } from "@/components/ui/searchable-select"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; | 5 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| @@ -126,3 +127,32 @@ it("says so when the search matches nothing", async () => { | |||
| 126 | 127 | ||
| 127 | await waitFor(() => expect(screen.getByText("No results.")).toBeTruthy()); | 128 | await waitFor(() => expect(screen.getByText("No results.")).toBeTruthy()); |
| 128 | }); | 129 | }); |
| 130 | + | ||
| 131 | +it("clears the search on reopening while keeping the selected option", async () => { | ||
| 132 | + const [value, setValue] = createSignal("m"); | ||
| 133 | + const onChange = vi.fn((next: string) => setValue(next)); | ||
| 134 | + render(() => ( | ||
| 135 | + <PreferencesProvider> | ||
| 136 | + <SearchableSelect value={value()} onChange={onChange} options={OPTIONS} placeholder="Seç" /> | ||
| 137 | + </PreferencesProvider> | ||
| 138 | + )); | ||
| 139 | + const input = screen.getByRole("combobox") as HTMLInputElement; | ||
| 140 | + | ||
| 141 | + expect(input.value).toBe("9-A · Matematik"); | ||
| 142 | + fireEvent.click(input); | ||
| 143 | + expect(input.value).toBe(""); | ||
| 144 | + await screen.findByRole("option", { name: "10-B · Türkçe" }); | ||
| 145 | + | ||
| 146 | + fireEvent.input(input, { target: { value: "turkce" } }); | ||
| 147 | + await waitFor(() => expect(screen.queryByRole("option", { name: "9-A · Matematik" })).toBeNull()); | ||
| 148 | + fireEvent.click(screen.getByRole("option", { name: "10-B · Türkçe" })); | ||
| 149 | + await waitFor(() => expect(value()).toBe("t")); | ||
| 150 | + await waitFor(() => expect(input.value).toBe("10-B · Türkçe")); | ||
| 151 | + | ||
| 152 | + fireEvent.click(input); | ||
| 153 | + expect(input.value).toBe(""); | ||
| 154 | + await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy()); | ||
| 155 | + expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy(); | ||
| 156 | + expect(value()).toBe("t"); | ||
| 157 | + expect(onChange).toHaveBeenCalledTimes(1); | ||
| 158 | +}); | ||
src/components/ui/searchable-select.tsxmodified+12 -2
| @@ -34,8 +34,17 @@ export function SearchableSelect(props: { | |||
| 34 | const t = useT(); | 34 | const t = useT(); |
| 35 | const [query, setQuery] = createSignal(""); | 35 | const [query, setQuery] = createSignal(""); |
| 36 | const [open, setOpen] = createSignal(false); | 36 | const [open, setOpen] = createSignal(false); |
| 37 | + let inputRef: HTMLInputElement | undefined; | ||
| 37 | const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null); | 38 | const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null); |
| 38 | const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label); | 39 | const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label); |
| 40 | + const changeOpen = (nextOpen: boolean, fromTyping = false) => { | ||
| 41 | + setOpen(nextOpen); | ||
| 42 | + if (!nextOpen || fromTyping || !inputRef) return; | ||
| 43 | + // Kobalte owns the displayed input value. An input event clears its | ||
| 44 | + // internal filter as well as the field, without changing the selection. | ||
| 45 | + inputRef.value = ""; | ||
| 46 | + inputRef.dispatchEvent(new Event("input", { bubbles: true })); | ||
| 47 | + }; | ||
| 39 | // Kobalte renders nothing when its filter keeps no option, which reads as a | 48 | // Kobalte renders nothing when its filter keeps no option, which reads as a |
| 40 | // broken dropdown; this says the search came up empty instead. | 49 | // broken dropdown; this says the search came up empty instead. |
| 41 | const empty = createMemo(() => { | 50 | const empty = createMemo(() => { |
| @@ -54,7 +63,7 @@ export function SearchableSelect(props: { | |||
| 54 | // picking an option leaves it closed. Everything else (typing, ArrowDown, | 63 | // picking an option leaves it closed. Everything else (typing, ArrowDown, |
| 55 | // Escape, the chevron, click-outside) still drives this same signal. | 64 | // Escape, the chevron, click-outside) still drives this same signal. |
| 56 | open={open()} | 65 | open={open()} |
| 57 | - onOpenChange={setOpen} | 66 | + onOpenChange={(nextOpen, triggerMode) => changeOpen(nextOpen, triggerMode === "input")} |
| 58 | value={selected()} | 67 | value={selected()} |
| 59 | onChange={(option) => props.onChange(option?.value ?? "")} | 68 | onChange={(option) => props.onChange(option?.value ?? "")} |
| 60 | onInputChange={setQuery} | 69 | onInputChange={setQuery} |
| @@ -78,10 +87,11 @@ export function SearchableSelect(props: { | |||
| 78 | <ComboboxControl class={props.class}> | 87 | <ComboboxControl class={props.class}> |
| 79 | <ComboboxInput | 88 | <ComboboxInput |
| 80 | id={props.id} | 89 | id={props.id} |
| 90 | + ref={inputRef} | ||
| 81 | // The field is the affordance: a plain click toggles the list. The | 91 | // The field is the affordance: a plain click toggles the list. The |
| 82 | // chevron is a sibling button, so its own pointerdown toggle never | 92 | // chevron is a sibling button, so its own pointerdown toggle never |
| 83 | // reaches this handler. | 93 | // reaches this handler. |
| 84 | - onClick={() => setOpen((isOpen) => !isOpen)} | 94 | + onClick={() => changeOpen(!open())} |
| 85 | /> | 95 | /> |
| 86 | <ComboboxTrigger /> | 96 | <ComboboxTrigger /> |
| 87 | </ComboboxControl> | 97 | </ComboboxControl> |
src/components/users/user-search-select.test.tsxadded+82 -0
| @@ -0,0 +1,82 @@ | |||
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | ||
| 2 | +import { createSignal } from "solid-js"; | ||
| 3 | +import { afterEach, expect, it, vi } from "vitest"; | ||
| 4 | +import type { PersonRef } from "@/api/client"; | ||
| 5 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | ||
| 6 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 7 | + | ||
| 8 | +const getUserSearch = vi.fn(); | ||
| 9 | +const getUserById = vi.fn(); | ||
| 10 | + | ||
| 11 | +vi.mock("@/stores/auth-context", () => ({ | ||
| 12 | + useAuth: () => ({ user: () => ({ role: "manager" }) }), | ||
| 13 | +})); | ||
| 14 | +vi.mock("@/api/users", () => ({ | ||
| 15 | + getUserById: (...args: unknown[]) => getUserById(...args), | ||
| 16 | + getUserSearch: (...args: unknown[]) => getUserSearch(...args), | ||
| 17 | +})); | ||
| 18 | + | ||
| 19 | +const ada: PersonRef = { id: "ada", username: "ada", display_name: "Ada Yılmaz" }; | ||
| 20 | +const mehmet: PersonRef = { id: "mehmet", username: "mehmet", display_name: "Mehmet Kaya" }; | ||
| 21 | + | ||
| 22 | +afterEach(() => { | ||
| 23 | + cleanup(); | ||
| 24 | + getUserSearch.mockReset(); | ||
| 25 | + getUserById.mockReset(); | ||
| 26 | +}); | ||
| 27 | + | ||
| 28 | +it("clears a clicked user's search without clearing the selected user", async () => { | ||
| 29 | + getUserSearch.mockImplementation(async (query: string) => ({ | ||
| 30 | + items: query ? [mehmet] : [ada, mehmet], | ||
| 31 | + })); | ||
| 32 | + getUserById.mockResolvedValue(ada); | ||
| 33 | + const [value, setValue] = createSignal(ada.id); | ||
| 34 | + const onChange = vi.fn((next: string) => setValue(next)); | ||
| 35 | + render(() => ( | ||
| 36 | + <PreferencesProvider> | ||
| 37 | + <UserSearchSelect id="student" value={value()} onChange={onChange} initialUser={ada} /> | ||
| 38 | + </PreferencesProvider> | ||
| 39 | + )); | ||
| 40 | + const input = screen.getByRole("combobox") as HTMLInputElement; | ||
| 41 | + | ||
| 42 | + await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); | ||
| 43 | + fireEvent.click(input); | ||
| 44 | + | ||
| 45 | + expect(input.value).toBe(""); | ||
| 46 | + expect(value()).toBe(ada.id); | ||
| 47 | + expect(onChange).not.toHaveBeenCalled(); | ||
| 48 | + await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("", expect.any(AbortSignal), undefined)); | ||
| 49 | + await screen.findByRole("option", { name: /Mehmet Kaya/ }); | ||
| 50 | + | ||
| 51 | + fireEvent.input(input, { target: { value: "meh" } }); | ||
| 52 | + await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("meh", expect.any(AbortSignal), undefined)); | ||
| 53 | + fireEvent.click(input); | ||
| 54 | + | ||
| 55 | + expect(input.value).toBe(""); | ||
| 56 | + expect(value()).toBe(ada.id); | ||
| 57 | + expect(onChange).not.toHaveBeenCalled(); | ||
| 58 | + await waitFor(() => expect(getUserSearch).toHaveBeenLastCalledWith("", expect.any(AbortSignal), undefined)); | ||
| 59 | + | ||
| 60 | + fireEvent.click(await screen.findByRole("option", { name: /Mehmet Kaya/ })); | ||
| 61 | + await waitFor(() => expect(value()).toBe(mehmet.id)); | ||
| 62 | + expect(onChange).toHaveBeenCalledExactlyOnceWith(mehmet.id); | ||
| 63 | +}); | ||
| 64 | + | ||
| 65 | +it("clears the selected label when the chevron opens suggestions", async () => { | ||
| 66 | + getUserSearch.mockResolvedValue({ items: [ada, mehmet] }); | ||
| 67 | + getUserById.mockResolvedValue(ada); | ||
| 68 | + const onChange = vi.fn(); | ||
| 69 | + render(() => ( | ||
| 70 | + <PreferencesProvider> | ||
| 71 | + <UserSearchSelect id="student" value={ada.id} onChange={onChange} initialUser={ada} /> | ||
| 72 | + </PreferencesProvider> | ||
| 73 | + )); | ||
| 74 | + const input = screen.getByRole("combobox") as HTMLInputElement; | ||
| 75 | + await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); | ||
| 76 | + | ||
| 77 | + fireEvent.pointerDown(screen.getByRole("button"), { pointerType: "mouse", button: 0 }); | ||
| 78 | + | ||
| 79 | + expect(input.value).toBe(""); | ||
| 80 | + await screen.findByRole("option", { name: /Mehmet Kaya/ }); | ||
| 81 | + expect(onChange).not.toHaveBeenCalled(); | ||
| 82 | +}); | ||
src/components/users/user-search-select.tsxmodified+27 -4
| @@ -58,6 +58,8 @@ export function UserSearchSelect(props: { | |||
| 58 | const [selected, setSelected] = createSignal<PersonRef | null>(null); | 58 | const [selected, setSelected] = createSignal<PersonRef | null>(null); |
| 59 | const [loading, setLoading] = createSignal(false); | 59 | const [loading, setLoading] = createSignal(false); |
| 60 | const [open, setOpen] = createSignal(false); | 60 | const [open, setOpen] = createSignal(false); |
| 61 | + let inputRef: HTMLInputElement | undefined; | ||
| 62 | + let clearingForOpen = false; | ||
| 61 | 63 | ||
| 62 | const options = createMemo(() => { | 64 | const options = createMemo(() => { |
| 63 | const excluded = new Set(props.excludeIds ?? []); | 65 | const excluded = new Set(props.excludeIds ?? []); |
| @@ -151,12 +153,26 @@ export function UserSearchSelect(props: { | |||
| 151 | }); | 153 | }); |
| 152 | }, q.length === 0 ? 0 : 300); | 154 | }, q.length === 0 ? 0 : 300); |
| 153 | }; | 155 | }; |
| 154 | - // Opening the list (focus, the chevron) with nothing typed yet loads the | 156 | + // Opening the list starts a fresh search. Kobalte owns the displayed input |
| 155 | - // suggestions once; typing narrows them through the search. | 157 | + // value, so clear it through an input event while guarding the saved pick: |
| 158 | + // an actual user erase still clears an optional selection below. | ||
| 156 | const openSuggestions = () => { | 159 | const openSuggestions = () => { |
| 157 | if (!canSearch() || props.disabled) return; | 160 | if (!canSearch() || props.disabled) return; |
| 161 | + const needsReset = query().trim() !== "" || !!inputRef?.value; | ||
| 158 | setOpen(true); | 162 | setOpen(true); |
| 159 | - if (query().trim() === "" && users().length === 0 && !loading()) runSearch(""); | 163 | + if (needsReset) { |
| 164 | + setQuery(""); | ||
| 165 | + setUsers([]); | ||
| 166 | + if (inputRef) { | ||
| 167 | + clearingForOpen = true; | ||
| 168 | + inputRef.value = ""; | ||
| 169 | + inputRef.dispatchEvent(new Event("input", { bubbles: true })); | ||
| 170 | + clearingForOpen = false; | ||
| 171 | + } | ||
| 172 | + runSearch(""); | ||
| 173 | + } else if (users().length === 0 && !loading()) { | ||
| 174 | + runSearch(""); | ||
| 175 | + } | ||
| 160 | }; | 176 | }; |
| 161 | onCleanup(() => { | 177 | onCleanup(() => { |
| 162 | controller?.abort(); | 178 | controller?.abort(); |
| @@ -186,7 +202,11 @@ export function UserSearchSelect(props: { | |||
| 186 | // loading, then results pop in. | 202 | // loading, then results pop in. |
| 187 | allowsEmptyCollection | 203 | allowsEmptyCollection |
| 188 | open={open()} | 204 | open={open()} |
| 189 | - onOpenChange={(nextOpen) => (nextOpen ? openSuggestions() : setOpen(false))} | 205 | + onOpenChange={(nextOpen, triggerMode) => { |
| 206 | + if (!nextOpen) setOpen(false); | ||
| 207 | + else if (triggerMode === "input") setOpen(true); | ||
| 208 | + else openSuggestions(); | ||
| 209 | + }} | ||
| 190 | value={selected()} | 210 | value={selected()} |
| 191 | onChange={(user) => { | 211 | onChange={(user) => { |
| 192 | setSelected(user); | 212 | setSelected(user); |
| @@ -194,6 +214,7 @@ export function UserSearchSelect(props: { | |||
| 194 | props.onSelect?.(user ?? null); | 214 | props.onSelect?.(user ?? null); |
| 195 | }} | 215 | }} |
| 196 | onInputChange={(value) => { | 216 | onInputChange={(value) => { |
| 217 | + if (clearingForOpen) return; | ||
| 197 | // Kobalte echoes the picked person's label into the input; that is | 218 | // Kobalte echoes the picked person's label into the input; that is |
| 198 | // not a search, and treating it as one drops the preset person from | 219 | // not a search, and treating it as one drops the preset person from |
| 199 | // the options, which makes Kobalte clear the field again. | 220 | // the options, which makes Kobalte clear the field again. |
| @@ -232,6 +253,7 @@ export function UserSearchSelect(props: { | |||
| 232 | <ComboboxControl> | 253 | <ComboboxControl> |
| 233 | <ComboboxInput | 254 | <ComboboxInput |
| 234 | id={props.id} | 255 | id={props.id} |
| 256 | + ref={inputRef} | ||
| 235 | autocomplete="off" | 257 | autocomplete="off" |
| 236 | // Typing into a field that already names someone used to append | 258 | // Typing into a field that already names someone used to append |
| 237 | // to the name ("Ayşe Yılmaz (ayse.yilmaz)Mehmet") and find no one, | 259 | // to the name ("Ayşe Yılmaz (ayse.yilmaz)Mehmet") and find no one, |
| @@ -240,6 +262,7 @@ export function UserSearchSelect(props: { | |||
| 240 | (event.currentTarget as HTMLInputElement).select(); | 262 | (event.currentTarget as HTMLInputElement).select(); |
| 241 | openSuggestions(); | 263 | openSuggestions(); |
| 242 | }} | 264 | }} |
| 265 | + onClick={openSuggestions} | ||
| 243 | /> | 266 | /> |
| 244 | <ComboboxTrigger /> | 267 | <ComboboxTrigger /> |
| 245 | </ComboboxControl> | 268 | </ComboboxControl> |