# Parts on field components

> Give a field component a plugin's config once, and every field it renders gets it.

Source: https://forms.lunarhue.dev/react/plugin-composition/field-parts

A part is a plugin's config for one place. Put one on a field type and every
field that type renders gets it, so the phone format lives with the phone
input instead of on each form. One component makes all three field types
below.

```tsx title="contact.tsx"
import { createForms, defineField, useForm } from "@lunarhue/forms-react";
import { digits, mask, masked, upper } from "@lunarhue/forms-react/plugins";
import { TextInput } from "./TextInput";

// "5551234567" shows as "(555) 123-4567".
function formatPhone(value: string) {
  if (value.length > 6) return `(${value.slice(0, 3)}) ${value.slice(3, 6)}-${value.slice(6, 10)}`;
  if (value.length > 3) return `(${value.slice(0, 3)}) ${value.slice(3)}`;
  return value;
}

// Two field types from one component. Each part travels with its type.
const PhoneField = defineField(TextInput, masked({ clean: digits, display: formatPhone }));
const CodeField = defineField(TextInput, masked({ clean: upper, display: (value) => value }));

const forms = createForms({ plugins: [mask()], fields: { TextInput, PhoneField, CodeField } });

const contact = forms.define({
  defaultValues: { name: "", phone: "", referral: "" },
  onSubmit: (value) => value,
});

export default function ContactForm() {
  const form = useForm(contact, { id: "contact" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.TextInput name="name" label="Name" />
          <form.PhoneField name="phone" label="Phone" />
          <form.CodeField name="referral" label="Referral code" />
          <button type="submit">Save</button>
        </form>
      )}
    </form.Form>
  );
}
```

```tsx title="TextInput.tsx"
import { type FieldApi, inputProps } from "@lunarhue/forms-react";

interface TextInputProps {
  readonly field: FieldApi<string>;
  readonly label: string;
}

// One plain component. The field types in contact.tsx give it different masks.
export function TextInput({ field, label }: TextInputProps) {
  return (
    <label>
      {label}
      <input {...inputProps(field)} />
      <output>Stored: {JSON.stringify(field.value)}</output>
    </label>
  );
}
```

## Field types with parts

`defineField` takes the component and any number of parts:

```ts
const PhoneField = defineField(TextInput, masked({ clean: digits, display: formatPhone }));
const CodeField = defineField(TextInput, masked({ clean: upper, display: (value) => value }));

const forms = createForms({ plugins: [mask()], fields: { TextInput, PhoneField, CodeField } });
```

`<form.PhoneField name="phone" />` stores digits and shows them formatted.
`<form.TextInput name="name" />` is the same component with no mask.

A part's type says which plugin it needs and which values it fits. Two
mistakes fail to compile:

- A part on a component whose value it doesn't fit: `masked` takes string
  fields, so `defineField(NumberInput, masked(...))` is a type error.
- A form that renders the field type without the plugin. The error reads
  `Add mask() to this form's plugins: PhoneField uses it`.

## When parts attach

A field type's parts attach the first time it renders a field. Until then the
field has no mask, so its value isn't cleaned. A value that matters before
that, such as a field on a step the user hasn't reached, can be pinned on the
definition:

```ts
const contact = forms.define({ defaultValues, onSubmit }, PhoneField.at("phone"));
```

The part then attaches when the store is made, so it is in the server HTML
too. Rendering the field later doesn't attach it twice.
