This repository was archived by the owner on Feb 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSelect.tsx
154 lines (146 loc) · 3.63 KB
/
Select.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use client";
import * as React from "react";
import { SelectGroup } from "@radix-ui/react-select";
import { tv } from "tailwind-variants";
import { cn } from "@/lib/utils";
import { IconType } from "@/primitives/Icon";
import {
Select as ShadcnSelect,
SelectTrigger,
SelectContent,
SelectLabel,
SelectItem,
SelectSeparator,
} from "@/ui-shadcn/select";
import { Label } from "./Label";
export interface SelectProps {
options: {
groupLabel?: string;
items: {
label: string;
value: string;
icon?: React.ReactNode;
iconPosition?: "left" | "right";
iconType?: IconType;
}[];
separator?: boolean;
}[];
placeholder?: React.ReactNode;
onValueChange?: (value: string) => void;
value?: string;
defaultValue?: string;
variant?: "default" | "outlined" | "filled";
size?: "sm" | "md" | "lg";
className?: string;
}
const selectStyles = tv({
base: "h-10 rounded-md border text-sm",
slots: {
trigger: "h-10 rounded-md border text-sm",
item: "",
label: "",
},
variants: {
variant: {
default: {
trigger: "border-transparent bg-white ",
content: "",
item: "",
label: "",
},
outlined: {
trigger: "border-transparent bg-transparent",
item: "",
label: "",
},
filled: {
trigger: " bg-grey-200 text-grey-900",
content: "",
item: "",
label: "",
},
},
size: {
sm: {
trigger: "h-8 gap-1 text-xs",
item: "h-8 text-xs",
label: "text-xs",
},
md: {
trigger: "h-10 px-3 py-2 text-sm",
item: "text-sm",
label: "text-sm",
},
lg: {
trigger: "h-12 gap-2 text-base",
item: "text-base",
label: "",
},
},
},
defaultVariants: {
variant: "default",
size: "md",
},
});
export const getSelectedItem = (
options: SelectProps["options"],
value?: string,
defaultValue?: string,
onValueChange?: (value: string) => void,
) => {
const itemValue = value || defaultValue;
const item = options
.find((option) => option.items.find((item) => item.value === itemValue))
?.items.find((item) => item.value === itemValue);
if (item) {
onValueChange?.(item.value);
}
return item;
};
export const Select = ({
options,
placeholder = "Select an option",
onValueChange,
defaultValue,
value,
variant,
size,
className,
}: SelectProps) => {
const item = getSelectedItem(options, value, defaultValue, onValueChange);
const { trigger, item: selectItem, label } = selectStyles({ variant, size, className });
return (
<ShadcnSelect
defaultValue={defaultValue}
onValueChange={onValueChange}
value={value || defaultValue}
>
<SelectTrigger className={cn(trigger(), className)}>
{item ? <Label {...item} /> : placeholder}
</SelectTrigger>
<SelectContent>
{options.map((group, index) => (
<React.Fragment key={index}>
{group.groupLabel && (
<SelectGroup>
<SelectLabel className={label()}>{group.groupLabel}</SelectLabel>
</SelectGroup>
)}
{group.items.map((item) => (
<SelectItem key={item.value} value={item.value}>
<Label
label={item.label}
icon={item.icon}
iconPosition={item.iconPosition}
className={selectItem()}
/>
</SelectItem>
))}
{group.separator && <SelectSeparator />}
</React.Fragment>
))}
</SelectContent>
</ShadcnSelect>
);
};