forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

searchable-select.tsx96 lines, 3.5 KiBHistoryRawDownload
1import { Show, createMemo, createSignal } from "solid-js";
2import {
3 Combobox,
4 ComboboxContent,
5 ComboboxControl,
6 ComboboxHiddenSelect,
7 ComboboxInput,
8 ComboboxItem,
9 ComboboxItemLabel,
10 ComboboxTrigger,
11} from "@/components/ui/combobox";
12import { matchesSearch } from "@/lib/search-text";
13import { useT } from "@/stores/preferences-context";
14
15export type SearchableOption = { value: string; label: string };
16
17/**
18 * Type-to-filter dropdown over a preloaded option list. Kobalte handles
19 * keyboard nav, ARIA and highlight; the filter is ours so that typing
20 * "9a matematik" finds "9-A · Matematik" — Kobalte's own "contains" is a raw
21 * substring test and misses both the dash and the word order.
22 */
23export function SearchableSelect(props: {
24 id?: string;
25 name?: string;
26 value: string;
27 onChange: (value: string) => void;
28 options: SearchableOption[];
29 placeholder?: string;
30 disabled?: boolean;
31 required?: boolean;
32 class?: string;
33}) {
34 const t = useT();
35 const [query, setQuery] = createSignal("");
36 const [open, setOpen] = createSignal(false);
37 const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null);
38 const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label);
39 // Kobalte renders nothing when its filter keeps no option, which reads as a
40 // broken dropdown; this says the search came up empty instead.
41 const empty = createMemo(() => {
42 const input = query().trim();
43 if (!input || input === selected()?.label) return false;
44 return !props.options.some((option) => filter(option, input));
45 });
46
47 return (
48 <Combobox<SearchableOption>
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}
58 value={selected()}
59 onChange={(option) => props.onChange(option?.value ?? "")}
60 onInputChange={setQuery}
61 defaultFilter={filter}
62 // Without this Kobalte closes the popover when its filter keeps nothing,
63 // so the "no results" line below would never get a chance to render.
64 allowsEmptyCollection
65 optionValue="value"
66 optionLabel="label"
67 optionTextValue="label"
68 placeholder={props.placeholder}
69 disabled={props.disabled}
70 required={props.required}
71 name={props.name}
72 itemComponent={(itemProps) => (
73 <ComboboxItem item={itemProps.item}>
74 <ComboboxItemLabel>{itemProps.item.rawValue.label}</ComboboxItemLabel>
75 </ComboboxItem>
76 )}
77 >
78 <ComboboxControl class={props.class}>
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 />
86 <ComboboxTrigger />
87 </ComboboxControl>
88 <ComboboxHiddenSelect />
89 <ComboboxContent>
90 <Show when={empty()}>
91 <p class="px-2.5 py-2 text-xs text-muted-foreground">{t("common.noResults")}</p>
92 </Show>
93 </ComboboxContent>
94 </Combobox>
95 );
96}