# Parts

> Plugin config for one field type, fieldset or definition, and the attach hooks that use it.

Source: https://forms.lunarhue.dev/solid/writing-plugins/parts

A plugin that applies everywhere is too blunt for most jobs. A length limit
belongs on a note field, not on every string in the form. Parts carry a
plugin's config to one place. Type past 20 characters below.

```tsx title="note.tsx"
import { createForm, createForms, defineField } from "@lunarhue/forms-solid";
import { limit, limited } from "./limit";
import { NoteInput } from "./NoteInput";

// The part rides on the field type. Plain NoteInput fields have no limit.
const ShortNote = defineField(NoteInput, limited(20));

const forms = createForms({ plugins: [limit()], fields: { NoteInput, ShortNote } });

const note = forms.define({
  defaultValues: { headline: "", body: "" },
  onSubmit: (value) => value,
});

export default function NoteForm() {
  const form = createForm(note, { id: "note" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.ShortNote name="headline" label="Headline (20 at most)" />
          <form.NoteInput name="body" label="Body" />
        </form>
      )}
    </form.Form>
  );
}
```

```ts title="limit.ts"
import type { Part, PluginContext } from "@lunarhue/forms-solid";
import { definePlugin } from "@lunarhue/forms-solid/plugins";

interface Limit {
  readonly max: number;
}

// The plugin. `attach` runs once for each field a `limited` part lands on.
export const limit = () =>
  definePlugin({
    name: "limit",
    attach: (_ctx: PluginContext, { max }: Limit) => ({
      beforeWrite: (write) => (typeof write.value === "string" ? write.value.slice(0, max) : write.value),
    }),
  });

// The part. It fits string fields only and needs limit() in the form's plugins.
export const limited = (max: number): Part<"limit", string> => ({ plugin: "limit", config: { max } });
```

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

export function NoteInput(props: { field: FieldApi<string>; label: string }) {
  return (
    <label>
      {props.label}
      <input {...inputProps(props.field)} />
      <output>{props.field.value.length} characters</output>
    </label>
  );
}
```

A part is a plugin's config for one place. A field type, a fieldset or a
definition carries it, and the plugin's `attach` gets it with the path it
landed on. Write a typed constructor for your parts, so each one names the
plugin and the values it fits:

```ts
import type { Part, PluginContext } from "@lunarhue/forms-core";
import { definePlugin } from "@lunarhue/forms-core/plugins";

interface Limit {
  readonly max: number;
}

export const limit = () =>
  definePlugin({
    name: "limit",
    attach: (_ctx: PluginContext, { max }: Limit) => ({
      beforeWrite: (write) => (typeof write.value === "string" ? write.value.slice(0, max) : write.value),
    }),
  });

// Fits string fields only.
export const limited = (max: number): Part<"limit", string> => ({ plugin: "limit", config: { max } });
```

An app puts `limit()` in the form's plugins and `limited(...)` where it
applies: `defineField(NoteInput, limited(140))` for every field a component
renders, `forms.fieldset(Address, part)` for a fieldset, or
`forms.define(config, part)` for the form. `Part<"limit", string>` makes two
things type errors: a part on a field type whose value isn't a string, and a
form that uses the field type without `limit()` in its plugins.

`ctx.path` is the stable path the part attached at. The hooks `attach`
returns get only the writes that reach that path. A write above it arrives
walked down to the path, and a refuse there refuses the whole write. A write
below it arrives as it is. When the field goes away, as when its array item
is removed, its hooks go too.

A field type's parts attach the first time it renders a field. A fieldset's
attach the first time its scope is read, and a definition's when the store
is made. Before the store has listeners, `attach` runs at once and what it
writes is part of the starting state, as in setup. After that it runs on a
microtask, so its writes don't land while a framework is rendering. Either
way its writes make no records.
