field.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use client';
  2. import { useMemo } from 'react';
  3. import { cva, type VariantProps } from 'class-variance-authority';
  4. import { cn } from '@/lib/utils';
  5. import { Label } from '@/components/ui/label';
  6. import { Separator } from '@/components/ui/separator';
  7. function FieldSet({ className, ...props }: React.ComponentProps<'fieldset'>) {
  8. return (
  9. <fieldset
  10. data-slot="field-set"
  11. className={cn(
  12. 'gap-6 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3 flex flex-col',
  13. className,
  14. )}
  15. {...props}
  16. />
  17. );
  18. }
  19. function FieldLegend({
  20. className,
  21. variant = 'legend',
  22. ...props
  23. }: React.ComponentProps<'legend'> & { variant?: 'legend' | 'label' }) {
  24. return (
  25. <legend
  26. data-slot="field-legend"
  27. data-variant={variant}
  28. className={cn(
  29. 'mb-3 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base',
  30. className,
  31. )}
  32. {...props}
  33. />
  34. );
  35. }
  36. function FieldGroup({ className, ...props }: React.ComponentProps<'div'>) {
  37. return (
  38. <div
  39. data-slot="field-group"
  40. className={cn(
  41. 'gap-7 data-[slot=checkbox-group]:gap-3 [&>[data-slot=field-group]]:gap-4 group/field-group @container/field-group flex w-full flex-col',
  42. className,
  43. )}
  44. {...props}
  45. />
  46. );
  47. }
  48. const fieldVariants = cva('data-[invalid=true]:text-destructive gap-3 group/field flex w-full', {
  49. variants: {
  50. orientation: {
  51. vertical: 'flex-col [&>*]:w-full [&>.sr-only]:w-auto',
  52. horizontal:
  53. 'flex-row items-center [&>[data-slot=field-label]]:flex-auto has-[>[data-slot=field-content]]:items-start has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px',
  54. responsive:
  55. 'flex-col [&>*]:w-full [&>.sr-only]:w-auto @md/field-group:flex-row @md/field-group:items-center @md/field-group:[&>*]:w-auto @md/field-group:[&>[data-slot=field-label]]:flex-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px',
  56. },
  57. },
  58. defaultVariants: {
  59. orientation: 'vertical',
  60. },
  61. });
  62. function Field({
  63. className,
  64. orientation = 'vertical',
  65. ...props
  66. }: React.ComponentProps<'div'> & VariantProps<typeof fieldVariants>) {
  67. return (
  68. <div
  69. role="group"
  70. data-slot="field"
  71. data-orientation={orientation}
  72. className={cn(fieldVariants({ orientation }), className)}
  73. {...props}
  74. />
  75. );
  76. }
  77. function FieldContent({ className, ...props }: React.ComponentProps<'div'>) {
  78. return (
  79. <div
  80. data-slot="field-content"
  81. className={cn('gap-1 group/field-content flex flex-1 flex-col leading-snug', className)}
  82. {...props}
  83. />
  84. );
  85. }
  86. function FieldLabel({ className, ...props }: React.ComponentProps<typeof Label>) {
  87. return (
  88. <Label
  89. data-slot="field-label"
  90. className={cn(
  91. 'has-data-checked:bg-primary/5 has-data-checked:border-primary dark:has-data-checked:bg-primary/10 gap-2 group-data-[disabled=true]/field:opacity-50 has-[>[data-slot=field]]:rounded-md has-[>[data-slot=field]]:border [&>*]:data-[slot=field]:p-3 group/field-label peer/field-label flex w-fit leading-snug',
  92. 'has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col',
  93. className,
  94. )}
  95. {...props}
  96. />
  97. );
  98. }
  99. function FieldTitle({ className, ...props }: React.ComponentProps<'div'>) {
  100. return (
  101. <div
  102. data-slot="field-label"
  103. className={cn(
  104. 'gap-2 text-sm font-medium group-data-[disabled=true]/field:opacity-50 flex w-fit items-center leading-snug',
  105. className,
  106. )}
  107. {...props}
  108. />
  109. );
  110. }
  111. function FieldDescription({ className, ...props }: React.ComponentProps<'p'>) {
  112. return (
  113. <p
  114. data-slot="field-description"
  115. className={cn(
  116. 'text-muted-foreground text-left text-sm [[data-variant=legend]+&]:-mt-1.5 leading-normal font-normal group-has-[[data-orientation=horizontal]]/field:text-balance',
  117. 'last:mt-0 nth-last-2:-mt-1',
  118. '[&>a:hover]:text-primary [&>a]:underline [&>a]:underline-offset-4',
  119. className,
  120. )}
  121. {...props}
  122. />
  123. );
  124. }
  125. function FieldSeparator({
  126. children,
  127. className,
  128. ...props
  129. }: React.ComponentProps<'div'> & {
  130. children?: React.ReactNode;
  131. }) {
  132. return (
  133. <div
  134. data-slot="field-separator"
  135. data-content={!!children}
  136. className={cn(
  137. '-my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2 relative',
  138. className,
  139. )}
  140. {...props}
  141. >
  142. <Separator className="absolute inset-0 top-1/2" />
  143. {children && (
  144. <span
  145. className="text-muted-foreground px-2 bg-background relative mx-auto block w-fit"
  146. data-slot="field-separator-content"
  147. >
  148. {children}
  149. </span>
  150. )}
  151. </div>
  152. );
  153. }
  154. function FieldError({
  155. className,
  156. children,
  157. errors,
  158. ...props
  159. }: React.ComponentProps<'div'> & {
  160. errors?: Array<{ message?: string } | undefined>;
  161. }) {
  162. const content = useMemo(() => {
  163. if (children) {
  164. return children;
  165. }
  166. if (!errors?.length) {
  167. return null;
  168. }
  169. const uniqueErrors = [...new Map(errors.map((error) => [error?.message, error])).values()];
  170. if (uniqueErrors?.length == 1) {
  171. return uniqueErrors[0]?.message;
  172. }
  173. return (
  174. <ul className="ml-4 flex list-disc flex-col gap-1">
  175. {uniqueErrors.map((error, index) => error?.message && <li key={index}>{error.message}</li>)}
  176. </ul>
  177. );
  178. }, [children, errors]);
  179. if (!content) {
  180. return null;
  181. }
  182. return (
  183. <div
  184. role="alert"
  185. data-slot="field-error"
  186. className={cn('text-destructive text-sm font-normal', className)}
  187. {...props}
  188. >
  189. {content}
  190. </div>
  191. );
  192. }
  193. export {
  194. Field,
  195. FieldLabel,
  196. FieldDescription,
  197. FieldError,
  198. FieldGroup,
  199. FieldLegend,
  200. FieldSeparator,
  201. FieldSet,
  202. FieldContent,
  203. FieldTitle,
  204. };