# History

> Undo and redo the user's edits.

Source: https://forms.lunarhue.dev/solid/plugins/history

A long text or a settings page needs undo. `history()` records the user's
edits so an Undo button can step back through them. Type, then undo.

```tsx title="note.tsx"
import { createForm, createForms } from "@lunarhue/forms-solid";
import { history } from "@lunarhue/forms-solid/plugins";
import { TextField } from "./TextField";

// Keep the object `history()` returns. Its `formMeta` reader says
// whether there is a step to undo or redo.
const undoHistory = history();

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

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

export default function NoteForm() {
  const form = createForm(note, { id: "note" });
  const meta = () => undoHistory.formMeta(form.state);
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.TextField name="title" label="Title" />
          <form.TextField name="body" label="Body" />
          <div>
            <button
              type="button"
              disabled={!meta().canUndo}
              onClick={() => form.plugins.history.undo()}
            >
              Undo
            </button>
            <button
              type="button"
              disabled={!meta().canRedo}
              onClick={() => form.plugins.history.redo()}
            >
              Redo
            </button>
          </div>
        </form>
      )}
    </form.Form>
  );
}
```

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

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

export function TextField(props: TextFieldProps) {
  return (
    <label>
      {props.label}
      <input {...inputProps(props.field)} />
    </label>
  );
}
```

`history()` records the user's value and array writes, one step per task, such
as one keystroke or one paste. `form.plugins.history` has `undo()` and
`redo()`, and each returns false when there is no step to take. It keeps the
latest 100 steps. Pass another limit as `history(50)`.

## Undo buttons

Whether there is a step to undo or redo is form meta. Read it with the
plugin's own `formMeta` reader, so keep the object `history()` returns:

```tsx
const undoHistory = history();
const forms = createForms({ plugins: [undoHistory] });

// In a component with a form of those forms.
<button
  type="button"
  disabled={!undoHistory.formMeta(form.state).canUndo}
  onClick={() => form.plugins.history.undo()}
>
  Undo
</button>;
```

Both flags are false before the first step, on the server too. They change in
the same render as an undo, a redo or a reset. After an edit they change a
microtask later, when `history` records the step.

## What it records

Only user writes make a step. A plugin's writes, resets and rebases don't, and
a reset clears both stacks. A plugin that writes to the store directly instead
of through `ctx.write` makes a user write, so that does make a step. See
[Writing a plugin](https://forms.lunarhue.dev/solid/writing-plugins/first-plugin.md).

Undo and redo go through the fields, so validation runs on the values they
write.

## Arrays

Undoing an insert or a move keeps the keys of the rows that stay, and their
touched, edited and error state with them.

Undoing a remove puts the row back with a new key. Core never brings a removed
key back, so the row's state starts fresh: touched, edited and errors are
gone, and a component keyed by the old key mounts again.
