Skip to main content

DataForm

Check out the Intro To Forms Tutorial to get familia with how forms work in React with Kyber.

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.

Result
Loading...
Live Editor

All Fields

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

All Fields With Yup Validation

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

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.

Result
Loading...
Live Editor

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' },
},
};

Props