# Overview

TanStack Form is the ultimate solution for handling forms in web applications, providing a powerful and flexible approach to form management. Designed with first-class TypeScript support, headless UI components, and a framework-agnostic design, it streamlines form handling and ensures a seamless experience across various front-end frameworks.

## Motivation

Most web frameworks do not offer a comprehensive solution for form handling, leaving developers to create their own custom implementations or rely on less-capable libraries. This often results in a lack of consistency, poor performance, and increased development time. TanStack Form aims to address these challenges by providing an all-in-one solution for managing forms that is both powerful and easy to use.

With TanStack Form, developers can tackle common form-related challenges such as:

- Reactive data binding and state management
- Complex validation and error handling
- Accessibility and responsive design
- Internationalization and localization
- Cross-platform compatibility and custom styling

By providing a complete solution for these challenges, TanStack Form empowers developers to build robust and user-friendly forms with ease.

## Enough talk, show me some code already!

In the example below, you can see TanStack Form in action with the Lit framework adapter:

<!-- ::start:tabs variant="files" -->

```ts ignore file="App.ts"
import { LitElement, html } from 'lit'
import { TanStackFormController } from '@tanstack/lit-form'
import { fieldInfo } from './FieldInfo'
import { textInput } from './TextInput'

export class App extends LitElement {
  private form = new TanStackFormController(this, {
    defaultValues: {
      firstName: '',
      lastName: '',
    },
    onSubmit: async ({ value }) => {
      // Do something with the validated form values
      console.log(value)
    },
  })

  render() {
    return html`
      <form
        @submit=${(event: SubmitEvent) => {
          event.preventDefault()
          // Validate the form before calling onSubmit.
          void this.form.api.handleSubmit().catch(console.error)
        }}
      >
        <!-- A type-safe field directive -->
        ${this.form.field(
          {
            name: 'firstName',
            validators: [
              {
                // Validate on change as well as on submit.
                triggers: ['change'],
                run: ({ value }) => {
                  if (value.trim().length === 0) {
                    return 'Enter your first name'
                  }
                  return undefined
                },
              },
            ],
          },
          (field) => html`
            <div>
              <!-- Use your own markup and UI libraries! -->
              <label for=${field.name}>First name</label>
              ${textInput(field)} ${fieldInfo(field)}
            </div>
          `,
        )}
        ${this.form.field(
          { name: 'lastName' },
          (field) => html`
            <div>
              <label for=${field.name}>Last name</label>
              ${textInput(field)} ${fieldInfo(field)}
            </div>
          `,
        )}
        <!-- Update this button when the selected state changes. -->
        ${this.form.subscribe(
          (state) => [state.canSubmit, state.isSubmitting] as const,
          ([canSubmit, isSubmitting]) => html`
            <button type="submit" ?disabled=${!canSubmit}>
              ${isSubmitting ? 'Submitting...' : 'Submit'}
            </button>
          `,
        )}
        <!-- Restore default values and clear validation and interaction state. -->
        <button
          type="reset"
          @click=${(event: Event) => {
            event.preventDefault()
            this.form.api.reset()
          }}
        >
          Reset
        </button>
      </form>
    `
  }
}

customElements.define('example-form', App)
```

```ts ignore file="TextInput.ts"
import { html } from 'lit'
import type { FieldWithValue } from '@tanstack/lit-form'

// This file provides the UI. Use any UI library you prefer.
export function textInput(field: FieldWithValue<string>) {
  return html`
    <input
      id=${field.name}
      name=${field.name}
      .value=${field.value}
      @input=${(event: InputEvent) =>
        field.handleChange((event.currentTarget as HTMLInputElement).value)}
      @blur=${() => field.handleBlur()}
    />
  `
}
```

```ts ignore file="FieldInfo.ts"
import { html, nothing } from 'lit'
import type { AnyFieldApi } from '@tanstack/lit-form'

// This file provides the UI. Use any UI library you prefer.
export function fieldInfo(field: AnyFieldApi) {
  if (field.meta.isValid) return nothing

  return html`
    <p role="alert">${field.errors.map((error) => error.message).join(', ')}</p>
  `
}
```

<!-- ::end:tabs -->

## Next steps

Use [Installation](./installation) to set up your framework package. The
[Form API](./reference/interfaces/FormApi) and
[Field API](./reference/interfaces/FieldApi) references describe the methods and
state available as you extend your form.
