Skip to content

Commit c93a811

Browse files
committed
feat/DXDP-5245 | Added Form component from UDS
1 parent ba72ea4 commit c93a811

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

src/components/ui/form.tsx

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"use client";
2+
3+
import { Label } from "@/components/ui/label";
4+
import { Field as FieldPrimitive } from "@base-ui-components/react";
5+
import {
6+
Controller,
7+
FormProvider,
8+
useFormContext,
9+
useFormState,
10+
type ControllerProps,
11+
type FieldPath,
12+
type FieldValues,
13+
} from "react-hook-form";
14+
import * as React from "react";
15+
import { cn } from "@/lib/utils";
16+
17+
const Form = FormProvider;
18+
19+
type FormFieldContextValue<
20+
TFieldValues extends FieldValues = FieldValues,
21+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
22+
> = {
23+
name: TName;
24+
};
25+
26+
const FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue);
27+
28+
const FormField = <
29+
TFieldValues extends FieldValues = FieldValues,
30+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
31+
>({
32+
...props
33+
}: ControllerProps<TFieldValues, TName>) => {
34+
return (
35+
<FormFieldContext.Provider value={{ name: props.name }}>
36+
<Controller {...props} />
37+
</FormFieldContext.Provider>
38+
);
39+
};
40+
41+
const useFormField = () => {
42+
const fieldContext = React.useContext(FormFieldContext);
43+
const itemContext = React.useContext(FormItemContext);
44+
const { getFieldState } = useFormContext();
45+
const formState = useFormState({ name: fieldContext.name });
46+
const fieldState = getFieldState(fieldContext.name, formState);
47+
48+
if (!fieldContext) {
49+
throw new Error("useFormField should be used within <FormField>");
50+
}
51+
52+
const { id } = itemContext;
53+
54+
return {
55+
id,
56+
name: fieldContext.name,
57+
formItemId: `${id}-form-item`,
58+
formDescriptionId: `${id}-form-item-description`,
59+
formMessageId: `${id}-form-item-message`,
60+
...fieldState,
61+
};
62+
};
63+
64+
type FormItemContextValue = {
65+
id: string;
66+
};
67+
68+
const FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue);
69+
70+
function FormItem({ className, ...props }: React.ComponentProps<"div">) {
71+
const id = React.useId();
72+
73+
return (
74+
<FormItemContext.Provider value={{ id }}>
75+
<FieldPrimitive.Root data-slot="form-item" className={cn("grid gap-2", className)} {...props} />
76+
</FormItemContext.Provider>
77+
);
78+
}
79+
80+
function FormLabel({ className, ...props }: React.ComponentProps<typeof FieldPrimitive.Label>) {
81+
const { error, formItemId } = useFormField();
82+
83+
return (
84+
<Label
85+
data-slot="form-label"
86+
data-error={!!error}
87+
className={cn("data-[error=true]:text-destructive", className)}
88+
htmlFor={formItemId}
89+
{...props}
90+
/>
91+
);
92+
}
93+
94+
function FormControl({ children, ...props }: React.ComponentProps<typeof FieldPrimitive.Control>) {
95+
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
96+
97+
return (
98+
<FieldPrimitive.Control
99+
data-slot="form-control"
100+
id={formItemId}
101+
aria-describedby={!error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`}
102+
aria-invalid={!!error}
103+
render={children as React.ReactElement}
104+
{...props}
105+
/>
106+
);
107+
}
108+
109+
function FormDescription({ className, ...props }: React.ComponentProps<"p">) {
110+
const { formDescriptionId } = useFormField();
111+
112+
return (
113+
<p
114+
data-slot="form-description"
115+
id={formDescriptionId}
116+
className={cn("text-muted-foreground text-sm", className)}
117+
{...props}
118+
/>
119+
);
120+
}
121+
122+
function FormMessage({ className, ...props }: React.ComponentProps<"p">) {
123+
const { error, formMessageId } = useFormField();
124+
const body = error ? String(error?.message ?? "") : props.children;
125+
126+
if (!body) {
127+
return null;
128+
}
129+
130+
return (
131+
<p data-slot="form-message" id={formMessageId} className={cn("text-destructive text-sm", className)} {...props}>
132+
{body}
133+
</p>
134+
);
135+
}
136+
137+
export { useFormField, Form, FormItem, FormLabel, FormControl, FormDescription, FormMessage, FormField };

0 commit comments

Comments
 (0)