xonecas.io
5414
Sa

sat / files

sat / src / components / ui / field.tsx

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