Skip to content

Plugins

Add cross-cutting behavior — analytics, guards, or integrations — without forking core.

Previous: Adapters · Next: Patterns

Playground

Plugins explorer → — register hooks and inspect the event log.

Overview

Plugins receive the form instance and a typed hook API. Return a cleanup function or { onDestroy } for teardown.

ts
import type { FormPlugin } from "@jayoncode/form-intelligent";

const audit: FormPlugin = {
  name: "audit",
  order: 10,
  setup(form, api) {
    api.on("beforeSubmit", () => {
      console.log("submitting", form.values());
    });
    return {
      onDestroy() {
        console.log("audit removed");
      },
    };
  },
};

form.use(audit);
// or form.registerPlugin(audit);

Lifecycle hooks

HookWhenReturn false to
beforeValidateBefore validationCancel validation
afterValidateAfter validation
beforeSubmitAfter valid, before transportCancel submit
afterSubmitAfter submit attempt
onAutosaveAutosave succeeded
onDraftRestoreDraft merged on create
ts
api.on("beforeSubmit", () => false); // block submit
api.on("afterValidate", ({ valid, paths }) => {
  console.log(paths, valid);
});

Form events (lower level)

EventFires when
changeA value updates
blur / focusField binding events
validate / validatedValidation runs / finishes
submitSubmit starts
autosaveAutosave triggers
draftDraft persisted
resetForm resets
ts
setup(form) {
  return form.on("change", () => console.log("changed"));
}

Built-in plugin factories

ts
import {
  createBrowserLifecyclePlugin,
  createDevToolsPlugin,
  createKeyboardPlugin,
  keyboard,
} from "@jayoncode/form-intelligent/plugins";

form.use(createBrowserLifecyclePlugin({ saveDraftOnHidden: true }));
form.use(createDevToolsPlugin());
form.use(createKeyboardPlugin([keyboard.shortcut("mod+s", (f) => f.saveDraft())]));

Aliases: browserLifecyclePlugin, devtoolsPlugin.


Cleanup

ts
setup(form, api) {
  const off = api.on("afterSubmit", handler);
  return () => {
    off();
  };
}

Destroying the form runs all plugin cleanups.

Next: Patterns — wizard, offline submit, plugin recipes.

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.