Hone UI
Components

FormControl and form parts

Share state between a label, an input and helper text.

FormControl shares focus, error, disabled and filled state with the parts inside it. Those parts are FormLabel, InputLabel and FormHelperText. Use them around a custom input. For a text input use TextField, which already does this. All four are client components and read theme defaultProps.

import FormControl, { useFormControl } from '@hone-ui/react/FormControl';
import FormLabel from '@hone-ui/react/FormLabel';
import InputLabel from '@hone-ui/react/InputLabel';
import FormHelperText from '@hone-ui/react/FormHelperText';
import Select from '@hone-ui/react/Select';
import MenuItem from '@hone-ui/react/MenuItem';

Examples

Required

<FormControl error sx={{ minWidth: 200 }}>
  <InputLabel id="demo-age-label">Age</InputLabel>
  <Select labelId="demo-age-label" label="Age" value="" aria-describedby="demo-age-help">
    <MenuItem value={10}>Ten</MenuItem>
  </Select>
  <FormHelperText id="demo-age-help">Required</FormHelperText>
</FormControl>

More variations:

<FormControl error sx={{ minWidth: 200 }}>
  <InputLabel id="a-label">Age</InputLabel>
  <Select labelId="a-label" label="Age" value="" aria-describedby="a-help">
    <MenuItem value={10}>Ten</MenuItem>
  </Select>
  <FormHelperText id="a-help">Required</FormHelperText>
</FormControl>

<FormControl required>
  <FormLabel id="g">Gender</FormLabel>
  <RadioGroup aria-labelledby="g">...</RadioGroup>
</FormControl>

<FormControl component="fieldset">
  <FormLabel component="legend">Pets</FormLabel>
  <FormGroup>...</FormGroup>
</FormControl>

<FormControl variant="filled" sx={{ minWidth: 200 }}>
  <InputLabel id="n-label">Size</InputLabel>
  <Select labelId="n-label" value="">
    <MenuItem value="s">Small</MenuItem>
    <MenuItem value="l">Large</MenuItem>
  </Select>
</FormControl>

FormControl props

PropTypeDefaultDescription
variant'outlined', 'filled', 'standard''outlined'Passed to the parts inside.
size'small', 'medium''medium'Passed to the parts inside.
margin'none', 'dense', 'normal''none'Vertical margin.
colorpalette color'primary'The focused color.
error, disabled, required, fullWidth, hiddenLabelbooleanfalseState shared with the parts.
focusedbooleannoneForces the focused state. Unset, the input's focus drives it.
componentElementType'div'The root element. Use 'fieldset' for a group of inputs.
sx, className, style, ref, childrenStandard. They go to the root element.

useFormControl() returns the shared state, or undefined outside a FormControl. It holds focused, filled, adornedStart and the callbacks onFocus, onBlur, onFilled, onEmpty and setAdornedStart.

FormLabel props

color, disabled, error, filled, focused and required. Each falls back to the FormControl. component picks the element (default label); legend renders no htmlFor. required adds an asterisk hidden from screen readers. By default it renders a label, so link it with htmlFor or aria-labelledby.

InputLabel props

PropTypeDefaultDescription
shrinkbooleanautomaticUnset, the label shrinks on focus, with a value, or with a start adornment.
disableAnimationbooleanfalseSkips the shrink transition.
variant, sizefrom FormControlAs in FormControl. margin is accepted and ignored.

It also takes every FormLabel prop. In the Material look the label floats over the input. The Minimal look keeps it above the input and never floats it.

FormHelperText props

PropTypeDefaultDescription
childrenReactNodenoneA single space keeps the line's height without visible text.
disabled, error, filled, focused, requiredbooleanfrom FormControlState.
margin'dense'from FormControlTightens the top margin.
variant'outlined', 'filled', 'standard'from FormControlOutlined and filled inset the text.

Notes

  • FormHelperText renders a p. Its component prop picks another tag, such as div. InputLabel has no component prop.

  • FormControl adds no ARIA. Link the label with htmlFor and the helper text with aria-describedby yourself.

  • Do not give a control inside a form the id or name reset. The element shadows form.reset, and React Aria's form validation then throws TypeError: Cannot redefine property: reset. Pick another id, such as reset-form.

Accessibility

Without error or a surrounding FormControl, FormHelperText does not look like an error.

On this page