Generating a form
formFromObject(value, options?) builds a simple form for a plain JSON value, and obfo reads it back as the same
value with no options:
import obfo, { formFromObject } from "@johnhenry/obfo";
const form = formFromObject({ title: "Notes", count: 3, done: false, tags: ["a", "b"], owner: null });document.body.append(form);obfo(form); // { title: "Notes", count: 3, done: false, tags: ["a", "b"], owner: null }What it builds
Section titled “What it builds”| Value | Element |
|---|---|
| object | A fieldset with data-obfo-container="{}" and a legend (the root is the form itself). |
| array | A fieldset with data-obfo-container="[]"; items are labelled [0], [1], … |
| string | input type="text", or a textarea when it contains a line break. |
| number | input type="number" step="any" with data-obfo-cast="number". |
| boolean | input type="checkbox" with data-obfo-cast="checkbox". |
null |
input type="hidden" with data-obfo-cast="null". |
Every leaf is wrapped in a label with its key, and carries name and data-obfo-name. Because each input carries
the cast that reads it back, the form round-trips under the default cast and under cast: "auto" alike, and
fill can write a new value of the same shape into it.
- The root must be a plain object or an array. A primitive, a class instance or a
Dateat the root throws aTypeError. - Only JSON. Keys whose value is
undefinedare left out andundefinedarray items becomenull, asJSON.stringifydoes. Anything else that isn’t JSON (aDate, aMap,NaN,Infinity, a function, abigint, a symbol) throws aTypeErrornaming its path (a.b[1] is NaN). - Data never becomes markup. Keys and values are set with
setAttributeandtextContent, never parsed as HTML. A key named__proto__round-trips as an ordinary own property. - A document is needed.
options.documentis the document to create elements in; it defaults to the globaldocument, and in Node you pass a happy-dom or jsdom document. - Line breaks normalize. A
textareareturns\nfor line breaks, so a lone\rdoesn’t survive.
The form is unstyled and has no submit button: a starting point for an “input form generated from data”, to style,
fill and observe.