A library helper to generate complex forms and offer a system to decorate them.
To abstract as much as possible the hierarchy and functionality of data structures and allow constructing a UX-suffisant ecosystem to represent and collect information to the user.
sharedContext
, SchemaNode
and schema's meta
attributesmeta
attribute if a spec does not fit the mission of the library directlymeta
if really needed (use good judgement). The goal is to have a JSON that's as minimal as possible, but still offers enough information to structure an abstract layout that presents and collects information. Right now, the schema is generated by code but could also be generated through a WYSIWYG editor in the future and we have to take that into consideration (reusing similar patterns across node types, having similar relations across different structure depths, etc.)dev up
or yarn
to install dependencies
dev build
or yarn build
to create a consumable package
Create a context to be shared across the form
let say we work from this network request
{
"firstName": {
"type": "string",
"value": "John",
"validators": [{"name": "Presence"}],
"labels": {
"label": "First name",
"description": "Birth given first name"
}
},
"lastName": {
"type": "string",
"value": "Smith",
"validators": [{"name": "Presence"}],
"labels": {
"label": "Last name",
"description": "Birth given last name"
}
},
"age": {
"type": "integer",
"value": 30,
"validators": [
{"name": "Presence"},
{"name": "Format", "format": "[1-9][0-9]"}
]
},
"medicalNumber": {
"type": "string",
"validators": [
{
"name": "Presence",
"message": "We need your medical number to verify your identity"
},
{"name": "Format", "format": "[A-Z]{4}-?[0-9]{6}-?[0-9]{2}"}
],
"labels": {
"label": "Medical insurance number",
"helpText": "This is the number at the top of your Medical card, 4 letters followed by 8 digits"
}
}
}
We can create our root SchemaNode
from:
import {DeclarativeFormContext} from '@shopify/declarative-forms';
import {decorate} from 'decoratorFactory';
import request from 'request.json';
const {values, schema, labels} = request;
const context = new DeclarativeFormContext({
values,
decorate,
translators,
sharedContext,
});
const root = new SchemaNode(context, schema);
Now let's look at decorators. decorators can be found at
export function decorate(context) {
context
.where(({depth}) => depth === 0)
.prependWith(HeaderBanner)
.appendWith(FooterActions);
context.where(({type}) => type === 'string').replaceWith(TextField);
context.where(({type}) => type === 'boolean').replaceWith(CheckBoxComponent);
context
// Allows for granular selectors, eveything from the SchemaNode is accessible to help filtering
.where(({schema}: SchemaNode) => Boolean(schema.options?.length))
// Additional props can receive a factory that read the current node
.replaceWith(SelectComponent, ({schema}) => ({
preselectFirstItem: !!schema.meta.preselect,
}));
}
Once you have you have a node, you can simply render it with
import {renderNodes} from '@shopify/declarative-forms';
function ReactComponent({request}) {
const root: SchemaNode = useMemoizedRootNodeFromRequest(request);
return <Layout>{renderNodes(root)}</Layout>;
}
Or you can render sub parts of the schema
import {renderNodes} from '@shopify/declarative-forms';
function ReactComponent({request}) {
const root: SchemaNode = useMemoizedRootNodeFromRequest(request);
return <Layout>{renderNodes(root.children.medicalNumber)}</Layout>;
}
You can have a state-self-managed context shared across all the components and available at the consumer level, it's on the context object called sharedContext
.
When creating a context
const context = new DeclarativeFormContext({
values,
decorate,
translators,
sharedContext, // <---- this is the shared context
});
const root = new SchemaNode(context, schema);
it's structure is extendable with anything and useNode
will expose this context and react to it's changes.
from anywhere in the code you can
node.context.updateContext({
anySortofFlag: true,
});
then from any React component
function CustomComponent({node}: NodeProps) {
const {
sharedContext: {anySortofFlag},
} = useNode(node);
return <Modal visible={anySortofFlag} />;
}
and the component will automatically react to the changes
the full API documentation is available at https://shopify.github.io/declarative-forms/
you can run a local version of this by simply running yarn start
(make sure to have the depedencies installed before with yarn
).
This will create a local folder docs/
that you might want to clean up before committing some code changes, leaving the docs in place will not cause any harm but it will polluate the git diff on your next PR with autogenerated files.
If you can't click those links, it's probably because you are viewing the the README.md directly instead of browsing the advanced documentation at https://shopify.github.io/declarative-forms/
Generated using TypeDoc