forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

select.tsx189 lines, 7.2 KiBHistoryRawDownload
1import { For, Show, createEffect, createSignal, onCleanup, onMount, splitProps, type ComponentProps, type JSX } from "solid-js";
2import {
3 DropdownMenu,
4 DropdownMenuContent,
5 DropdownMenuItem,
6 DropdownMenuTrigger,
7} from "@/components/ui/dropdown-menu";
8import { IconCheck, IconChevronDown } from "@/components/ui/icons";
9import { cn } from "@/lib/cn";
10
11/** Shared trigger look for every dropdown-style picker (Select, DropdownSelect). */
12const SELECT_TRIGGER =
13 "inline-flex items-center justify-between gap-2.5 rounded-lg border border-border/70 bg-muted/40 px-2.5 text-[13px] font-medium text-foreground transition-colors hover:border-border hover:bg-muted/60 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 disabled:pointer-events-none disabled:opacity-50 data-expanded:border-border data-expanded:bg-muted/60";
14
15export type SelectOption<T extends string | number = string> = {
16 value: T;
17 label: string;
18 icon?: JSX.Element;
19};
20
21export type DropdownSelectProps<T extends string | number = string> = {
22 options: SelectOption<T>[];
23 value: T;
24 onChange: (value: T) => void;
25 placeholder?: string;
26 labelPrefix?: string;
27 class?: string;
28 triggerClass?: string;
29 disabled?: boolean;
30};
31
32export function DropdownSelect<T extends string | number = string>(props: DropdownSelectProps<T>) {
33 const selectedOption = () => props.options.find((opt) => opt.value === props.value);
34 const displayLabel = () => selectedOption()?.label ?? props.placeholder ?? "";
35
36 return (
37 <DropdownMenu placement="bottom-start" gutter={6}>
38 <DropdownMenuTrigger
39 disabled={props.disabled}
40 // A prefixed picker is a filter chip, whose first option is its "all"
41 // default; any other value marks it active so the toolbar can tint it.
42 data-filter-active={props.labelPrefix && props.options.length > 0 && props.value !== props.options[0].value ? "" : undefined}
43 class={cn(
44 SELECT_TRIGGER,
45 "h-8",
46 props.triggerClass,
47 props.class,
48 )}
49 >
50 <div class="flex items-center gap-1.5 min-w-0 truncate">
51 <Show when={props.labelPrefix}>
52 <span class="shrink-0 text-xs font-semibold text-muted-foreground">{props.labelPrefix}:</span>
53 </Show>
54 <Show when={selectedOption()?.icon}>
55 <span class="shrink-0">{selectedOption()!.icon}</span>
56 </Show>
57 <span class="truncate font-medium">{displayLabel()}</span>
58 </div>
59 <IconChevronDown class="h-3.5 w-3.5 shrink-0 opacity-60 transition-transform duration-200" />
60 </DropdownMenuTrigger>
61 <DropdownMenuContent class="max-h-72 min-w-48 overflow-y-auto">
62 <For each={props.options}>
63 {(option) => {
64 const isSelected = () => option.value === props.value;
65 return (
66 <DropdownMenuItem
67 class={cn(
68 "flex items-center justify-between gap-2.5 text-sm",
69 isSelected() && "bg-primary/10 text-primary-text font-semibold",
70 )}
71 onSelect={() => props.onChange(option.value)}
72 >
73 <div class="flex items-center gap-2 min-w-0 truncate">
74 <Show when={option.icon}>
75 <span class="shrink-0">{option.icon}</span>
76 </Show>
77 <span class="truncate">{option.label}</span>
78 </div>
79 <Show when={isSelected()}>
80 <IconCheck class="h-3.5 w-3.5 shrink-0 text-primary-text" />
81 </Show>
82 </DropdownMenuItem>
83 );
84 }}
85 </For>
86 </DropdownMenuContent>
87 </DropdownMenu>
88 );
89}
90
91export type SelectProps = ComponentProps<"select"> & {
92 wrapperClass?: string;
93};
94
95type OptionSnapshot = { value: string; label: string; disabled: boolean };
96
97/**
98 * Drop-in for a native `<select>` with `<option>` children, drawn as the app's
99 * own dropdown menu. The native element stays in the DOM (visually hidden) and
100 * remains the source of truth: it holds `name`/`required`/labels for forms and
101 * tests, and picking an item sets its value and fires a real `change` event,
102 * so every existing `onChange={(e) => e.currentTarget.value}` keeps working.
103 */
104export function Select(props: SelectProps) {
105 const [local, rest] = splitProps(props, ["class", "wrapperClass", "disabled"]);
106 let native: HTMLSelectElement | undefined;
107 const [options, setOptions] = createSignal<OptionSnapshot[]>([]);
108 const [current, setCurrent] = createSignal("");
109
110 const snapshot = () => {
111 if (!native) return;
112 setOptions(
113 Array.from(native.options).map((option) => ({
114 value: option.value,
115 label: option.textContent ?? "",
116 disabled: option.disabled,
117 })),
118 );
119 setCurrent(native.value);
120 };
121
122 onMount(() => {
123 snapshot();
124 // <For> inside the select adds options after mount; follow them.
125 const observer = new MutationObserver(snapshot);
126 observer.observe(native!, { childList: true, subtree: true, characterData: true, attributes: true });
127 native!.addEventListener("change", snapshot);
128 onCleanup(() => {
129 observer.disconnect();
130 native?.removeEventListener("change", snapshot);
131 });
132 });
133 // A controlled `value` can change without touching the option list.
134 createEffect(() => {
135 void props.value;
136 queueMicrotask(snapshot);
137 });
138
139 const selected = () => options().find((option) => option.value === current()) ?? options()[0];
140
141 const pick = (value: string) => {
142 if (!native || native.value === value) return;
143 native.value = value;
144 native.dispatchEvent(new Event("input", { bubbles: true }));
145 native.dispatchEvent(new Event("change", { bubbles: true }));
146 };
147
148 return (
149 <div class={cn("relative w-full", local.wrapperClass)}>
150 <select
151 ref={native}
152 tabIndex={-1}
153 aria-hidden="true"
154 disabled={local.disabled}
155 class="sr-only"
156 {...rest}
157 />
158 <DropdownMenu placement="bottom-start" gutter={6} sameWidth>
159 <DropdownMenuTrigger
160 disabled={local.disabled}
161 aria-label={props["aria-label"] ?? selected()?.label}
162 class={cn(SELECT_TRIGGER, "h-9 w-full", local.class)}
163 >
164 <span class="min-w-0 flex-1 truncate text-left">{selected()?.label ?? ""}</span>
165 <IconChevronDown class="h-3.5 w-3.5 shrink-0 opacity-60" />
166 </DropdownMenuTrigger>
167 <DropdownMenuContent class="max-h-72 min-w-40 overflow-y-auto">
168 <For each={options()}>
169 {(option) => (
170 <DropdownMenuItem
171 disabled={option.disabled}
172 class={cn(
173 "flex items-center justify-between gap-2.5 text-sm",
174 option.value === current() && "bg-primary/10 font-semibold text-primary-text",
175 )}
176 onSelect={() => pick(option.value)}
177 >
178 <span class="truncate">{option.label}</span>
179 <Show when={option.value === current()}>
180 <IconCheck class="h-3.5 w-3.5 shrink-0 text-primary-text" />
181 </Show>
182 </DropdownMenuItem>
183 )}
184 </For>
185 </DropdownMenuContent>
186 </DropdownMenu>
187 </div>
188 );
189}