xonecas.io
556
Bn

bees-nest / files

bees-nest / 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<"fieldset"> & VariantProps<typeof fieldVariants>) {
76  return (
77    <fieldset
78      data-slot="field"
79      data-orientation={orientation}
80      className={cn(fieldVariants({ orientation }), className)}
81      {...props}
82    />
83  );
84}
85
86function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
87  return (
88    <div
89      data-slot="field-content"
90      className={cn(
91        "gap-1 group/field-content flex flex-1 flex-col leading-snug",
92        className,
93      )}
94      {...props}
95    />
96  );
97}
98
99function FieldLabel({
100  className,
101  ...props
102}: React.ComponentProps<typeof Label>) {
103  return (
104    <Label
105      data-slot="field-label"
106      className={cn(
107        "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",
108        "has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
109        className,
110      )}
111      {...props}
112    />
113  );
114}
115
116function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
117  return (
118    <div
119      data-slot="field-label"
120      className={cn(
121        "gap-2 text-sm font-medium group-data-[disabled=true]/field:opacity-50 flex w-fit items-center leading-snug",
122        className,
123      )}
124      {...props}
125    />
126  );
127}
128
129function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
130  return (
131    <p
132      data-slot="field-description"
133      className={cn(
134        "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",
135        "last:mt-0 nth-last-2:-mt-1",
136        "[&>a:hover]:text-primary [&>a]:underline [&>a]:underline-offset-4",
137        className,
138      )}
139      {...props}
140    />
141  );
142}
143
144function FieldSeparator({
145  children,
146  className,
147  ...props
148}: React.ComponentProps<"div"> & {
149  children?: React.ReactNode;
150}) {
151  return (
152    <div
153      data-slot="field-separator"
154      data-content={!!children}
155      className={cn(
156        "-my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2 relative",
157        className,
158      )}
159      {...props}
160    >
161      <Separator className="absolute inset-0 top-1/2" />
162      {children && (
163        <span
164          className="text-muted-foreground px-2 bg-background relative mx-auto block w-fit"
165          data-slot="field-separator-content"
166        >
167          {children}
168        </span>
169      )}
170    </div>
171  );
172}
173
174function FieldError({
175  className,
176  children,
177  errors,
178  ...props
179}: React.ComponentProps<"div"> & {
180  errors?: Array<{ message?: string } | undefined>;
181}) {
182  const content = useMemo(() => {
183    if (children) {
184      return children;
185    }
186
187    if (!errors?.length) {
188      return null;
189    }
190
191    const uniqueErrors = [
192      ...new Map(errors.map((error) => [error?.message, error])).values(),
193    ];
194
195    if (uniqueErrors?.length === 1) {
196      return uniqueErrors[0]?.message;
197    }
198
199    return (
200      <ul className="ml-4 flex list-disc flex-col gap-1">
201        {uniqueErrors.map(
202          (error) =>
203            error?.message && <li key={error.message}>{error.message}</li>,
204        )}
205      </ul>
206    );
207  }, [children, errors]);
208
209  if (!content) {
210    return null;
211  }
212
213  return (
214    <div
215      role="alert"
216      data-slot="field-error"
217      className={cn("text-destructive text-sm font-normal", className)}
218      {...props}
219    >
220      {content}
221    </div>
222  );
223}
224
225export {
226  Field,
227  FieldLabel,
228  FieldDescription,
229  FieldError,
230  FieldGroup,
231  FieldLegend,
232  FieldSeparator,
233  FieldSet,
234  FieldContent,
235  FieldTitle,
236};