Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

field.tsx

246 lines6.1 KB

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