select.tsx 6.1 KB

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