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

Input

Input caption

Select

Select Subtext
AliceBlue
Selected AliceBlue.
open
Select caption

Required input

Select...
open

Horizontal

SubText

Help Text will be rendered b/w Label and Field

Note, red dot appears when required attribute is set. Form Control does not validate input, that has to be handled by consumer.

Textarea

Textarea caption

Validation example (with email)

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.

Checkbox

Checkbox caption

Note, for a Checkbox, the actual label is the accompanying text on the right side of the checkbox element.

Switch

Switch caption

Note, for a Switch, the actual label is the accompanying text on the right side of the switch element.

RadioGroup

RadioGroup caption

Note, for a Radio/RadioGroup, the actual label is the accompanying text on the right side of the radio element.

Caption and Error

Input caption

Caption with intent error

Error caption

Caption with warning intent
Error caption

Caption with default intent
Success caption

Caption with intent error is similar to error.

API

Form Control props

NameTypeDefaultDescription
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
falseDisplays 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'
defaultDefines the intent of the Input.
label
function
A label rendered above the input field.
layout
'vertical'| 'horizontal'
verticalDefines the layout of the field
overrides
custom
Lets you customize all aspects of the component.
required
boolean
falseIf 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
falseRender 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