select.tsx 6.1 KB

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