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