# Content temporarily unavailable
As a form grows, keeping every field in one component can make the code harder
to navigate and maintain. You can move related fields into smaller components
while keeping one form instance, but each new component boundary needs a type
for the form or field it receives.

## Split a large form into sections

First define the form's reusable options with `formOptions`. In this example,
the same module also exports a named form type derived from those options. The
parent can add page-specific options, such as `onSubmit`, when it creates the
form, while extracted sections can import the same form type.

Lit’s `LitFormType` derives a controller type from the shared options. The
result keeps the field names and values available to the controller's `field`
method in the extracted render function.

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

```ts file="shared-form.ts"
import { formOptions } from '@tanstack/lit-form'
import type { LitFormType } from '@tanstack/lit-form'

export const profileFormOptions = formOptions({
  defaultValues: {
    name: '',
    address: {
      street: '',
      city: '',
    },
  },
})

export type ProfileForm = LitFormType<typeof profileFormOptions>
```

```ts file="address-fields.ts"
import { html } from 'lit'
import type { ProfileForm } from './shared-form'

export function addressFields(form: ProfileForm) {
  return html`
    ${form.field(
      { name: 'address.street' },
      (field) => html`
        <input
          aria-label="Street"
          .value=${field.value}
          @input=${(event: InputEvent) =>
            field.handleChange((event.currentTarget as HTMLInputElement).value)}
        />
      `,
    )}
    ${form.field(
      { name: 'address.city' },
      (field) => html`
        <input
          aria-label="City"
          .value=${field.value}
          @input=${(event: InputEvent) =>
            field.handleChange((event.currentTarget as HTMLInputElement).value)}
        />
      `,
    )}
  `
}
```

```ts file="profile-form.ts"
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import { TanStackFormController } from '@tanstack/lit-form'
import { addressFields } from './address-fields'
import { profileFormOptions } from './shared-form'

@customElement('profile-form')
export class ProfileForm extends LitElement {
  private form = new TanStackFormController(this, {
    ...profileFormOptions,
    onSubmit: ({ value }) => console.log(value),
  })

  render() {
    return html`${addressFields(this.form)}`
  }
}
```

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

## Extract a field component by value type

After splitting the form into sections, you may still have repeated controls
for values such as strings, numbers, or dates. These controls don't need to
know the form's complete data shape or the path of a particular field. They
only need the type of the value they read and update.

`FieldWithValue<T>` describes a field by the value type a reusable component
handles. This preserves type checking when the component reads or updates the
value without tying it to a particular field path or form shape.

Type the Lit render helper's field parameter as `FieldWithValue<string>`, then
call it from the controller's `field` render callback.

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

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

export function textField(field: FieldWithValue<string>, label: string) {
  return html`
    <label>
      <span>${label}</span>
      <input
        name=${field.name}
        .value=${field.value}
        @blur=${() => field.handleBlur()}
        @input=${(event: InputEvent) =>
          field.handleChange((event.currentTarget as HTMLInputElement).value)}
      />
    </label>
  `
}
```

```ts file="name-field.ts"
import { textField } from './text-field'
import type { ProfileForm } from './shared-form'

export function nameField(form: ProfileForm) {
  return form.field({ name: 'name' }, (field) => textField(field, 'Name'))
}
```

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

## Accept any form or field

Some extracted controls behave the same regardless of the form's data. An
error display only needs field metadata, while a submit or reset button may
only need form state and methods. Giving these controls the concrete
`ProfileForm` type would couple them to details they don't use.

Use `AnyFieldApi` when a control doesn't depend on the field's value type. For
form-level controls, use the adapter's `Any*FormApi` type. These types
deliberately remove information about values and field paths, so use a concrete
type when a component reads either one.

Lit doesn't expose an `AnyLitFormApi` controller type. Use `AnyFieldApi` for a
field render helper, or pass the controller's core `form.api` object to a
helper typed with `AnyFormApi`.

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

```ts file="field-errors.ts"
import { html } from 'lit'
import type { AnyFieldApi } from '@tanstack/lit-form'

export function fieldErrors(field: AnyFieldApi) {
  return html`
    <small role="alert" aria-live="polite">
      ${field.errors.map((error) => error.message).join(', ')}
    </small>
  `
}
```

```ts file="reset-button.ts"
import { html } from 'lit'
import type { AnyFormApi } from '@tanstack/lit-form'

export function resetButton(form: AnyFormApi) {
  return html`
    <button type="button" @click=${() => form.reset()}>Reset</button>
  `
}
```

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