diff --git a/src/components/ui/searchable-select.test.tsx b/src/components/ui/searchable-select.test.tsx index 5fc45ec..c46ac78 100644 --- a/src/components/ui/searchable-select.test.tsx +++ b/src/components/ui/searchable-select.test.tsx @@ -1,4 +1,5 @@ import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; +import { createSignal } from "solid-js"; import { afterEach, expect, it, vi } from "vitest"; import { SearchableSelect } from "@/components/ui/searchable-select"; import { PreferencesProvider } from "@/stores/preferences-context"; @@ -126,3 +127,32 @@ it("says so when the search matches nothing", async () => { await waitFor(() => expect(screen.getByText("No results.")).toBeTruthy()); }); + +it("clears the search on reopening while keeping the selected option", async () => { + const [value, setValue] = createSignal("m"); + const onChange = vi.fn((next: string) => setValue(next)); + render(() => ( + + + + )); + const input = screen.getByRole("combobox") as HTMLInputElement; + + expect(input.value).toBe("9-A · Matematik"); + fireEvent.click(input); + expect(input.value).toBe(""); + await screen.findByRole("option", { name: "10-B · Türkçe" }); + + fireEvent.input(input, { target: { value: "turkce" } }); + await waitFor(() => expect(screen.queryByRole("option", { name: "9-A · Matematik" })).toBeNull()); + fireEvent.click(screen.getByRole("option", { name: "10-B · Türkçe" })); + await waitFor(() => expect(value()).toBe("t")); + await waitFor(() => expect(input.value).toBe("10-B · Türkçe")); + + fireEvent.click(input); + expect(input.value).toBe(""); + await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy()); + expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy(); + expect(value()).toBe("t"); + expect(onChange).toHaveBeenCalledTimes(1); +}); diff --git a/src/components/ui/searchable-select.tsx b/src/components/ui/searchable-select.tsx index ed05818..d5f61ef 100644 --- a/src/components/ui/searchable-select.tsx +++ b/src/components/ui/searchable-select.tsx @@ -34,8 +34,17 @@ export function SearchableSelect(props: { const t = useT(); const [query, setQuery] = createSignal(""); const [open, setOpen] = createSignal(false); + let inputRef: HTMLInputElement | undefined; const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null); const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label); + const changeOpen = (nextOpen: boolean, fromTyping = false) => { + setOpen(nextOpen); + if (!nextOpen || fromTyping || !inputRef) return; + // Kobalte owns the displayed input value. An input event clears its + // internal filter as well as the field, without changing the selection. + inputRef.value = ""; + inputRef.dispatchEvent(new Event("input", { bubbles: true })); + }; // Kobalte renders nothing when its filter keeps no option, which reads as a // broken dropdown; this says the search came up empty instead. const empty = createMemo(() => { @@ -54,7 +63,7 @@ export function SearchableSelect(props: { // picking an option leaves it closed. Everything else (typing, ArrowDown, // Escape, the chevron, click-outside) still drives this same signal. open={open()} - onOpenChange={setOpen} + onOpenChange={(nextOpen, triggerMode) => changeOpen(nextOpen, triggerMode === "input")} value={selected()} onChange={(option) => props.onChange(option?.value ?? "")} onInputChange={setQuery} @@ -78,10 +87,11 @@ export function SearchableSelect(props: { setOpen((isOpen) => !isOpen)} + onClick={() => changeOpen(!open())} /> diff --git a/src/components/users/user-search-select.test.tsx b/src/components/users/user-search-select.test.tsx new file mode 100644 index 0000000..be979c9 --- /dev/null +++ b/src/components/users/user-search-select.test.tsx @@ -0,0 +1,82 @@ +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; +import { createSignal } from "solid-js"; +import { afterEach, expect, it, vi } from "vitest"; +import type { PersonRef } from "@/api/client"; +import { UserSearchSelect } from "@/components/users/user-search-select"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +const getUserSearch = vi.fn(); +const getUserById = vi.fn(); + +vi.mock("@/stores/auth-context", () => ({ + useAuth: () => ({ user: () => ({ role: "manager" }) }), +})); +vi.mock("@/api/users", () => ({ + getUserById: (...args: unknown[]) => getUserById(...args), + getUserSearch: (...args: unknown[]) => getUserSearch(...args), +})); + +const ada: PersonRef = { id: "ada", username: "ada", display_name: "Ada Yılmaz" }; +const mehmet: PersonRef = { id: "mehmet", username: "mehmet", display_name: "Mehmet Kaya" }; + +afterEach(() => { + cleanup(); + getUserSearch.mockReset(); + getUserById.mockReset(); +}); + +it("clears a clicked user's search without clearing the selected user", async () => { + getUserSearch.mockImplementation(async (query: string) => ({ + items: query ? [mehmet] : [ada, mehmet], + })); + getUserById.mockResolvedValue(ada); + const [value, setValue] = createSignal(ada.id); + const onChange = vi.fn((next: string) => setValue(next)); + render(() => ( + + + + )); + const input = screen.getByRole("combobox") as HTMLInputElement; + + await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); + fireEvent.click(input); + + expect(input.value).toBe(""); + expect(value()).toBe(ada.id); + expect(onChange).not.toHaveBeenCalled(); + await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("", expect.any(AbortSignal), undefined)); + await screen.findByRole("option", { name: /Mehmet Kaya/ }); + + fireEvent.input(input, { target: { value: "meh" } }); + await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("meh", expect.any(AbortSignal), undefined)); + fireEvent.click(input); + + expect(input.value).toBe(""); + expect(value()).toBe(ada.id); + expect(onChange).not.toHaveBeenCalled(); + await waitFor(() => expect(getUserSearch).toHaveBeenLastCalledWith("", expect.any(AbortSignal), undefined)); + + fireEvent.click(await screen.findByRole("option", { name: /Mehmet Kaya/ })); + await waitFor(() => expect(value()).toBe(mehmet.id)); + expect(onChange).toHaveBeenCalledExactlyOnceWith(mehmet.id); +}); + +it("clears the selected label when the chevron opens suggestions", async () => { + getUserSearch.mockResolvedValue({ items: [ada, mehmet] }); + getUserById.mockResolvedValue(ada); + const onChange = vi.fn(); + render(() => ( + + + + )); + const input = screen.getByRole("combobox") as HTMLInputElement; + await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); + + fireEvent.pointerDown(screen.getByRole("button"), { pointerType: "mouse", button: 0 }); + + expect(input.value).toBe(""); + await screen.findByRole("option", { name: /Mehmet Kaya/ }); + expect(onChange).not.toHaveBeenCalled(); +}); diff --git a/src/components/users/user-search-select.tsx b/src/components/users/user-search-select.tsx index ca9965c..2317955 100644 --- a/src/components/users/user-search-select.tsx +++ b/src/components/users/user-search-select.tsx @@ -58,6 +58,8 @@ export function UserSearchSelect(props: { const [selected, setSelected] = createSignal(null); const [loading, setLoading] = createSignal(false); const [open, setOpen] = createSignal(false); + let inputRef: HTMLInputElement | undefined; + let clearingForOpen = false; const options = createMemo(() => { const excluded = new Set(props.excludeIds ?? []); @@ -151,12 +153,26 @@ export function UserSearchSelect(props: { }); }, q.length === 0 ? 0 : 300); }; - // Opening the list (focus, the chevron) with nothing typed yet loads the - // suggestions once; typing narrows them through the search. + // Opening the list starts a fresh search. Kobalte owns the displayed input + // value, so clear it through an input event while guarding the saved pick: + // an actual user erase still clears an optional selection below. const openSuggestions = () => { if (!canSearch() || props.disabled) return; + const needsReset = query().trim() !== "" || !!inputRef?.value; setOpen(true); - if (query().trim() === "" && users().length === 0 && !loading()) runSearch(""); + if (needsReset) { + setQuery(""); + setUsers([]); + if (inputRef) { + clearingForOpen = true; + inputRef.value = ""; + inputRef.dispatchEvent(new Event("input", { bubbles: true })); + clearingForOpen = false; + } + runSearch(""); + } else if (users().length === 0 && !loading()) { + runSearch(""); + } }; onCleanup(() => { controller?.abort(); @@ -186,7 +202,11 @@ export function UserSearchSelect(props: { // loading, then results pop in. allowsEmptyCollection open={open()} - onOpenChange={(nextOpen) => (nextOpen ? openSuggestions() : setOpen(false))} + onOpenChange={(nextOpen, triggerMode) => { + if (!nextOpen) setOpen(false); + else if (triggerMode === "input") setOpen(true); + else openSuggestions(); + }} value={selected()} onChange={(user) => { setSelected(user); @@ -194,6 +214,7 @@ export function UserSearchSelect(props: { props.onSelect?.(user ?? null); }} onInputChange={(value) => { + if (clearingForOpen) return; // Kobalte echoes the picked person's label into the input; that is // not a search, and treating it as one drops the preset person from // the options, which makes Kobalte clear the field again. @@ -232,6 +253,7 @@ export function UserSearchSelect(props: {