cern-sis / react-formule

User-friendly, extensible form builder for React based on JSON Schema and RJSF
https://cern-sis.github.io/react-formule/
MIT License
7 stars 3 forks source link
forms jsonschema react rjsf

🕹️ DEMO 🕹️

Commitizen friendly

:horse: What is Formule?

Formule is a powerful, user-friendly, extensible and mobile-friendly form building library based on JSON Schema and RJSF, which aims to make form creation easier for both technical and non-technical people.

It originated from the need of a flexible tool for physicists at CERN to create their custom forms in the CERN Analysis Preservation application (a process that was originally done by the CAP team who had to manually define the JSON schemas for every member experiment) in a zero-code fashion. This tool proved to be very useful for us to more easily scalate and expand, reaching a wider audience here at CERN. So, we thought it could also be useful for other people and decided to decouple it from CAP and release it as an open source library.

[!WARNING] react-formule has just come out and is undergoing active development, so please feel free to share any issue you find with us and/or to contribute!

:carousel_horse: How it looks like

A simple setup (see ./formule-demo) could look like this:

:racehorse: How it works

Formule consists of the following main components:

It also exports the following functions:

And the following utilities:

Field types

Formule includes a variety of predefined field types, grouped in three categories:

You can freely remove some of these predefined fields and add your own custom fields and widgets following the JSON Schema specifications. More details below.

All of these items contain different settings that you can tinker with, separated into Schema Settings (generally affecting how the field works) and UI Schema Settings (generally affecting how the field looks like).

:horse_racing: Setting it up

Installation

npm install react-formule
# or
yarn add react-formule

Basic setup

import {
    FormuleContext,
    SelectOrEdit,
    SchemaPreview,
    FormPreview,
    initFormuleSchema
} from "react-formule";

const useEffect(() => initFormuleSchema(), []);

<FormuleContext>
    <SelectOrEdit />
    <SchemaPreview />
    <FormPreview />
</FormuleContext>

Customizing and adding new field types

<FormuleContext
  theme={{token: {colorPrimary: "blue"}}}
  customFieldTypes={...}
  customFields={...}
  customWidgets={...}>
// ...
</FormuleContext>

If you use Formule to edit existing JSON schemas that include extra fields (e.g. metadata fields) that you don't want to show up in the Formule editor (i.e. in SchemaPreview and SchemaTree), you can use transformSchema to exclude them:

const transformSchema = (schema) => {
  // Remove properties...
  return transformedSchema;
};

<FormuleContext transformSchema={transformSchema}>/* ... */</FormuleContext>;

Handling and customizing errors

You can add a custom transformErrors function to process, edit or filter the errors from RJSF in the way that best suits our needs:

const transformErrors = (errors) => {
  return errors.filter(...)
};

<FormuleForm transformErrors={transformErrors} />

Syncing Formule state

If you want to run some logic in your application every time the current Formule state changes in any way (e.g. to run some action every time a new field is added to the form) you can pass a function to be called back when that happens:

const handleFormuleStateChange = (newState) => {
  // Do something when the state changes
};

<FormuleContext synchonizeState={handleFormuleStateChange}>
  // ...
</FormuleContext>;

Alternatively, you can pull the current state on demand by calling getFormuleState at any moment.

[!TIP] For more examples, feel free to browse around the CERN Analysis Preservation repository, where we use all the features mentioned above.

:space_invader: Local demo & how to contribute

Apart from trying the online demo you can clone the repo and run formule-demo to play around. Follow the instructions in its README: it will explain how to install react-formule as a local dependency so that you can modify Formule and test the changes live in your host app, which will be ideal if you want to troubleshoot or contribute to the project.