@abgov/jsonforms-components
v2.86.3
Published
Government of Alberta - React renderers for JSON Forms based on the design system.
Keywords
Readme
jsonforms-components
React renderers for JSON Forms built on the Government of Alberta design system components. The ADSP form app and the form editor preview use them, and they are published to npm as @abgov/jsonforms-components so product teams can render ADSP form definitions in their own applications.
Inside this monorepo, import it through the @abgov/jsonforms-components path alias in tsconfig.base.json.
Install
npm install @abgov/jsonforms-componentsThese packages are peer dependencies, so install them in your application too (see package.json for the exact ranges):
react,@jsonforms/core,@jsonforms/react@abgov/react-components,@abgov/ui-components-commonajv,ajv-errors,ajv-formats@mdx-js/mdx,react-markdown,rehype-sanitize,dompurify,xssexpr-eval-fork,pluralize
@apidevtools/json-schema-ref-parser is an optional dependency, used by resolveRefs and tryResolveRefs.
Usage
Pass GoARenderers and GoACells to the JsonForms component. Wrap it in the two context providers the renderers read from:
- the provider returned by
ContextProviderFactory(), which supplies file management, submit and save callbacks, auto-populated data and stepper navigation to the renderers; JsonFormRegisterProvider, which supplies data register values to dropdown and text controls that use theregisteroption.
import { JsonForms } from '@jsonforms/react';
import {
ContextProviderFactory,
createDefaultAjv,
GoACells,
GoARenderers,
JsonFormRegisterProvider,
} from '@abgov/jsonforms-components';
// Create both once, at module level. JsonForms recompiles the schema when the ajv instance changes.
const ContextProvider = ContextProviderFactory();
const ajv = createDefaultAjv();
export const MyForm = ({ schema, uischema, data, onChange }) => (
<ContextProvider>
<JsonFormRegisterProvider defaultRegisters={undefined}>
<JsonForms
schema={schema}
uischema={uischema}
data={data}
renderers={GoARenderers}
cells={GoACells}
ajv={ajv}
validationMode="ValidateAndShow"
onChange={onChange}
/>
</JsonFormRegisterProvider>
</ContextProvider>
);To show a read-only summary of submitted data, pass GoAReviewRenderers instead of GoARenderers.
The UI schema selects and configures these renderers through its options. The Form service tutorials cover the schema side; the Cheat sheet has examples for each control, layout and stepper.
Reference
Main exports from src/index.ts:
- Renderer sets:
GoARenderers(full form, including stepper, file uploader, address lookup, name, phone, calculation and contact controls),GoAReviewRenderers(read-only review),GoABaseRenderers,GoABaseReviewRenderersandGoABaseTableReviewRenderers(subsets to build your own set), andGoACells. - Context:
ContextProviderFactory,JsonFormContext,useShowChangeButtons,ReviewRenderProvider,useReviewContext. - Data registers:
JsonFormRegisterProvider,JsonFormsRegisterContext,useRegisterUser, and the register types. - Validation and schemas:
createDefaultAjv(an Ajv instance with the formats the renderers use, such asphone,sinandfile-urn),resolveRefs,tryResolveRefs. - Auto-populate:
getAutoPopulatedData,mergeAutoPopulatedData,createAutoPopulateMiddleware. - Controls: the individual controls and their testers, for example
GoAInputTextControlandGoATextControlTester, plus the stepper navigation helpers such asgetNavigationTargetsanduseStepChange. - Performance:
memoizeTesterandmemoizeRendererTesters, which cache tester ranks so large forms don't re-run every tester on each render.
Development
npx nx test jsonforms-components
npx nx lint jsonforms-components
npx nx build jsonforms-componentsThe package is published to npm with semantic-release through the release target (.releaserc.json). Commits with the scope jsonforms-components trigger a release, for example feat(jsonforms-components): add a new control. See AGENTS.md for the commit conventions.
Related guides
- Integration: adding the renderers to your application.
- Data registers:
JsonFormRegisterProviderand theregisteroption. - File uploader: the
fileManagementcallbacks on the context provider. - External navigation: driving the stepper and review "Change" links from the host.
- Building forms and the Cheat sheet: UI schema options for each renderer.
