MobxSchemaForm
MobxSchemaForm is a React component which renders form fields as declared in json schema config object it receives. It supports the following props:
prop
Data type
Description
model
schema
Object
Describes the data properties of the model
schema.type
String
Per json-schema-form standard, this should be "object"
schema.title
String
Optional - arbitrary title for this schema - currently not used anywhere
schema.properties
Object
Each data property should have a key in this object. mobx-schema-form also supports deep/nested objects which are referenced in "parent.child" format but there are limitations in MobX FormStore when using non-flat schemas.
schema.properties[key]
Object
schema.required
Array
Optional unordered array of field names (i.e. keys in schema.properties object) listing data properties that are considered required.
form
Array
options
Object
Passed to react-schema-form SchemaForm in option prop.
options.suppressPropertyTitles (in react-schema-form it's supress, one p)
Boolean
options.formDefaults
Object
options.validators (*)
ignore
Object
Optional object with stringyfied schema paths as keys and true as values for this schema path to be ignored.
onModelChange
Function that receives the model key (i.e. the key in form metadata object or the modelKey in the corresponding schema object if defined) and the value
Optional callback that will be called whenever a widget raises an onChange event or a widget is first mounted or dismounted (in which case value will be undefined).
mapper
Object with each key referencing a React component/function
mergeMapper (*)
Boolean
If explicitly set to false (not undefined), provided mapper object will replace the default React-Toolbox-based mapper entirely instead of merging into it. This is useful to prevent MobxSchemaForm from requiring React-Toolbox components completely and allows you to use MobxSchemaForm without an environment configured for SASS-loading as required by React-Toolbox.
asArray (**)
Boolean
If true, the component will return an array of rendered fields without wrapping them in a div (requires React 16.0+ if used without a child component (see below)). Otherwise, just like the original react-schema-form <0.5.0, it will wrap the fields in a div with style=width:100% and className=SchemaForm. Note that this array is not really an array of components but a React Fragment and currently cannot be iterated/traversed.
className (***)
String
Optional class name for the top-level div. Defaults to "SchemaForm".
children (****)
React Element
Optional single child component to which fields are passed as children, so it can further wrap each field if necessary. The Child component will go inside the SchemaForm div unless asArray=true.
* = feature not present in react-schema-form. ** = new feature in v1.6. *** = new feature in v1.7. **** = new feature in v1.8.
model.fields
It is often desired to call model.refresh() when MobxSchemaForm mounts to retrieve from server existing data that needs to be edited in the form. Be sure to do this in componentDidMount() of your form so that model.fields are in place when the FormStore afterRefresh callback runs, so that this callback can do:
This runs form-wide validation on already filled fields (though usually server won't return invalid data back) but more importantly it will also re-populate model.data properties that got 'refreshed' with null to defaults defined in the schema.
Note that starting with v1.6, if you mount multiple instances of MobxSchemaForm against the same FormStore model, whole-form validation will validate all fields from all mounted instances that use this model.
model.validators
MobxSchemaForm will merge options.validators object it receives if any into model.validators.
Last updated
Was this helpful?