Skip to content
forms

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.

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.

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:

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:

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>:

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

Any form from forms fits.

Last updated