Form

Advanced form state management using react-hook-form and Zod, with built-in loading and feedback overlays.

This component requires the FyskProvider. See Quick Start →

Preview

Form
TypeScript

Stay Updated

Join our newsletter for the latest design updates.

Installation

bash
bash
1
npx shadcn@latest add https://fysk.dev/r/form.json

Usage

tsx
tsx
1
import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui"
2
import { useForm } from "react-hook-form"
3
4
const form = useForm({ ... })
5
6
<Form onSubmit={form.handleSubmit(onSubmit)}>
7
<FormField
8
control={form.control}
9
name="username"
10
render={({ field }) => (
11
<FormItem>
12
<FormLabel>Username</FormLabel>
13
<FormControl><Input {...field} /></FormControl>
14
<FormMessage />
15
</FormItem>
16
)}
17
/>
18
<Button type="submit">Submit</Button>
19
</Form>

Examples

Complete Feedback Loop

Showcasing the built-in overlays for submission and success messages.

Component
TypeScript

Create an account

Enter your details below to create your account

Must be at least 8 characters long.

Features

  • Integrated State: The state prop handles loading, success, and error globally for the form, including blur/overlay effects.

  • Auto-Aria: Components like FormControl and FormMessage automatically sync IDs and aria-describedby for a11y.

  • Slot Implementation: Uses Radix Slot in FormControl to safely inject props into any child input component.

  • Hook Integration: Designed specifically to work with react-hook-form and zod for declarative validation.

Props

Form

PropTypeDefaultDescription
state"idle" | "loading" | "success" | "error""idle"The visual submission state.
errorReact.ReactNode-A manual/server error message to display at the top level.
afterSubmissionReact.ReactNode-Content/Message to show in a centered overlay on success.
onCloseAfterSubmission() => void-Callback when the success overlay is closed.
LoadingIconReact.ReactNode-Custom icon for the loading state.
SuccessIconReact.ReactNode-Custom icon for the success overlay.
ErrorIconReact.ReactNode-Custom icon for the error state.
CloseIconReact.ReactNode-Custom icon for closing the success overlay.

FormField

A wrapper around react-hook-form's Controller that provides context to child components.

PropTypeDefaultDescription
controlControl-The form control object from useForm().
namestring-The field name (must match a key in your form schema).
render({ field, fieldState, formState }) => React.ReactElement-Render function that receives field props and state.

FormItem

A container for a single form field. Provides spacing and context for associating labels, descriptions, and error messages.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for custom styling.

FormLabel

A label for form inputs. Automatically associates with the input via the FormItem context and shows error styling when validation fails.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for custom styling.

FormControl

Wraps form inputs to provide proper accessibility attributes. Uses Radix Slot to merge props with the child element.

When used within a FormField, automatically adds unique ID for label association, aria-describedby for description and error message, and aria-invalid when field has validation errors.

FormDescription

Provides additional context or instructions for a form field. Automatically associated with the input via aria-describedby.

PropTypeDefaultDescription
classNamestring-Additional CSS classes for custom styling.

FormMessage

Displays validation error messages for a form field. When used within a FormField, automatically displays the error message from react-hook-form validation.

PropTypeDefaultDescription
state"error" | "success""error"The visual style of the message.
childrenReact.ReactNode-Custom message content (overrides auto-detected error).

Hooks

useFormField

Hook to access the current form field's state and metadata. Must be used within a FormField and FormItem component.

Returns:

PropertyTypeDescription
idstringThe unique ID for the form item.
namestringThe field name registered with react-hook-form.
formItemIdstringID for the form item element.
formDescriptionIdstringID for the description element (for aria-describedby).
formMessageIdstringID for the error message element.
errorFieldError | undefinedThe current field error (if any).
invalidbooleanWhether the field has a validation error.
isDirtybooleanWhether the field value has been modified.
isTouchedbooleanWhether the field has been touched/focused.

All components accept standard props from their respective Radix and HTML elements.

Built with 💖 | Created by Yashraj