Form Control
The FormControl
component adds a label and caption for its child control.
If an error message is passed it renders that message in place of the caption.
Note, the FormControl
expects a single child component.
If that child has an id
prop, the label
in the form control will assign the htmlFor
accordingly.
Examples
Note, red dot appears when required attribute is set. Form Control does not validate input, that has to be handled by consumer.
A common use case for an input on a form is to do some quick client side validation. Here, we are using the validate method from the email-validator
package to track and update error states for both the form control and input components. This particular example shows the input in the context of a <form>
element to show how the components' custom error states can compliment the browser's native form validation. The default form behavior is prevented so no network requests are sent on submit.
Note, for a Checkbox
, the actual label is the accompanying text on the right side of the checkbox element.
Note, for a Switch
, the actual label is the accompanying text on the right side of the switch element.
Note, for a Radio
/RadioGroup
, the actual label is the accompanying text on the right side of the radio element.
Caption and Error
Caption with intent error is similar to error.
API
Form Control props
Name | Type | Default | Description |
---|---|---|---|
caption | function | A message rendered below the form field. Caption will get styles based on intent. | |
children | react node | Content wrapped by FormControl. | |
className | string | Utility class names to be provided to form-control root | |
disabled | boolean | false | Displays label in light gray color if true. |
error | string | An error message rendered below the caption (if provided) or form field. | |
intent | 'default' | 'success' | 'warning' | 'error' | default | Defines the intent of the Input. |
label | function | A label rendered above the input field. | |
layout | 'vertical' | 'horizontal' | vertical | Defines the layout of the field |
overrides | custom | Lets you customize all aspects of the component. | |
required | boolean | false | If form property is required, display red dot beside Label. |
subText | function | A text rendered b/w label and the form field. Info Icon will be rendered for horizontal layout. | |
subTextAsIcon | boolean | false | Render info icon beside label. Displays subText in tooltip. For horizontal layout subText will be rendered as icon only. |
You can check the implementation of the component for more details.
Form Control exports
You can import this module like so:
import {FormControl} from 'spaceweb/form-control'
It exports the following components or utility functions:
- FormControl
- StyledCaption
- StyledControlContainer
- StyledInfoIconContainer
- StyledLabel
- StyledLabelContainer
- StyledRequiredDot
- StyledRoot
- StyledSubText