select.tsx
3,176 bytes
| 1 | 'use client'; |
|---|---|
| 2 | |
| 3 | import * as SelectPrimitive from '@radix-ui/react-select'; |
| 4 | import { Check, ChevronDown } from 'lucide-react'; |
| 5 | import * as React from 'react'; |
| 6 | import { cn } from '@/lib/utils'; |
| 7 | |
| 8 | const Select = SelectPrimitive.Root; |
| 9 | const SelectGroup = SelectPrimitive.Group; |
| 10 | const SelectValue = SelectPrimitive.Value; |
| 11 | |
| 12 | const SelectTrigger = React.forwardRef< |
| 13 | React.ElementRef<typeof SelectPrimitive.Trigger>, |
| 14 | React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> |
| 15 | >(({ className, children, ...props }, ref) => ( |
| 16 | <SelectPrimitive.Trigger |
| 17 | ref={ref} |
| 18 | className={cn( |
| 19 | 'flex h-9 w-full items-center justify-between rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1', |
| 20 | className, |
| 21 | )} |
| 22 | {...props} |
| 23 | > |
| 24 | {children} |
| 25 | <SelectPrimitive.Icon asChild> |
| 26 | <ChevronDown className="h-4 w-4 opacity-50" /> |
| 27 | </SelectPrimitive.Icon> |
| 28 | </SelectPrimitive.Trigger> |
| 29 | )); |
| 30 | SelectTrigger.displayName = SelectPrimitive.Trigger.displayName; |
| 31 | |
| 32 | const SelectContent = React.forwardRef< |
| 33 | React.ElementRef<typeof SelectPrimitive.Content>, |
| 34 | React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content> |
| 35 | >(({ className, children, position = 'popper', ...props }, ref) => ( |
| 36 | <SelectPrimitive.Portal> |
| 37 | <SelectPrimitive.Content |
| 38 | ref={ref} |
| 39 | className={cn( |
| 40 | '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', |
| 41 | position === 'popper' && 'data-[side=bottom]:translate-y-1', |
| 42 | className, |
| 43 | )} |
| 44 | position={position} |
| 45 | {...props} |
| 46 | > |
| 47 | <SelectPrimitive.Viewport |
| 48 | className={cn('p-1', position === 'popper' && 'w-full min-w-[var(--radix-select-trigger-width)]')} |
| 49 | > |
| 50 | {children} |
| 51 | </SelectPrimitive.Viewport> |
| 52 | </SelectPrimitive.Content> |
| 53 | </SelectPrimitive.Portal> |
| 54 | )); |
| 55 | SelectContent.displayName = SelectPrimitive.Content.displayName; |
| 56 | |
| 57 | const SelectItem = React.forwardRef< |
| 58 | React.ElementRef<typeof SelectPrimitive.Item>, |
| 59 | React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item> |
| 60 | >(({ className, children, ...props }, ref) => ( |
| 61 | <SelectPrimitive.Item |
| 62 | ref={ref} |
| 63 | className={cn( |
| 64 | 'relative flex w-full cursor-pointer select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50', |
| 65 | className, |
| 66 | )} |
| 67 | {...props} |
| 68 | > |
| 69 | <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center"> |
| 70 | <SelectPrimitive.ItemIndicator> |
| 71 | <Check className="h-4 w-4" /> |
| 72 | </SelectPrimitive.ItemIndicator> |
| 73 | </span> |
| 74 | <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText> |
| 75 | </SelectPrimitive.Item> |
| 76 | )); |
| 77 | SelectItem.displayName = SelectPrimitive.Item.displayName; |
| 78 | |
| 79 | export { Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectItem }; |
| 80 | |