Form
Public API
actions
actions
represent verbs you want to make available to the User, like
View Terms and Conditions to display a popup with such information.
Another example would be Delete to delete an entity instance represented by data
. actions
will be displayed as buttons at the bottom of the Form.
Form does not provide the logic to handle
actions
, the implementation for deleting a entity insntances, display popups, etc. it's up to you.
By defining an action, you are telling Form how you want to be notified when your action button is pressed. Via the onAction
event.
modules
At the moment, we have only two types of modules.
- button, for simple actions like resetting the form, or display terms and conditions, etc
- edit, to be used if you want the form to become editable by the action of the User.
For button, a single onAction
event will be emitted whenever the button is clicked.
For edit, the onAction
event will be emitted up to 3 times as it creates extra buttons:
- edit.start when entering in edit mode
- edit.cancel when exiting the edit mode via the cancel button
- edit.save when exiting the edit mode via the save button
As with Table all this information is part of the onAction event handler signature, please make sure you review it as well.
An example actions
prop:
data
You can pass your own data, Form will then use it as the initial values for any fields it generates from the modules you describe in dataDisplay
.
An example data shape:
By passing the data
prop, you are basically describing the initial state of the data you will get back when Form emits an onAction
event, because you will get a merge of the data
object you passed plus the changes made by User.
Hidden fields - Every property of
data
that is not used by any module in the form will still be part of the data you'll get when anonAction
event is emitted. In that sense, those act as hidden fields.
dataDisplay
An Array of module objects. Every object in this array can potentially be rendered in the form. Modules have a special shape too, the good news is that you have plenty more options to choose from. Please choose the one that best fits your data type.
supported data types (modules)
example
In this example we are displaying all the properties in data
except the id, because we want it to be hidden.
But it's up to you how many and which properties to display:
isInitialEditing
By default, Form starts in View mode, that means that it displays the non-editable version of your data. Pass this boolean prop if you want your Form to start in Edit mode.
onAction (event handler)
A function you can pass to handle every event your Form emits. Every event carries two pieces of information:
- action, the object (button) that was actually clicked and
- data, the object that represents
data
you initially passed in, and the changes made by the User.
tip: do not bloat your Form! keep the number of buttons low, specially if you are targetting mobile devices.
Mixing everyting together
With the props described above, we can easily create a Form: (open your dev tools!)
Everything together!