diff --git a/README.md b/README.md index 5296e05..b840cca 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,117 @@ -# React Formule +

-![Formule](public/logo.png) +## :horse: What is Formule? + +Formule is a **powerful, user-friendly, extensible and mobile-friendly form building library** based on [JSON Schema](https://json-schema.org/) and [RJSF](https://github.com/rjsf-team/react-jsonschema-form), 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](https://github.com/cernanalysispreservation/analysispreservation.cern.ch) 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: + +- **`FormuleContext`**: Formule components need to be wrapped by a FormuleContext. It also allows you to provide an antd theme and your own custom fields and widgets. +- The form editor, which has been split into three different components that work together for more flexibility: + - **`SelectOrEdit`** (or, separately, **`SelectFieldType`** and **`PropertyEditor`**): You can select fields to add to the form and customize their properties. + - **`SchemaPreview`**: A tree view of the fields where you can rearrange or select fields to be edited. + - **`FormPreview`**: A live, iteractive preview of the form. +- **`FormuleForm`**: You can use it to display a form (JSON Schema) generated by Formule. + +It also exports the following functions: + +- **`initFormuleSchema`**: Inits the JSONSchema, *needs* to be run on startup. +- **`getFormuleState`**: Formule has its own internal redux state. You can retrieve it at any moment if you so require for more advanced use cases. If you want to continuosly synchronize the Formule state in your app, you can pass a callback function to FormuleContext instead (see below), which will be called every time the form state changes. + +### Field types + +Formule includes a variety of predefined field types, grouped in three categories: +- **Simple fields**: `Text`, `Text area`, `Number`, `Checkbox`, `Switch`, `Radio`, `Select` and `Date` fields. +- **Collections**: + - `Object`: Use it of you want to group fields or to add several of them inside of a `List`. + - `List`: It allows you to have as many instances of a field or `Object` as you want. + - `Accordion`: When containing a `List`, it works as a `List` with collapsible entries. + - `Layer`: When containing a `List`, it works as a `List` whose entries will open in a dialog window. + - `Tab`: It's commonly supposed to be used as a wrapper around the rest of the elements. You will normally want to add an `Object` inside and you can use it to separate the form in different pages or sections. +- **Advanced fields**: More complex or situational fields such as `URI`, `Rich/Latex editor`, `Tags` and `ID Fetcher`. + +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 +```sh +npm install react-formule +# or +yarn add react-formule +``` + +### Basic setup +```jsx +import { + FormuleContext, + SelectOrEdit, + SchemaPreview, + FormPreview, + initFormuleSchema +} from "react-formule"; + +const useEffect(() => initFormuleSchema(), []); + + + + + + +``` + +### Customizing and adding new field types +```jsx + +// ... + +``` + +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: + +```jsx +const transformSchema = (schema) => { + // Remove properties... + return transformedSchema +} + + +// ... + +``` + +### 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: + +```jsx +const handleFormuleStateChange = newState => { + // Do something when the state changes +} + + +// ... + +``` + +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](https://github.com/cernanalysispreservation/analysispreservation.cern.ch) repository, where we use all the features mentioned above. + +## :space_invader: Local demo & how to contribute +You can also clone the repo and run `formule-demo` to play around. Follow the instructions in its [README](./formule-demo/README.md): it will explain how to install `react-formule` as a local dependency (with either `yarn link` or, better, `yalc`) 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. \ No newline at end of file diff --git a/docs/demo.gif b/docs/demo.gif new file mode 100644 index 0000000..740721f Binary files /dev/null and b/docs/demo.gif differ diff --git a/docs/logo.png b/docs/logo.png new file mode 100644 index 0000000..fb11c8b Binary files /dev/null and b/docs/logo.png differ diff --git a/formule-demo/README.md b/formule-demo/README.md index 51057d7..931e091 100644 --- a/formule-demo/README.md +++ b/formule-demo/README.md @@ -6,7 +6,7 @@ This is a small application that serves as a playground to test react-formule. ### The easy way -Run `yarn link-local` in react-formule, then run `yarn link-local-lib` and `yarn install` in formule-demo. To run the playground app, execute `yarn dev`. You will see any changes in react-formule immediately in the playground app. +Run `yarn link-local` in react-formule, then run `yarn link-local-lib` and `yarn install` in formule-demo. To run the playground app, execute `yarn dev` and visit `localhost:3030`. You will see any changes in react-formule immediately in the playground app. ### The advanced way diff --git a/package.json b/package.json index 70c774e..3685fce 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,10 @@ "version": "0.1.0", "author": "CERN", "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/cern-sis/react-formule.git" + }, "type": "module", "files": [ "dist" diff --git a/public/logo.png b/public/logo.png deleted file mode 100644 index 04c4052..0000000 Binary files a/public/logo.png and /dev/null differ diff --git a/vite.config.ts b/vite.config.ts index 2c53363..31fc09c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -25,7 +25,7 @@ export default defineConfig({ rollupOptions: { // make sure to externalize deps that shouldn't be bundled // into your library - external: ["react"], + external: ["react", "./docs"], }, },