Form
Forms
Form components integrated with react-hook-form.
Installation
npm install react-hook-form @hookform/resolvers zodImport
import { PixelForm, PixelFormField, PixelFormItem, PixelFormLabel, PixelFormControl, PixelFormMessage } from "@/components/ui/pixel-form"Usage
Interactive preview available - see usage code below
Component Source
Copy and paste the following code into your project at /src/components/ui/pixel-form.tsx
"use client";
import type * as LabelPrimitive from "@radix-ui/react-label";
import { Slot } from "@radix-ui/react-slot";
import * as React from "react";
import {
Controller,
type ControllerProps,
type FieldPath,
type FieldValues,
FormProvider,
useFormContext,
} from "react-hook-form";
import { cn } from "@/lib/utils";
import { PixelLabel } from "./pixel-label";
const PixelForm = FormProvider;
type FormFieldContextValue<
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
> = {
name: TName;
};
const FormFieldContext = React.createContext<FormFieldContextValue>(
{} as FormFieldContextValue,
);
const PixelFormField = <
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>({
...props
}: ControllerProps<TFieldValues, TName>) => {
return (
<FormFieldContext.Provider value={{ name: props.name }}>
<Controller {...props} />
</FormFieldContext.Provider>
);
};
const useFormField = () => {
const fieldContext = React.useContext(FormFieldContext);
const itemContext = React.useContext(FormItemContext);
const { getFieldState, formState } = useFormContext();
const fieldState = getFieldState(fieldContext.name, formState);
if (!fieldContext) {
// ... (more code below)Accessibility
This component is built with accessibility in mind, including proper ARIA attributes, keyboard navigation, and focus management.