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