forge
Explore
Log in

Don't you dare go Hollow

merge: 🩹 the picker toggles on click instead of reopening

Browse files at this commit

30e18152a5e2d40ee719df217caa68e0214aa05dAhmet Kaan GümüşParent: edbe8b02ec3110
src/components/ui/searchable-select.test.tsxmodified+45 -7
@@ -19,7 +19,7 @@ function renderSelect(onChange = vi.fn()) {
19 return { input: screen.getByRole("combobox"), onChange };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 () => {22+it("1. opens the option list on a plain click on the field", async () => {
23 const { input } = renderSelect();23 const { input } = renderSelect();
24 24
25 // No typing, no chevron: the field itself is the affordance.25 // 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 () => {
29 expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy();29 expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy();
30 });30 });
31 31
32-it("reports the picked value when an option is clicked", async () => {32+it("2. closes again when the open field is clicked", async () => {
33+ const { input } = renderSelect();
34+
35+ fireEvent.click(input);
36+ await screen.findByRole("option", { name: "9-A · Matematik" });
37+
38+ fireEvent.click(input);
39+
40+ await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
41+});
42+
43+it("3. reports the picked value and stays closed after the pick", async () => {
33 const { input, onChange } = renderSelect();44 const { input, onChange } = renderSelect();
34 45
35 fireEvent.click(input);46 fireEvent.click(input);
36 fireEvent.click(await screen.findByRole("option", { name: "10-B · Türkçe" }));47 fireEvent.click(await screen.findByRole("option", { name: "10-B · Türkçe" }));
37 48
38 await waitFor(() => expect(onChange).toHaveBeenCalledWith("t"));49 await waitFor(() => expect(onChange).toHaveBeenCalledWith("t"));
50+ // The close → refocus dance must not re-open the list by itself.
51+ await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
52+});
53+
54+it("4. closes when clicking outside the field", async () => {
55+ const { input } = renderSelect();
56+
57+ fireEvent.click(input);
58+ await screen.findByRole("option", { name: "9-A · Matematik" });
59+
60+ // Kobalte attaches its document-level outside listener one macrotask after
61+ // it opens; a real click always lands long after that.
62+ const tick = Promise.withResolvers<void>();
63+ setTimeout(tick.resolve, 0);
64+ await tick.promise;
65+ fireEvent.pointerDown(document.body);
66+
67+ await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
39 });68 });
40 69
41-it("closes on Escape without changing the value", async () => {70+it("5. closes on Escape without changing the value", async () => {
42 const { input, onChange } = renderSelect();71 const { input, onChange } = renderSelect();
43 72
44 fireEvent.click(input);73 fireEvent.click(input);
@@ -50,18 +79,27 @@ it("closes on Escape without changing the value", async () => {
50 expect(onChange).not.toHaveBeenCalled();79 expect(onChange).not.toHaveBeenCalled();
51 });80 });
52 81
53-it("closes the list after a pick and keeps the chevron working", async () => {82+it("6. opens on ArrowDown from the closed field", async () => {
83+ const { input } = renderSelect();
84+
85+ fireEvent.keyDown(input, { key: "ArrowDown" });
86+
87+ await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy());
88+});
89+
90+it("7. chevron toggles the list open and closed", async () => {
54 const { onChange } = renderSelect();91 const { onChange } = renderSelect();
55 92
56 // Opened from the chevron, not the field: the trigger must still open.93 // Opened from the chevron, not the field: the trigger must still open.
57 // Kobalte toggles on pointerdown (a bare click event never reaches it).94 // Kobalte toggles on pointerdown (a bare click event never reaches it).
58 const trigger = screen.getByRole("button");95 const trigger = screen.getByRole("button");
59 fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 });96 fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 });
60- fireEvent.click(trigger);97+ await screen.findByRole("option", { name: "9-A · Matematik" });
61- fireEvent.click(await screen.findByRole("option", { name: "9-A · Matematik" }));98+
99+ fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 });
62 100
63 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());101 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
64- expect(onChange).toHaveBeenCalledWith("m");102+ expect(onChange).not.toHaveBeenCalled();
65 });103 });
66 104
67 it("finds an option across a dash and word order", async () => {105 it("finds an option across a dash and word order", async () => {
src/components/ui/searchable-select.tsxmodified+16 -6
@@ -33,6 +33,7 @@ export function SearchableSelect(props: {
33 }) {33 }) {
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 selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null);37 const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null);
37 const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label);38 const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label);
38 // Kobalte renders nothing when its filter keeps no option, which reads as a39 // Kobalte renders nothing when its filter keeps no option, which reads as a
@@ -46,15 +47,18 @@ export function SearchableSelect(props: {
46 return (47 return (
47 <Combobox<SearchableOption>48 <Combobox<SearchableOption>
48 options={props.options}49 options={props.options}
50+ // Open state is ours: Kobalte's click handler only ever OPENS (and only
51+ // when triggerMode is "focus"), so with it a field click could not close
52+ // the list again and a pick re-opened it on the focus-back. Owning the
53+ // signal makes the field a real toggle: click opens, click again closes,
54+ // picking an option leaves it closed. Everything else (typing, ArrowDown,
55+ // Escape, the chevron, click-outside) still drives this same signal.
56+ open={open()}
57+ onOpenChange={setOpen}
49 value={selected()}58 value={selected()}
50 onChange={(option) => props.onChange(option?.value ?? "")}59 onChange={(option) => props.onChange(option?.value ?? "")}
51 onInputChange={setQuery}60 onInputChange={setQuery}
52 defaultFilter={filter}61 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"
58 // Without this Kobalte closes the popover when its filter keeps nothing,62 // Without this Kobalte closes the popover when its filter keeps nothing,
59 // so the "no results" line below would never get a chance to render.63 // so the "no results" line below would never get a chance to render.
60 allowsEmptyCollection64 allowsEmptyCollection
@@ -72,7 +76,13 @@ export function SearchableSelect(props: {
72 )}76 )}
73 >77 >
74 <ComboboxControl class={props.class}>78 <ComboboxControl class={props.class}>
75- <ComboboxInput id={props.id} />79+ <ComboboxInput
80+ id={props.id}
81+ // The field is the affordance: a plain click toggles the list. The
82+ // chevron is a sibling button, so its own pointerdown toggle never
83+ // reaches this handler.
84+ onClick={() => setOpen((isOpen) => !isOpen)}
85+ />
76 <ComboboxTrigger />86 <ComboboxTrigger />
77 </ComboboxControl>87 </ComboboxControl>
78 <ComboboxHiddenSelect />88 <ComboboxHiddenSelect />