|
1 | 1 | This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app). |
2 | 2 | # JSON Forms React seed App |
3 | | -This seed demonstrates how to use JSON Forms with React. |
| 3 | +This seed demonstrates how to use [JSON Forms](https://jsonforms.io) with React |
| 4 | +in order to render a simple form for displaying a task entity. |
| 5 | + |
| 6 | +It is based on create-react-app and only contains minor modifications. |
| 7 | + |
| 8 | +## File Structure |
| 9 | +Let's briefly have a look at the most important files: |
| 10 | +* `src/schema.json` contains the JSON schema (also referred to as 'data schema') |
| 11 | +* `src/uischema.json` contains the UI schema |
| 12 | +* `src/index.js` is the entry point of the application and sets up the redux store |
| 13 | + that contains the data, the JSON and the UI schema necessary for JSON Forms. |
| 14 | +* `src/App.js` is the main React component and makes use of the JSON Forms Component |
| 15 | + in order to render a form. |
| 16 | + |
| 17 | +The [data schema](https://github.com/edgarmueller/jsonforms-react-seed/blob/master/src/schema.json) defines |
| 18 | +the structure of a Task: it contains attributes such as title, description, due date and so on. |
| 19 | + |
| 20 | +The [corresponding UI schema](https://github.com/edgarmueller/jsonforms-react-seed/blob/master/src/uischema.json) |
| 21 | +specifies controls for each property and puts them into a vertical layout that in turn contains two |
| 22 | +horizontal layouts. |
| 23 | + |
| 24 | +Both the data schema and the UI schema are imported within `index.js` and are used |
| 25 | +to set up a redux store. We make use of a helper function exported by JSON Forms |
| 26 | +which expects the initial state. If you already have an existing redux store, |
| 27 | +you'll need to import the jsonforms reducer and add it to your store. |
| 28 | +Please refer to TODO for how to do this. |
| 29 | + |
| 30 | +## Setting up the store |
| 31 | + |
| 32 | +```js |
| 33 | +import schema from './schema.json'; |
| 34 | +import uischema from './uischema.json'; |
| 35 | + |
| 36 | +// TODO: update JSONForms to most recent version to support new signature |
| 37 | +const store = initJsonFormsStore({ |
| 38 | + data, |
| 39 | + schema, |
| 40 | + uischema, |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +We then use the `Provider` component provided by `react-redux` to provide the store to the |
| 45 | +`App` component and all its children. |
| 46 | + |
| 47 | +```js |
| 48 | +ReactDOM.render( |
| 49 | + <Provider store={store}> |
| 50 | + <App /> |
| 51 | + </Provider>, |
| 52 | + document.getElementById('root') |
| 53 | +); |
| 54 | +``` |
| 55 | + |
| 56 | +## Rendering our form |
| 57 | +The `App` component is responsible for rendering our actual form. |
| 58 | +It does so by importing and using `DispatchRenderer` from `@jsonforms/core`. |
| 59 | +`DispatchRenderer` expects `schema` and `uischema` props which define |
| 60 | +the form to be rendered but if those are omitted, they will be pulled from the |
| 61 | +store which was provided via `Provider` previously. |
| 62 | + |
| 63 | +Since we also like to display the actual data that is being edited we |
| 64 | +`connect` the `App` component to the store in order to extract the data |
| 65 | +from it. |
| 66 | + |
| 67 | +## Custom renderers |
| 68 | +Please see our corresponding tutorial (TODO) on how to add custom renderers. |
0 commit comments