Intro
The @emoney/kyber-forms package can be used to create any type of form. The examples in this readme are oriented from simple to complex.
Fields
Kyber's form components are stateless high level components that encapsulate serialization and validation. Most of them can be used uncontrolled or controlled. Each field component has the following shared props:
Prop Name | Type | Description |
---|---|---|
desc | string | The description for the field. |
disabled | boolean | Whether or not the field is disabled. |
errorText | string | The error text to display if isInvalid is true. |
isInvalid | boolean | Whether or not the field is invalid. |
isValid | boolean | Whether or not the field is valid. |
isWarning | boolean | Whether or not the field is warning. |
label | string | The label for the field. |
name | string | The name of the field. This is used to serialize the form data. |
radius | string | The radius of the field. One of round , square , or pill . |
readOnly | boolean | Whether or not the field is read only. |
renderEndAddOn | function | A function that returns a React node to render at the end of the field. Use one InputGroup component, Button, or Dropdown. |
renderPrefix | function | A function that returns a React node. |
renderStartAddOn | function | A function that returns a React node to render at the start of the field. Use one InputGroup component, Button, or Dropdown. |
renderSuffix | function | A function that returns a React node. |
size | string | The size of the field. One of sm , md , or lg . |
validText | string | The valid text to display if isValid is true. |
warningText | string | The warning text to display if isWarning is true. |
Validation States
Addons, Prefixes, and Suffixes
Multiple prefixes or suffixes
Some field components (e.g. NumberField) utilize prefixes or suffixes to display controls like the plus and minus spinner. In these cases it may be necessary to set the flex order of your prefix/suffix to make sure it's displayed correctly.
Sizes
The size prop can be used to change the size of the field. The default size is md
.
Radii
The radius prop can control the corner styling of any field component. The default is round
. The other options are square
and pill
.
Usage
Form Component
If you need more control over how the form inputs are rendered (E.g. columns or other custom layouts) you can use the individual form components inside of a Kyber Form component. The Form component will still help you manage state and validation while you have direct access to each input component.
DataForm Component
If your form is straightforward you can build it using just the DataForm component and an object based schema.
DataForm is only available in the @emoney/kyber-forms
package.
Self Managed
If you need complete control over the form you can manage state and validation yourself by importing the individual form components and combining them with third party form management and validation libraries. The Kyber team recommends Formik and Yup, but other libraries like React Hook Form can also work.
Note: This example uses the HTML <form>
element, not the Kyber Form component.