select.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use client';
  2. import * as React from 'react';
  3. import { Select as SelectPrimitive } from 'radix-ui';
  4. import { cn } from '@/lib/utils';
  5. import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from 'lucide-react';
  6. function Select({ ...props }: React.ComponentProps<typeof SelectPrimitive.Root>) {
  7. return <SelectPrimitive.Root data-slot="select" {...props} />;
  8. }
  9. function SelectGroup({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.Group>) {
  10. return (
  11. <SelectPrimitive.Group
  12. data-slot="select-group"
  13. className={cn('scroll-my-1 p-1', className)}
  14. {...props}
  15. />
  16. );
  17. }
  18. function SelectValue({ ...props }: 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 dark:bg-input/30 dark:hover:bg-input/50 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:aria-invalid:border-destructive/50 gap-1.5 rounded-md border bg-transparent py-2 pr-2 pl-2.5 text-sm shadow-xs transition-[color,box-shadow] focus-visible:ring-[3px] aria-invalid:ring-[3px] data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:flex *:data-[slot=select-value]:gap-1.5 [&_svg:not([class*='size-'])]:size-4 flex w-fit items-center justify-between whitespace-nowrap 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 [&_svg]:pointer-events-none [&_svg]:shrink-0",
  35. className,
  36. )}
  37. {...props}
  38. >
  39. {children}
  40. <SelectPrimitive.Icon asChild>
  41. <ChevronDownIcon className="text-muted-foreground size-4 pointer-events-none" />
  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-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-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 ring-foreground/10 min-w-36 rounded-md shadow-md ring-1 duration-100 relative z-50 max-h-(--radix-select-content-available-height) origin-(--radix-select-content-transform-origin) overflow-x-hidden overflow-y-auto',
  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. data-position={position}
  70. className={cn(
  71. 'data-[position=popper]:h-[var(--radix-select-trigger-height)] data-[position=popper]:w-full data-[position=popper]:min-w-[var(--radix-select-trigger-width)]',
  72. position === 'popper' && '',
  73. )}
  74. >
  75. {children}
  76. </SelectPrimitive.Viewport>
  77. <SelectScrollDownButton />
  78. </SelectPrimitive.Content>
  79. </SelectPrimitive.Portal>
  80. );
  81. }
  82. function SelectLabel({ className, ...props }: 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 not-data-[variant=destructive]:focus:**:text-accent-foreground gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2 relative flex w-full cursor-default items-center outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
  101. className,
  102. )}
  103. {...props}
  104. >
  105. <span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center">
  106. <SelectPrimitive.ItemIndicator>
  107. <CheckIcon className="pointer-events-none" />
  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 -mx-1 my-1 h-px pointer-events-none', 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. "bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4",
  135. className,
  136. )}
  137. {...props}
  138. >
  139. <ChevronUpIcon />
  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. "bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4",
  152. className,
  153. )}
  154. {...props}
  155. >
  156. <ChevronDownIcon />
  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. };