Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

select.tsx

186 lines5.8 KB

1'use client'
2​
3import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from 'lucide-react'
4import { Select as SelectPrimitive } from 'radix-ui'
5import type * as React from 'react'
6​
7import cn from '@/utils/cn'
8​
9function Select({
10 ...props
11}: React.ComponentProps<typeof SelectPrimitive.Root>) {
12 return <SelectPrimitive.Root data-slot="select" {...props} />
14​
15function SelectGroup({
16 ...props
17}: React.ComponentProps<typeof SelectPrimitive.Group>) {
18 return <SelectPrimitive.Group data-slot="select-group" {...props} />
20​
21function SelectValue({
22 ...props
23}: React.ComponentProps<typeof SelectPrimitive.Value>) {
24 return <SelectPrimitive.Value data-slot="select-value" {...props} />
26​
27function SelectTrigger({
28 className,
29 children,
30 ...props
31}: React.ComponentProps<typeof SelectPrimitive.Trigger>) {
32 return (
33 <SelectPrimitive.Trigger
34 className={cn(
35 'flex h-11 w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-foreground text-sm shadow-xs outline-none transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[placeholder]:text-muted-foreground *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 dark:aria-invalid:ring-destructive/40 [&>span]:line-clamp-1 [&_svg]:pointer-events-none [&_svg]:shrink-0 dark:hover:bg-input/50 dark:bg-input/30',
36 className,
37 )}
38 data-slot="select-trigger"
39 {...props}
40 >
41 {children}
42 <SelectPrimitive.Icon asChild>
43 <ChevronDownIcon
44 className="shrink-0 in-aria-invalid:text-destructive/80 text-muted-foreground/80"
45 size={16}
46 />
47 </SelectPrimitive.Icon>
48 </SelectPrimitive.Trigger>
49 )
51​
52function SelectContent({
53 className,
54 children,
55 position = 'popper',
56 ...props
57}: React.ComponentProps<typeof SelectPrimitive.Content>) {
58 return (
59 <SelectPrimitive.Portal>
60 <SelectPrimitive.Content
61 className={cn(
62 'data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 relative z-50 max-h-[min(24rem,var(--radix-select-content-available-height))] min-w-32 overflow-hidden rounded-md border border-input bg-popover text-popover-foreground shadow-lg data-[state=closed]:animate-out data-[state=open]:animate-in [&_[role=group]]:py-1',
63 position === 'popper' &&
64 'data-[side=left]:-translate-x-1 data-[side=top]:-translate-y-1 w-full min-w-[var(--radix-select-trigger-width)] data-[side=right]:translate-x-1 data-[side=bottom]:translate-y-1',
65 className,
66 )}
67 data-slot="select-content"
68 position={position}
69 {...props}
70 >
71 <SelectScrollUpButton />
72 <SelectPrimitive.Viewport
73 className={cn(
74 'p-1',
75 position === 'popper' && 'h-[var(--radix-select-trigger-height)]',
76 )}
77 >
78 {children}
79 </SelectPrimitive.Viewport>
80 <SelectScrollDownButton />
81 </SelectPrimitive.Content>
82 </SelectPrimitive.Portal>
83 )
85​
86function SelectLabel({
87 className,
88 ...props
89}: React.ComponentProps<typeof SelectPrimitive.Label>) {
90 return (
91 <SelectPrimitive.Label
92 className={cn(
93 'py-1.5 ps-8 pe-2 font-medium text-muted-foreground text-xs',
94 className,
95 )}
96 data-slot="select-label"
97 {...props}
98 />
99 )
101​
102function SelectItem({
103 className,
104 children,
105 ...props
106}: React.ComponentProps<typeof SelectPrimitive.Item>) {
107 return (
108 <SelectPrimitive.Item
109 className={cn(
110 'relative flex w-full cursor-default select-none items-center rounded py-1.5 ps-8 pe-2 text-sm outline-hidden focus:bg-accent focus:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50',
111 className,
112 )}
113 data-slot="select-item"
114 {...props}
115 >
116 <span className="absolute start-2 flex size-3.5 items-center justify-center">
117 <SelectPrimitive.ItemIndicator>
118 <CheckIcon size={16} />
119 </SelectPrimitive.ItemIndicator>
120 </span>
121 <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
122 </SelectPrimitive.Item>
123 )
125​
126function SelectSeparator({
127 className,
128 ...props
129}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
130 return (
131 <SelectPrimitive.Separator
132 className={cn('-mx-1 my-1 h-px bg-border', className)}
133 data-slot="select-separator"
134 {...props}
135 />
136 )
138​
139function SelectScrollUpButton({
140 className,
141 ...props
142}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
143 return (
144 <SelectPrimitive.ScrollUpButton
145 className={cn(
146 'flex cursor-default items-center justify-center py-1 text-muted-foreground/80',
147 className,
148 )}
149 data-slot="select-scroll-up-button"
150 {...props}
151 >
152 <ChevronUpIcon size={16} />
153 </SelectPrimitive.ScrollUpButton>
154 )
156​
157function SelectScrollDownButton({
158 className,
159 ...props
160}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
161 return (
162 <SelectPrimitive.ScrollDownButton
163 className={cn(
164 'flex cursor-default items-center justify-center py-1 text-muted-foreground/80',
165 className,
166 )}
167 data-slot="select-scroll-down-button"
168 {...props}
169 >
170 <ChevronDownIcon size={16} />
171 </SelectPrimitive.ScrollDownButton>
172 )
174​
175export {
176 Select,
177 SelectContent,
178 SelectGroup,
179 SelectItem,
180 SelectLabel,
181 SelectScrollDownButton,
182 SelectScrollUpButton,
183 SelectSeparator,
184 SelectTrigger,
185 SelectValue,