hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, createMemo, createSignal } from "solid-js"; |
| 2 | import { |
| 3 | Combobox, |
| 4 | ComboboxContent, |
| 5 | ComboboxControl, |
| 6 | ComboboxHiddenSelect, |
| 7 | ComboboxInput, |
| 8 | ComboboxItem, |
| 9 | ComboboxItemLabel, |
| 10 | ComboboxTrigger, |
| 11 | } from "@/components/ui/combobox"; |
| 12 | import { matchesSearch } from "@/lib/search-text"; |
| 13 | import { useT } from "@/stores/preferences-context"; |
| 14 | |
| 15 | export 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 | */ |
| 23 | export 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 | /** Filter-chip prefix drawn inside the pill ("Ders: Tümü"), like |
| 34 | * DropdownSelect's `labelPrefix`; it also labels the field. */ |
| 35 | labelPrefix?: string; |
| 36 | }) { |
| 37 | const t = useT(); |
| 38 | const [query, setQuery] = createSignal(""); |
| 39 | const [open, setOpen] = createSignal(false); |
| 40 | let inputRef: HTMLInputElement | undefined; |
| 41 | const selected = createMemo(() => props.options.find((o) => o.value === props.value) ?? null); |
| 42 | const filter = (option: SearchableOption, input: string) => matchesSearch(input, option.label); |
| 43 | const changeOpen = (nextOpen: boolean, fromTyping = false) => { |
| 44 | setOpen(nextOpen); |
| 45 | if (!nextOpen || fromTyping || !inputRef) return; |
| 46 | // Kobalte owns the displayed input value. An input event clears its |
| 47 | // internal filter as well as the field, without changing the selection. |
| 48 | inputRef.value = ""; |
| 49 | inputRef.dispatchEvent(new Event("input", { bubbles: true })); |
| 50 | }; |
| 51 | // Kobalte renders nothing when its filter keeps no option, which reads as a |
| 52 | // broken dropdown; this says the search came up empty instead. |
| 53 | const empty = createMemo(() => { |
| 54 | const input = query().trim(); |
| 55 | if (!input || input === selected()?.label) return false; |
| 56 | return !props.options.some((option) => filter(option, input)); |
| 57 | }); |
| 58 | |
| 59 | return ( |
| 60 | <Combobox<SearchableOption> |
| 61 | options={props.options} |
| 62 | // Open state is ours: Kobalte's click handler only ever OPENS (and only |
| 63 | // when triggerMode is "focus"), so with it a field click could not close |
| 64 | // the list again and a pick re-opened it on the focus-back. Owning the |
| 65 | // signal makes the field a real toggle: click opens, click again closes, |
| 66 | // picking an option leaves it closed. Everything else (typing, ArrowDown, |
| 67 | // Escape, the chevron, click-outside) still drives this same signal. |
| 68 | open={open()} |
| 69 | onOpenChange={(nextOpen, triggerMode) => changeOpen(nextOpen, triggerMode === "input")} |
| 70 | value={selected()} |
| 71 | onChange={(option) => props.onChange(option?.value ?? "")} |
| 72 | onInputChange={setQuery} |
| 73 | defaultFilter={filter} |
| 74 | // Without this Kobalte closes the popover when its filter keeps nothing, |
| 75 | // so the "no results" line below would never get a chance to render. |
| 76 | allowsEmptyCollection |
| 77 | optionValue="value" |
| 78 | optionLabel="label" |
| 79 | optionTextValue="label" |
| 80 | placeholder={props.placeholder} |
| 81 | disabled={props.disabled} |
| 82 | required={props.required} |
| 83 | name={props.name} |
| 84 | itemComponent={(itemProps) => ( |
| 85 | <ComboboxItem item={itemProps.item}> |
| 86 | <ComboboxItemLabel>{itemProps.item.rawValue.label}</ComboboxItemLabel> |
| 87 | </ComboboxItem> |
| 88 | )} |
| 89 | > |
| 90 | <ComboboxControl class={props.class}> |
| 91 | <Show when={props.labelPrefix}> |
| 92 | <label for={props.id} class="shrink-0 cursor-pointer pl-3 text-xs font-semibold text-muted-foreground"> |
| 93 | {props.labelPrefix}: |
| 94 | </label> |
| 95 | </Show> |
| 96 | <ComboboxInput |
| 97 | id={props.id} |
| 98 | class={props.labelPrefix ? "pl-1.5 font-medium" : undefined} |
| 99 | ref={inputRef} |
| 100 | // The field is the affordance: a plain click toggles the list. The |
| 101 | // chevron is a sibling button, so its own pointerdown toggle never |
| 102 | // reaches this handler. |
| 103 | onClick={() => changeOpen(!open())} |
| 104 | /> |
| 105 | <ComboboxTrigger /> |
| 106 | </ComboboxControl> |
| 107 | <ComboboxHiddenSelect /> |
| 108 | <ComboboxContent> |
| 109 | <Show when={empty()}> |
| 110 | <p class="px-2.5 py-2 text-xs text-muted-foreground">{t("common.noResults")}</p> |
| 111 | </Show> |
| 112 | </ComboboxContent> |
| 113 | </Combobox> |
| 114 | ); |
| 115 | } |