Form

free

Form keeps create and edit flows readable from input to validation.

import { useState } from 'react';
import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Installation

Add this component with the NateUI CLI.

npx nateui@latest add Form

Examples

Basic

import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Form Layout

import { useState } from 'react';
import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Form Size

import { useState } from 'react';
import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Label Extra

import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Message

import { PiQuestion } from 'react-icons/pi'
import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

Validation

import { useState, type FormEvent } from 'react';
import Button from '@/components/ui/button';
import Form from '@/components/ui/form';
import Input from '@/components/ui/input';

API

Form is a compound component: the root Form, Form.Field for label/control/error composition, and Form.Scope for nested layout or sizing context.

Form

PropDescriptionTypeDefault
childrenFields, scopes, and form content rendered inside the native form.ReactNode-
classNameClass names applied to the native form element.string-
containerClassNameClass names applied to the internal Form.Scope wrapper.string-
labelWidthLabel width shared by child fields when the layout is horizontal.string | number100
layoutLayout shared by child fields.FormLayout'vertical'
refRef forwarded to the native form element.Ref<HTMLFormElement>-
sizeControl size shared by descendant form-aware controls.ControlSize'md'
...nativeFormPropsNative props applied to the form element.NativeFormProps-

Form.Field

PropDescriptionTypeDefault
asteriskShows a destructive required marker before the label.boolean-
childrenThe control or content rendered under the label.ReactNode-
classNameClass names applied to the field wrapper.string-
errorMessageMessage rendered below the control when invalid is true, replacing hint while the field is invalid.string-
extraExtra label-side content, such as optional text or a compact count.string | ReactNode-
hintHelper text rendered below the control when there is no invalid error message.string | ReactNode-
htmlForAssociates the label with a control id.string-
invalidMarks the field invalid and provides invalid context to child controls.boolean-
labelField label content.string | ReactNode-
labelClassClass names applied to the label element.string-
labelIdId applied to the label element.string-
labelWidthOverrides the scoped label width for this field.string | number-
layoutOverrides the scoped layout for this field.FormLayout-
refRef forwarded to the field wrapper.Ref<HTMLDivElement>-
sizeOverrides the scoped control size for this field label.ControlSize-
...nativeDivPropsNative props applied to the field wrapper.NativeFieldProps-

Form.Scope

PropDescriptionTypeDefault
childrenForm fields that should share the scoped layout and size settings.ReactNode-
classNameClass names applied to the scope wrapper.string-
labelWidthLabel width shared by child fields when horizontal.string | number100
layoutLayout shared by child fields.FormLayout'vertical'
sizeControl size shared by descendant form-aware controls.ControlSize'md'
...nativeDivPropsNative props applied to the scope wrapper.NativeScopeProps-

Types

type FormLayout = 'horizontal' | 'vertical' | 'inline';
type ControlSize = 'sm' | 'md' | 'lg';
type NativeFormProps = ComponentPropsWithoutRef<'form'>;
type NativeFieldProps = ComponentProps<'div'>;
type NativeScopeProps = ComponentProps<'div'>;