DataForm
Basic Example
You can describe each field as an object that is passed into the DataForm's fields
prop. The DataForm will then handle rendering and configuring each field for you.
For each field object you need to provide a Kyber form component name for the as
key to tell the DataForm which component you would like to use. For example { as: 'TextField' }
will render the TextField component. You also need to provide values for the name
and fieldLabel
keys. The name
will be the key used in the returned form data. The fieldLabel
will be the label displayed above the field. All other key/value pairs are treated as props that will be spread onto the component. See each component's documentation for which props are available.
All Fields
Validation
Yup is a powerful validation library that can be used with the DataForm component. There are a lot of options and ways that Yup validation schemas can be constructed. Review the Yup Documentation for full details.
This example shows basic validation making each field required.
Cross Field Validation
Using Yup that showcases one way to handle cross field validation. Note the ref
to the age
field in the retirement_age
validation schema.
Required with Disabled Field
Using Yup to change the conditionality of field validation based on changes outside of the form. In this case toggling the disabled state of the Retirement Age field should allow the form to submit with optional validation to that field.
Add/Remove Field
Click the "Toggle Retirement Field" button to add/remove the "Retirement Age" field. Note how when the field is present it is required, but when it's removed the form can submit without it.
All Fields With Yup Validation
Custom Footer
Same as the regular Kyber Form component you can customize the form footer with the footerRenderer
prop. The function passed to the footerRenderer
prop will receive all the DataForm
props as an argument. This allows you submit the form with a custom button. The type="submit"
prop should be used on a button to handle keyboard submission.
Using the Form Ref
The Form's ref
prop will return a reference to the Formik instance. This gives you access to Formik's props and methods which let you control the form's state manually. For example this can be used to reset the form and clear out the field values.
Analytics
The DataForm component is trackable through Kyber Analytics. This is the default analytics config.
export default {
value: 'Form',
actions: {
onSubmit: { type: 'FORM_SUBMIT', payload: 'Submit' },
},
};