@form2js/js2form

March 21, 2026 ยท View on GitHub

@form2js/js2form moves data in the opposite direction: take a nested object and write it into matching form controls. Use it when you need to prefill a form, restore saved draft state, or sync object data back into existing DOM controls.

Installation

npm install @form2js/js2form

Standalone/global build is not shipped for this package.

General Example

import { objectToForm } from "@form2js/js2form";

objectToForm("profileForm", {
  person: {
    name: { first: "Tiffany", last: "Aching" },
    roles: ["witch"],
  },
});

Types and Properties

Exported Surface

ExportKindWhat it does
RootNodeInputtypeRoot as element id, node, null, or undefined.
ObjectToFormNodeCallbacktypeWrite-time callback for per-node assignment control.
ObjectToFormOptionsinterfaceOptions for name normalization, cleaning, and document resolution.
SupportedField, SupportedFieldCollection, FieldMaptypesField typing used by mapping and assignment helpers.
flattenDataForFormfunctionFlattens object data to an entry list.
mapFieldsByNamefunctionBuilds a normalized name-to-field mapping.
objectToFormfunctionPopulates matching fields from object data.
js2formfunctionCompatibility wrapper around objectToForm.
normalizeNamefunctionNormalizes field names and compacts indexed arrays.
export interface ObjectToFormOptions {
  delimiter?: string;
  nodeCallback?: ObjectToFormNodeCallback;
  useIdIfEmptyName?: boolean;
  shouldClean?: boolean;
  document?: Document;
}

export function flattenDataForForm(data: unknown): Entry[];
export function mapFieldsByName(
  rootNode: RootNodeInput,
  options?: Pick<ObjectToFormOptions, "delimiter" | "useIdIfEmptyName" | "shouldClean" | "document">
): FieldMap;
export function objectToForm(rootNode: RootNodeInput, data: unknown, options?: ObjectToFormOptions): void;

Options And Defaults

OptionDefaultWhereWhy this matters
delimiter"."objectToForm, mapFieldsByName, js2formMust match how your input keys are structured.
useIdIfEmptyNamefalseobjectToForm, mapFieldsByName, js2formUseful when form controls are keyed by id instead of name.
shouldCleantrueobjectToForm, mapFieldsByNameClears form state before applying incoming values.
documentambient/global documentall root-resolving APIsNeeded when running with a DOM shim.
nodeCallbackunsetobjectToForm, js2form optionsCalled before default assignment; return false to skip default assignment for that node.

shouldClean: false

Disable cleaning when you want to layer partial data onto an existing form without clearing unrelated controls first.

import { objectToForm } from "@form2js/js2form";

objectToForm(
  "profileForm",
  {
    person: {
      name: { first: "Tiffany" }
    }
  },
  { shouldClean: false }
);

useIdIfEmptyName

Match fields by id when the markup does not provide stable name attributes.

import { objectToForm } from "@form2js/js2form";

objectToForm(
  document.getElementById("profileForm"),
  {
    firstName: "Agnes"
  },
  { useIdIfEmptyName: true }
);

Behavior Notes

  • objectToForm is a no-op when the root cannot be resolved.
  • Checkbox and radio groups are matched with [] and non-[] name fallbacks.
  • Name normalization compacts sparse indexes to sequential indexes during matching.
  • For multi-select names like colors[], matching includes [] and bare-name fallbacks without creating one map key per option.
  • Form updates set values, checked state, and selected state, but do not dispatch synthetic events.