|
| 1 | +import * as SelectPrimitive from '@radix-ui/react-select'; |
| 2 | +import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from 'lucide-react'; |
| 3 | + |
| 4 | +import { cn } from './utils'; |
| 5 | + |
| 6 | +function Select({ ...props }) { |
| 7 | + return <SelectPrimitive.Root data-slot="select" {...props} />; |
| 8 | +} |
| 9 | + |
| 10 | +function SelectGroup({ ...props }) { |
| 11 | + return <SelectPrimitive.Group data-slot="select-group" {...props} />; |
| 12 | +} |
| 13 | + |
| 14 | +function SelectValue({ ...props }) { |
| 15 | + return <SelectPrimitive.Value data-slot="select-value" {...props} />; |
| 16 | +} |
| 17 | + |
| 18 | +function SelectTrigger({ className, size = 'default', children, ...props }) { |
| 19 | + return ( |
| 20 | + <SelectPrimitive.Trigger |
| 21 | + data-slot="select-trigger" |
| 22 | + data-size={size} |
| 23 | + className={cn( |
| 24 | + 'flex w-full items-center justify-between gap-2 min-h-[44px] px-4 py-3 bg-white border border-gray-200 rounded-xl', |
| 25 | + 'text-sm whitespace-nowrap', |
| 26 | + 'data-[placeholder]:text-gray-400', |
| 27 | + 'focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent', |
| 28 | + 'disabled:bg-gray-50 disabled:cursor-not-allowed disabled:opacity-50', |
| 29 | + 'transition-[color,box-shadow]', |
| 30 | + "[&_svg:not([class*='text-'])]:text-gray-400", |
| 31 | + '*:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2', |
| 32 | + "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", |
| 33 | + className |
| 34 | + )} |
| 35 | + {...props} |
| 36 | + > |
| 37 | + {children} |
| 38 | + <SelectPrimitive.Icon asChild> |
| 39 | + <ChevronDownIcon className="size-4 opacity-50" /> |
| 40 | + </SelectPrimitive.Icon> |
| 41 | + </SelectPrimitive.Trigger> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function SelectContent({ className, children, position = 'popper', ...props }) { |
| 46 | + return ( |
| 47 | + <SelectPrimitive.Portal> |
| 48 | + <SelectPrimitive.Content |
| 49 | + data-slot="select-content" |
| 50 | + className={cn( |
| 51 | + 'bg-popover text-popover-foreground 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 relative z-50 max-h-(--radix-select-content-available-height) min-w-[8rem] origin-(--radix-select-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border shadow-md', |
| 52 | + position === 'popper' && |
| 53 | + 'data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1', |
| 54 | + className |
| 55 | + )} |
| 56 | + position={position} |
| 57 | + {...props} |
| 58 | + > |
| 59 | + <SelectScrollUpButton /> |
| 60 | + <SelectPrimitive.Viewport |
| 61 | + className={cn( |
| 62 | + 'p-1', |
| 63 | + position === 'popper' && |
| 64 | + 'h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)] scroll-my-1' |
| 65 | + )} |
| 66 | + > |
| 67 | + {children} |
| 68 | + </SelectPrimitive.Viewport> |
| 69 | + <SelectScrollDownButton /> |
| 70 | + </SelectPrimitive.Content> |
| 71 | + </SelectPrimitive.Portal> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +function SelectLabel({ className, ...props }) { |
| 76 | + return ( |
| 77 | + <SelectPrimitive.Label |
| 78 | + data-slot="select-label" |
| 79 | + className={cn('text-muted-foreground px-2 py-1.5 text-xs', className)} |
| 80 | + {...props} |
| 81 | + /> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function SelectItem({ className, children, ...props }) { |
| 86 | + return ( |
| 87 | + <SelectPrimitive.Item |
| 88 | + data-slot="select-item" |
| 89 | + className={cn( |
| 90 | + "focus:bg-accent focus:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2", |
| 91 | + className |
| 92 | + )} |
| 93 | + {...props} |
| 94 | + > |
| 95 | + <span className="absolute right-2 flex size-3.5 items-center justify-center"> |
| 96 | + <SelectPrimitive.ItemIndicator> |
| 97 | + <CheckIcon className="size-4" /> |
| 98 | + </SelectPrimitive.ItemIndicator> |
| 99 | + </span> |
| 100 | + <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText> |
| 101 | + </SelectPrimitive.Item> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +function SelectSeparator({ className, ...props }) { |
| 106 | + return ( |
| 107 | + <SelectPrimitive.Separator |
| 108 | + data-slot="select-separator" |
| 109 | + className={cn('bg-border pointer-events-none -mx-1 my-1 h-px', className)} |
| 110 | + {...props} |
| 111 | + /> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +function SelectScrollUpButton({ className, ...props }) { |
| 116 | + return ( |
| 117 | + <SelectPrimitive.ScrollUpButton |
| 118 | + data-slot="select-scroll-up-button" |
| 119 | + className={cn( |
| 120 | + 'flex cursor-default items-center justify-center py-1', |
| 121 | + className |
| 122 | + )} |
| 123 | + {...props} |
| 124 | + > |
| 125 | + <ChevronUpIcon className="size-4" /> |
| 126 | + </SelectPrimitive.ScrollUpButton> |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +function SelectScrollDownButton({ className, ...props }) { |
| 131 | + return ( |
| 132 | + <SelectPrimitive.ScrollDownButton |
| 133 | + data-slot="select-scroll-down-button" |
| 134 | + className={cn( |
| 135 | + 'flex cursor-default items-center justify-center py-1', |
| 136 | + className |
| 137 | + )} |
| 138 | + {...props} |
| 139 | + > |
| 140 | + <ChevronDownIcon className="size-4" /> |
| 141 | + </SelectPrimitive.ScrollDownButton> |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +export { |
| 146 | + Select, |
| 147 | + SelectContent, |
| 148 | + SelectGroup, |
| 149 | + SelectItem, |
| 150 | + SelectLabel, |
| 151 | + SelectScrollDownButton, |
| 152 | + SelectScrollUpButton, |
| 153 | + SelectSeparator, |
| 154 | + SelectTrigger, |
| 155 | + SelectValue, |
| 156 | +}; |
0 commit comments