Form
A conditionally stateful Form wrapper. This component maintains state and validation of any child FormField components. It utilizes Formik and can receive props related to configuring Formik.
Validation Sync and Async Form and Field level validation is available through the validate prop.
import { Alert } from '@emoney/kyber';
<Alert>
Check out the <a href="#/Tutorials/Intro%20To%20Forms">Intro To Forms Tutorial</a> to get
familiar with how forms work in React with Kyber.
</Alert>;
Basic Example
Wrap the Kyber form field component you would like to use in a FormField and place it inside the Form component. You will need to provide an id
and label
prop to the FormField. Set the name
prop on the field component, this will be used as the key in the returned data.
Validation
The Form and related FormField components support multiple types of validation.
The preferred order of validation is:
- Form level schema validation with yup.
- Form level synchronous validation.
- Form level asynchronous validation.
- Field level synchronous validation (See FormField).
- Field level synchronous validation (See FormField).
Validation Library
Yup is a powerful validation library that can be used with the Form component. There are a lot of options and ways that Yup validation schemas can be constructed. Review the Yup Documentation for full details.
Form Level Schema Validation
Sync Form Level Validation
Async Form Level Validation
Other Validation Uses
Toggling Field Validation
Sometimes it may be necessary to alter form behavior based on local or external state. A common case is that validation changes based on state changes.
You can do this by using Yup's when method to conditionaly add validation based on the value of another field.
Cross Field Validation
Custom footer
You can also replace the Form's footer by using the footerRenderer
prop on the Form
.
Alternatively you can set this prop to null
to disable the footer entirely.
Note: If you add type="submit"
to one of your buttons it will be used as the submit button.
Further Customization
With children As A Render Prop
See Formik for more details about the props that are forwarded to children.
Using Formik Context
It's possible to directly use the Formik context within a subcomponent of a Form. Either as the Formik published hook or through direct access with of the FormikContext object and useContext hook.
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.
Resetting the Form
Nested Values
Analytics
The Form component is trackable through Kyber Analytics. This is the default analytics config.
export default {
value: 'Form',
actions: {
onSubmit: { type: 'FORM_SUBMIT', payload: 'Submit' },
},
};