select.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. ...props
  51. }: React.ComponentProps<typeof SelectPrimitive.Content>) {
  52. return (
  53. <SelectPrimitive.Portal>
  54. <SelectPrimitive.Content
  55. data-slot="select-content"
  56. className={cn(
  57. "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",
  58. position === "popper" &&
  59. "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
  60. className
  61. )}
  62. position={position}
  63. {...props}
  64. >
  65. <SelectScrollUpButton />
  66. <SelectPrimitive.Viewport
  67. className={cn(
  68. "p-1",
  69. position === "popper" &&
  70. "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)] scroll-my-1"
  71. )}
  72. >
  73. {children}
  74. </SelectPrimitive.Viewport>
  75. <SelectScrollDownButton />
  76. </SelectPrimitive.Content>
  77. </SelectPrimitive.Portal>
  78. )
  79. }
  80. function SelectLabel({
  81. className,
  82. ...props
  83. }: React.ComponentProps<typeof SelectPrimitive.Label>) {
  84. return (
  85. <SelectPrimitive.Label
  86. data-slot="select-label"
  87. className={cn("text-muted-foreground px-2 py-1.5 text-xs", className)}
  88. {...props}
  89. />
  90. )
  91. }
  92. function SelectItem({
  93. className,
  94. children,
  95. ...props
  96. }: React.ComponentProps<typeof SelectPrimitive.Item>) {
  97. return (
  98. <SelectPrimitive.Item
  99. data-slot="select-item"
  100. className={cn(
  101. "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",
  102. className
  103. )}
  104. {...props}
  105. >
  106. <span className="absolute right-2 flex size-3.5 items-center justify-center">
  107. <SelectPrimitive.ItemIndicator>
  108. <CheckIcon className="size-4" />
  109. </SelectPrimitive.ItemIndicator>
  110. </span>
  111. <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
  112. </SelectPrimitive.Item>
  113. )
  114. }
  115. function SelectSeparator({
  116. className,
  117. ...props
  118. }: React.ComponentProps<typeof SelectPrimitive.Separator>) {
  119. return (
  120. <SelectPrimitive.Separator
  121. data-slot="select-separator"
  122. className={cn("bg-border pointer-events-none -mx-1 my-1 h-px", className)}
  123. {...props}
  124. />
  125. )
  126. }
  127. function SelectScrollUpButton({
  128. className,
  129. ...props
  130. }: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
  131. return (
  132. <SelectPrimitive.ScrollUpButton
  133. data-slot="select-scroll-up-button"
  134. className={cn(
  135. "flex cursor-default items-center justify-center py-1",
  136. className
  137. )}
  138. {...props}
  139. >
  140. <ChevronUpIcon className="size-4" />
  141. </SelectPrimitive.ScrollUpButton>
  142. )
  143. }
  144. function SelectScrollDownButton({
  145. className,
  146. ...props
  147. }: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
  148. return (
  149. <SelectPrimitive.ScrollDownButton
  150. data-slot="select-scroll-down-button"
  151. className={cn(
  152. "flex cursor-default items-center justify-center py-1",
  153. className
  154. )}
  155. {...props}
  156. >
  157. <ChevronDownIcon className="size-4" />
  158. </SelectPrimitive.ScrollDownButton>
  159. )
  160. }
  161. export {
  162. Select,
  163. SelectContent,
  164. SelectGroup,
  165. SelectItem,
  166. SelectLabel,
  167. SelectScrollDownButton,
  168. SelectScrollUpButton,
  169. SelectSeparator,
  170. SelectTrigger,
  171. SelectValue,
  172. }