|
| 1 | +# STAC-Manager 📡 📄 — Custom app |
| 2 | + |
| 3 | +If you're not using the STAC-Manager client app and want to build your own app using the plugin system, the following guide will help you set up the plugin system in your project. |
| 4 | + |
| 5 | +## Provider |
| 6 | + |
| 7 | +Wrap the `PluginConfigProvider` around the `App` component to provide the plugins with the configuration. |
| 8 | + |
| 9 | +```tsx |
| 10 | +import { PluginConfigProvider } from '@stac-manager/data-core'; |
| 11 | + |
| 12 | + <PluginConfigProvider config={config}> |
| 13 | + <App /> |
| 14 | + </PluginConfigProvider> |
| 15 | +``` |
| 16 | + |
| 17 | +## Config |
| 18 | + |
| 19 | +The config object should contain a list of plugins to use for the collections and items, as well as the widget configuration (which widgets to use for which field types). |
| 20 | + |
| 21 | +```js |
| 22 | +{ |
| 23 | + collectionPlugins: [ |
| 24 | + new PluginOne(), |
| 25 | + new PluginTwo(), |
| 26 | + ], |
| 27 | + itemPlugins: [ |
| 28 | + new PluginTwoB(), |
| 29 | + ], |
| 30 | + |
| 31 | + 'ui:widget': { |
| 32 | + 'customWidget': CustomWidgetComponent, |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +It is possible to extend the default widget configuration with the `extendPluginConfig` function. The default configuration already defines the widgets for the basic field types. |
| 38 | + |
| 39 | +```ts |
| 40 | +import { extendPluginConfig } from '@stac-manager/data-core'; |
| 41 | +import { defaultPluginWidgetConfig } from '@stac-manager/data-widgets'; |
| 42 | + |
| 43 | +export const config = extendPluginConfig(defaultPluginWidgetConfig, { |
| 44 | + collectionPlugins: [], |
| 45 | + itemPlugins: [], |
| 46 | + |
| 47 | + 'ui:widget': {} |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +## From |
| 52 | + |
| 53 | +The plugin system is responsible for dynamically generating forms based on a schema definition and uses [Formik](https://formik.org/) to handle the form state. Wrapping the plugins with a formik form is responsibility of the app, not the plugin system. |
| 54 | + |
| 55 | +Below there's a minimal example of all that's needed to use the plugin system. |
| 56 | +```tsx |
| 57 | +import React from 'react'; |
| 58 | +import { |
| 59 | + PluginBox, |
| 60 | + useCollectionPlugins, |
| 61 | + validatePluginsFieldsData, |
| 62 | + WidgetRenderer |
| 63 | +} from '@stac-manager/data-core'; |
| 64 | +import { Formik } from 'formik'; |
| 65 | + |
| 66 | +export function EditForm() { |
| 67 | + // Change this to the initial data of the form. Could come from an API if |
| 68 | + // editing an existing form. |
| 69 | + const initialData = {}; |
| 70 | + |
| 71 | + // The useCollectionPlugins takes the initial data and initializes all the |
| 72 | + // plugins by calling their init method. Which plugins are used is defined in |
| 73 | + // the config file passed to the PluginConfigProvider. |
| 74 | + // Once that's done prepares the form data by calling enterData on each plugin |
| 75 | + // and prepares the toOutData function that will be used to convert the form |
| 76 | + // data back to the original data structure by calling each plugin's exitData |
| 77 | + // method. |
| 78 | + // All this process is asynchronous and the isLoading flag will be true until |
| 79 | + // all everything are ready. |
| 80 | + // NOTE: If items are being edited, useItemPlugins would be used instead. |
| 81 | + const { plugins, formData, toOutData, isLoading } = |
| 82 | + useCollectionPlugins(initialData); |
| 83 | + |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + {isLoading ? ( |
| 87 | + <p>Loading plugins...</p> |
| 88 | + ) : ( |
| 89 | + // STAC-Manager uses Formik for form handling, therefore the form |
| 90 | + // must be wrapped in a Formik component. |
| 91 | + <Formik |
| 92 | + validateOnChange={false} |
| 93 | + enableReinitialize |
| 94 | + initialValues={formData} |
| 95 | + onSubmit={(values, actions) => { |
| 96 | + // Once the form is submitted, the toOutData function must be called |
| 97 | + // to convert the form data back to the original data structure. |
| 98 | + const exitData = toOutData(values); |
| 99 | + }} |
| 100 | + validate={(values) => { |
| 101 | + // validatePluginsFieldsData is a helper function that validates the |
| 102 | + // form data against the plugins schema. |
| 103 | + const [, error] = validatePluginsFieldsData(plugins, values); |
| 104 | + if (error) return error; |
| 105 | + }} |
| 106 | + > |
| 107 | + {({ handleSubmit }) => ( |
| 108 | + <form onSubmit={handleSubmit}> |
| 109 | + {plugins.map((plugin) => ( |
| 110 | + // Each plugin is wrapped in a headless PluginBox component that |
| 111 | + // does some plugin validation and provides each plugin with a |
| 112 | + // PluginProvider context which enables access to the plugin's |
| 113 | + // info through usePlugin hook. |
| 114 | + <PluginBox key={plugin.name} plugin={plugin}> |
| 115 | + {({ field }) => ( |
| 116 | + <div> |
| 117 | + <h2>{plugin.name}</h2> |
| 118 | + { |
| 119 | + // The WidgetRenderer component is used to render the |
| 120 | + // plugin's fields. It will render the correct widget |
| 121 | + // based on the field type provided in the field prop. |
| 122 | + // Since this is the root object, the pointer prop is |
| 123 | + // an empty string. |
| 124 | + } |
| 125 | + <WidgetRenderer pointer='' field={field} /> |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </PluginBox> |
| 129 | + ))} |
| 130 | + </form> |
| 131 | + )} |
| 132 | + </Formik> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + ); |
| 136 | +} |
| 137 | +``` |
0 commit comments