Branchwell

AI Resource Pro / website · airesourcepro.com / src / components / ui / select.tsx

Raw 5.5 KB on b7854f3692d64213a2fce644d0b5101d3e345b03
1"use client"
2
3import * as React from "react"
4import * as SelectPrimitive from "@radix-ui/react-select"
5import { Check, ChevronDown, ChevronUp } from "lucide-react"
6
7import { cn } from "@/lib/utils"
8
9const Select = SelectPrimitive.Root
10
11const SelectGroup = SelectPrimitive.Group
12
13const SelectValue = SelectPrimitive.Value
14
15const SelectTrigger = React.forwardRef<
16 React.ElementRef<typeof SelectPrimitive.Trigger>,
17 React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
18>(({ className, children, ...props }, ref) => (
19 <SelectPrimitive.Trigger
20 ref={ref}
21 className={cn(
22 "flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background data-[placeholder]:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
23 className
24 )}
25 {...props}
26 >
27 {children}
28 <SelectPrimitive.Icon asChild>
29 <ChevronDown className="h-4 w-4 opacity-50" />
30 </SelectPrimitive.Icon>
31 </SelectPrimitive.Trigger>
32))
33SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
34
35const SelectScrollUpButton = React.forwardRef<
36 React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
37 React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
38>(({ className, ...props }, ref) => (
39 <SelectPrimitive.ScrollUpButton
40 ref={ref}
41 className={cn(
42 "flex cursor-default items-center justify-center py-1",
43 className
44 )}
45 {...props}
46 >
47 <ChevronUp className="h-4 w-4" />
48 </SelectPrimitive.ScrollUpButton>
49))
50SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
51
52const SelectScrollDownButton = React.forwardRef<
53 React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
54 React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
55>(({ className, ...props }, ref) => (
56 <SelectPrimitive.ScrollDownButton
57 ref={ref}
58 className={cn(
59 "flex cursor-default items-center justify-center py-1",
60 className
61 )}
62 {...props}
63 >
64 <ChevronDown className="h-4 w-4" />
65 </SelectPrimitive.ScrollDownButton>
66))
67SelectScrollDownButton.displayName =
68 SelectPrimitive.ScrollDownButton.displayName
69
70const SelectContent = React.forwardRef<
71 React.ElementRef<typeof SelectPrimitive.Content>,
72 React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
73>(({ className, children, position = "popper", ...props }, ref) => (
74 <SelectPrimitive.Portal>
75 <SelectPrimitive.Content
76 ref={ref}
77 className={cn(
78 "relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 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",
79 position === "popper" &&
80 "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
81 className
82 )}
83 position={position}
84 {...props}
85 >
86 <SelectScrollUpButton />
87 <SelectPrimitive.Viewport
88 className={cn(
89 "p-1",
90 position === "popper" &&
91 "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
92 )}
93 >
94 {children}
95 </SelectPrimitive.Viewport>
96 <SelectScrollDownButton />
97 </SelectPrimitive.Content>
98 </SelectPrimitive.Portal>
99))
100SelectContent.displayName = SelectPrimitive.Content.displayName
101
102const SelectLabel = React.forwardRef<
103 React.ElementRef<typeof SelectPrimitive.Label>,
104 React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
105>(({ className, ...props }, ref) => (
106 <SelectPrimitive.Label
107 ref={ref}
108 className={cn("px-2 py-1.5 text-sm font-semibold", className)}
109 {...props}
110 />
111))
112SelectLabel.displayName = SelectPrimitive.Label.displayName
113
114const SelectItem = React.forwardRef<
115 React.ElementRef<typeof SelectPrimitive.Item>,
116 React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
117>(({ className, children, ...props }, ref) => (
118 <SelectPrimitive.Item
119 ref={ref}
120 className={cn(
121 "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
122 className
123 )}
124 {...props}
125 >
126 <span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
127 <SelectPrimitive.ItemIndicator>
128 <Check className="h-4 w-4" />
129 </SelectPrimitive.ItemIndicator>
130 </span>
131 <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
132 </SelectPrimitive.Item>
133))
134SelectItem.displayName = SelectPrimitive.Item.displayName
135
136const SelectSeparator = React.forwardRef<
137 React.ElementRef<typeof SelectPrimitive.Separator>,
138 React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
139>(({ className, ...props }, ref) => (
140 <SelectPrimitive.Separator
141 ref={ref}
142 className={cn("-mx-1 my-1 h-px bg-muted", className)}
143 {...props}
144 />
145))
146SelectSeparator.displayName = SelectPrimitive.Separator.displayName
147
148export {
149 Select,
150 SelectGroup,
151 SelectValue,
152 SelectTrigger,
153 SelectContent,
154 SelectLabel,
155 SelectItem,
156 SelectSeparator,
157 SelectScrollUpButton,
158 SelectScrollDownButton,
159}