forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

select.tsx105 lines, 4.1 KiBHistoryRawDownload
1import { For, Show, type JSX, splitProps, type ComponentProps } 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
11export type SelectOption<T extends string | number = string> = {
12 value: T;
13 label: string;
14 icon?: JSX.Element;
15};
16
17export type DropdownSelectProps<T extends string | number = string> = {
18 options: SelectOption<T>[];
19 value: T;
20 onChange: (value: T) => void;
21 placeholder?: string;
22 labelPrefix?: string;
23 class?: string;
24 triggerClass?: string;
25 disabled?: boolean;
26};
27
28export function DropdownSelect<T extends string | number = string>(props: DropdownSelectProps<T>) {
29 const selectedOption = () => props.options.find((opt) => opt.value === props.value);
30 const displayLabel = () => selectedOption()?.label ?? props.placeholder ?? "";
31
32 return (
33 <DropdownMenu placement="bottom-start" gutter={6}>
34 <DropdownMenuTrigger
35 disabled={props.disabled}
36 class={cn(
37 "inline-flex h-8 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-all hover:border-border hover:bg-muted active:scale-[0.98] focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring/70 focus-visible:ring-offset-1 disabled:pointer-events-none disabled:opacity-50",
38 props.triggerClass,
39 props.class,
40 )}
41 >
42 <div class="flex items-center gap-1.5 min-w-0 truncate">
43 <Show when={props.labelPrefix}>
44 <span class="shrink-0 text-xs font-semibold text-muted-foreground">{props.labelPrefix}:</span>
45 </Show>
46 <Show when={selectedOption()?.icon}>
47 <span class="shrink-0">{selectedOption()!.icon}</span>
48 </Show>
49 <span class="truncate font-medium">{displayLabel()}</span>
50 </div>
51 <IconChevronDown class="h-3.5 w-3.5 shrink-0 opacity-60 transition-transform duration-200" />
52 </DropdownMenuTrigger>
53 <DropdownMenuContent class="max-h-72 min-w-48 overflow-y-auto">
54 <For each={props.options}>
55 {(option) => {
56 const isSelected = () => option.value === props.value;
57 return (
58 <DropdownMenuItem
59 class={cn(
60 "flex items-center justify-between gap-2.5 text-sm",
61 isSelected() && "bg-primary/10 text-primary font-semibold",
62 )}
63 onSelect={() => props.onChange(option.value)}
64 >
65 <div class="flex items-center gap-2 min-w-0 truncate">
66 <Show when={option.icon}>
67 <span class="shrink-0">{option.icon}</span>
68 </Show>
69 <span class="truncate">{option.label}</span>
70 </div>
71 <Show when={isSelected()}>
72 <IconCheck class="h-3.5 w-3.5 shrink-0 text-primary" />
73 </Show>
74 </DropdownMenuItem>
75 );
76 }}
77 </For>
78 </DropdownMenuContent>
79 </DropdownMenu>
80 );
81}
82
83export type SelectProps = ComponentProps<"select"> & {
84 wrapperClass?: string;
85};
86
87export function Select(props: SelectProps) {
88 const [local, rest] = splitProps(props, ["class", "wrapperClass"]);
89 return (
90 <div class={cn("relative w-full", local.wrapperClass)}>
91 <select
92 class={cn(
93 "flex h-9 w-full appearance-none rounded-md border border-input bg-background/90 py-2 pl-3 pr-9 text-sm text-foreground shadow-sm transition-all",
94 "hover:border-ring/45 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring/70 focus-visible:ring-offset-1",
95 "disabled:cursor-not-allowed disabled:opacity-50 [&>option]:bg-popover [&>option]:text-popover-foreground [&>option]:py-1.5",
96 local.class,
97 )}
98 {...rest}
99 />
100 <span class="pointer-events-none absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground/70">
101 <IconChevronDown class="h-4 w-4" />
102 </span>
103 </div>
104 );
105}