# Form

Form container with validation, Ctrl+S submit, and dirty tracking



<ComponentPreview base="opentui" name="form-demo" />

Installation [#installation]

<CodeTabs>
  <TabsList>
    <TabsTrigger value="cli">
      Command
    </TabsTrigger>

    <TabsTrigger value="manual">
      Manual
    </TabsTrigger>
  </TabsList>

  <TabsContent value="cli">
    ```bash
    npx shadcn@latest add @termcn/opentui-form
    ```
  </TabsContent>

  <TabsContent value="manual">
    <Steps>
      <Step>
        Copy and paste the following code into your project.
      </Step>

      <ComponentSource base="opentui" name="form" title="components/ui/form.tsx" />

      <Step>
        Update the import paths to match your project setup.
      </Step>
    </Steps>
  </TabsContent>
</CodeTabs>

Usage [#usage]

```tsx
import { Form } from "@/components/ui/form";
```

```tsx
<Form
  initialValues={{ name: "", email: "" }}
  fields={[
    { name: "name", validate: (v) => (v ? null : "Name is required") },
    { name: "email", validate: (v) => (v ? null : "Email is required") },
  ]}
  onSubmit={(values) => console.log("Submitted:", values)}
>
  <Text>Name and email form (Ctrl+S to submit)</Text>
</Form>
```

API Reference [#api-reference]

Form [#form]

| Prop            | Type                                        | Default    |
| --------------- | ------------------------------------------- | ---------- |
| `onSubmit`      | `(values: Record<string, unknown>) => void` | `required` |
| `initialValues` | `Record<string, unknown>`                   | `{}`       |
| `fields`        | `FormField[]`                               | `[]`       |
| `children`      | `ReactNode`                                 | `required` |
