# Components that need a plugin

> Shared components that read a plugin's members, such as a dirty marker, an undo button or a save bar.

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

An undo button or a save bar belongs on every form, not in each definition.
The bar below works under any form from the same `createForms`, and the text
field marks itself changed.

```tsx title="editor.tsx"
import { useForm } from "@lunarhue/forms-react";
import { forms } from "./forms";
import { SaveBar } from "./SaveBar";

const post = forms.define({
  defaultValues: { title: "Hello", body: "First post." },
  onSubmit: (value) => value,
});

export default function PostEditor() {
  const form = useForm(post, { id: "editor" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.TextField name="title" label="Title" />
          <form.TextField name="body" label="Body" />
          <SaveBar />
        </form>
      )}
    </form.Form>
  );
}
```

```ts title="forms.ts"
import { createForms } from "@lunarhue/forms-react";
import { dirty, history } from "@lunarhue/forms-react/plugins";
import { TextField } from "./TextField";

// Keep the object `history()` returns for its `formMeta` reader.
export const undoHistory = history();

export const forms = createForms({ plugins: [dirty(), undoHistory], fields: { TextField } });
```

```tsx title="SaveBar.tsx"
import { useFormContext, useFormState } from "@lunarhue/forms-react";
import { forms, undoHistory } from "./forms";

// Works under any form from `forms`, whatever its definition. Its type knows
// the shared plugins, so `form.plugins.history` type-checks.
export function SaveBar() {
  const form = useFormContext(forms);
  const changed = useFormState(form, (state) => state.dirty);
  const canUndo = useFormState(form, (state) => undoHistory.formMeta(state).canUndo);
  return (
    <div>
      <button type="button" disabled={!canUndo} onClick={() => form.plugins.history.undo()}>
        Undo
      </button>
      <button type="submit" disabled={!changed}>
        Save
      </button>
    </div>
  );
}
```

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

// Reads `dirty`, so it says it needs dirty(). It fits only forms that have it.
export function TextField({ field, label }: { field: FieldApi<string, FormError, "dirty">; label: string }) {
  return (
    <label>
      {field.dirty ? `${label} (changed)` : label}
      <input {...inputProps(field)} />
    </label>
  );
}
```

## A field component that reads a plugin

A component that reads a plugin's member from its field says so in its type.
The third type argument of `FieldApi` names the plugins it needs:

```tsx
function TextField({ field, label }: { field: FieldApi<string, FormError, "dirty">; label: string }) {
  return (
    <label>
      {field.dirty ? `${label} (changed)` : label}
      <input {...inputProps(field)} />
    </label>
  );
}
```

It then fits only forms with `dirty()`. Without the third argument,
`field.dirty` is a type error.

## The form from context

A component shared by many forms can't name one definition. Pass the object
`createForms` returned to `useFormContext` instead. It returns the nearest
form above that one of its definitions made, and skips forms from other
`createForms` calls:

```tsx
const undoHistory = history();
export const forms = createForms({ plugins: [dirty(), undoHistory], fields: { TextField } });

function SaveBar() {
  const form = useFormContext(forms);
  const changed = useFormState(form, (state) => state.dirty);
  const canUndo = useFormState(form, (state) => undoHistory.formMeta(state).canUndo);
  return (
    <div>
      <button type="button" disabled={!canUndo} onClick={() => form.plugins.history.undo()}>
        Undo
      </button>
      <button type="submit" disabled={!changed}>
        Save
      </button>
    </div>
  );
}
```

The form's type knows the shared plugins, the error type and the registered
field components, so `form.plugins.history`, `dirty` and `form.TextField` all
type-check. Its value type is unknown, and plugins added by a definition or
by one form are not in its type. `useFormContext` throws when no form of
those forms is above.

## Passing the form as a prop

To take the form as a prop instead, type it `FormOf<typeof forms>`:

```tsx
function SaveBar(props: { form: FormOf<typeof forms> }) {
  // ...
}
```

Any form from `forms` fits.
