NOCTA UI

Form

A composable form component with field validation, labels, and actions for building accessible forms

Installation

Install the Form component using the nocta-ui CLI:

Terminal
npx nocta-ui add form

Then import the component:

Import
import { 
  Form, 
  FormField, 
  FormLabel, 
  FormControl, 
  FormDescription, 
  FormMessage, 
  FormActions 
} from '@/components/ui/form';

Basic Usage

The Form component follows a composable architecture where you build forms by combining smaller components together.

This will be displayed on your profile.

Form Architecture

The Form component is built with a composable architecture:

  • Form: The main form container
  • FormField: Wraps individual form fields and provides context
  • FormLabel: Accessible labels with required field indicators
  • FormControl: Wrapper that connects inputs to the field context
  • FormDescription: Helper text for additional guidance
  • FormMessage: Error, success, and warning messages
  • FormActions: Container for form action buttons

Error Handling

Handle form validation errors by passing an error prop to FormField. The error message will automatically be displayed using FormMessage.

Must be at least 8 characters long.

Complex Forms

Build complex forms with multiple field types, layouts, and validation.

We'll never share your email with anyone else.

Brief description for your profile. Maximum 500 characters.

Get notified about new features and updates.

Form Actions

The FormActions component supports different alignments for form buttons.

Message Types

Form messages support different types for various validation states.

Email is available!

Password strength is weak

Accessibility Features

The Form component is built with accessibility in mind:

  • Semantic HTML: Uses proper form elements and ARIA attributes
  • Label Association: Labels are automatically associated with their inputs
  • Error Announcements: Error messages are announced to screen readers
  • Required Field Indicators: Visual and accessible indicators for required fields
  • Focus Management: Proper focus order and visual focus indicators
  • Descriptive Content: Helper text is properly associated with form controls

Customization

Custom Styling

All form components accept a className prop for custom styling:

<Form className="max-w-2xl mx-auto">
  <FormField name="custom" className="mb-8">
    <FormLabel className="text-lg font-bold">Custom Label</FormLabel>
    <FormControl className="mt-3">
      <Input className="border-2 border-blue-500" />
    </FormControl>
    <FormDescription className="text-blue-600">
      Custom helper text styling
    </FormDescription>
  </FormField>
</Form>

Integration with Form Libraries

The Form component works well with form libraries like React Hook Form:

import { useForm, Controller } from 'react-hook-form';

function MyForm() {
  const { control, handleSubmit, formState: { errors } } = useForm();

  return (
    <Form onSubmit={handleSubmit(onSubmit)}>
      <Controller
        name="email"
        control={control}
        rules={{ required: 'Email is required' }}
        render={({ field }) => (
          <FormField name="email" error={errors.email?.message}>
            <FormLabel required>Email</FormLabel>
            <FormControl>
              <Input {...field} />
            </FormControl>
            <FormMessage />
          </FormField>
        )}
      />
      
      <FormActions>
        <Button type="submit">Submit</Button>
      </FormActions>
    </Form>
  );
}

Props

Form

PropTypeDefaultDescription
childrenReact.ReactNodeForm content
classNamestring''Additional CSS classes
onSubmit(event: React.FormEvent) => voidForm submit handler

FormField

PropTypeDefaultDescription
childrenReact.ReactNodeField content
namestringField name identifier
errorstringError message to display
descriptionstringDescription text
classNamestring''Additional CSS classes

FormLabel

PropTypeDefaultDescription
childrenReact.ReactNodeLabel content
classNamestring''Additional CSS classes
requiredbooleanfalseShow required indicator

FormMessage

PropTypeDefaultDescription
childrenReact.ReactNodeMessage content
classNamestring''Additional CSS classes
type'error' | 'success' | 'warning''error'Message type

FormActions

PropTypeDefaultDescription
childrenReact.ReactNodeAction buttons
classNamestring''Additional CSS classes
align'left' | 'center' | 'right''right'Button alignment

The Form components also accept all standard HTML attributes for their respective elements.