select.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use client";
  2. import * as React from "react";
  3. import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react";
  4. import { Select as SelectPrimitive } from "radix-ui";
  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 = "item-aligned",
  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
  109. data-slot="select-item-indicator"
  110. className="absolute right-2 flex size-3.5 items-center justify-center"
  111. >
  112. <SelectPrimitive.ItemIndicator>
  113. <CheckIcon className="size-4" />
  114. </SelectPrimitive.ItemIndicator>
  115. </span>
  116. <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
  117. </SelectPrimitive.Item>
  118. );
  119. }
  120. function SelectSeparator({
  121. className,
  122. ...props
  123. }: React.ComponentProps<typeof SelectPrimitive.Separator>) {
  124. return (
  125. <SelectPrimitive.Separator
  126. data-slot="select-separator"
  127. className={cn("bg-border pointer-events-none -mx-1 my-1 h-px", className)}
  128. {...props}
  129. />
  130. );
  131. }
  132. function SelectScrollUpButton({
  133. className,
  134. ...props
  135. }: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
  136. return (
  137. <SelectPrimitive.ScrollUpButton
  138. data-slot="select-scroll-up-button"
  139. className={cn(
  140. "flex cursor-default items-center justify-center py-1",
  141. className,
  142. )}
  143. {...props}
  144. >
  145. <ChevronUpIcon className="size-4" />
  146. </SelectPrimitive.ScrollUpButton>
  147. );
  148. }
  149. function SelectScrollDownButton({
  150. className,
  151. ...props
  152. }: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
  153. return (
  154. <SelectPrimitive.ScrollDownButton
  155. data-slot="select-scroll-down-button"
  156. className={cn(
  157. "flex cursor-default items-center justify-center py-1",
  158. className,
  159. )}
  160. {...props}
  161. >
  162. <ChevronDownIcon className="size-4" />
  163. </SelectPrimitive.ScrollDownButton>
  164. );
  165. }
  166. export {
  167. Select,
  168. SelectContent,
  169. SelectGroup,
  170. SelectItem,
  171. SelectLabel,
  172. SelectScrollDownButton,
  173. SelectScrollUpButton,
  174. SelectSeparator,
  175. SelectTrigger,
  176. SelectValue,
  177. };