Skip to Content
Referencefusion-uiComponentsFormsFormField

FormField

The FormField component is a wrapper that provides a consistent layout for form elements. It adds a label, hint, error message, and an indicator to its child component.


<FormField
  label="Stilt Sandpiper"
  indicator="Worm-eating Warbler"
  hint="Deporto amicitia civitas paulatim cunabula defetiscor depraedor contra."
>
  <Placeholder size={{ height: 300 }} />
</FormField>

Features

The FormField component has several features to enhance user experience.

Hint

You can provide a hint to the user to help them fill out the form field with the correct information.


<FormField label="Username" hint="Only letters and numbers are allowed.">
  <Input />
</FormField>

Indicator

You can add an indicator, which is useful for things like character counters.


<FormField label="Bio" indicator="13 of 200">
  <TextArea
    placeholder="Tell us about yourself..."
    defaultValue="Hello, world!"
    maxLength={200}
  />
</FormField>

Error

You can provide an error message that will replace the hint when the form field is in an error state.


<FormField label="Email" error="Please enter a valid email address.">
  <Input defaultValue="example.com" error />
</FormField>
Warning

Any child components will not automatically be styled to indicate an error, so you will need to take care of this yourself if needed. The Input component supports an error prop that you can use for this purpose.

Disabled

You can apply a disabled state to the form field, which will only affect the visual appearance of the form field’s own elements.


<FormField label="Age" hint="You cannot change this." disabled>
  <Input placeholder="25" disabled />
</FormField>
Warning

Any child components will not automatically become disabled, so you will need to take care of this yourself. Most interactive components support a disabled prop that you can use for this purpose.

Examples

The FormField component can wrap any form control.

With Select


() => {
  const [value, setValue] = React.useState();

  return (
    <FormField label="Fruit">
      <Select
        placeholder="Select a fruit"
        options={[
          { value: "apple", title: "Apple" },
          { value: "banana", title: "Banana" },
          { value: "cherry", title: "Cherry" },
        ]}
        value={value}
        onChange={setValue}
      />
    </FormField>
  );

}

With Checkbox


() => {
  const [checked, setChecked] = React.useState(false);

  const error = !checked && "Please accept the terms and conditions.";

  return (
    <FormField label="Terms & Conditions" error={error}>
      <Checkbox
        label="I have read and agree to the terms and conditions."
        checked={checked}
        onChange={setChecked}
        error={!!error}
      />
    </FormField>
  );

}

With Slider


() => {
  const [value, setValue] = React.useState(0.5);

  return (
    <FormField label="Volume" indicator={Math.round(value * 100)}>
      <Slider value={value} onChange={setValue} />
    </FormField>
  );

}

Anatomy

1.FormFieldThe root is a `Column` component.
2.LabelThe label is a `Text` component displayed at the top.
3.IndicatorThe indicator is a `Text` component, aligned to the end of the label.
4.HintThe hint is a `Text` component displayed below the input.
5.ErrorThe error is a `Text` component that replaces the hint when there is an error.

Reference

Props

The FormField component inherits all of the props from the Column component and adds the following props.

NameTypeDefaultDescription
labelstring-The label for the form field.
requiredbooleanfalseIf true, the label will have a required indicator.
indicatorstring-An indicator, often used for character counts.
hintReactNode-A hint message displayed below the input.
errorReactNode-An error message that replaces the hint when present.
disabledbooleanfalseIf true, the form field and its contents are disabled.
formstring-The form attribute of the label element.
htmlForstring-The for attribute of the label element.

Tokens

KeyDefault Value
formField.column.base.gapvar(--gpqiqd7)
formField.label.base.fontvar(--_1i4ikbk28)
formField.label.base.colorvar(--_1i4ikbkv)
formField.label.variants.disabled.true.opacityvar(--_1i4ikbk2i)
formField.indicator.base.fontvar(--_1i4ikbk29)
formField.indicator.base.colorvar(--_1i4ikbkv)
formField.indicator.variants.disabled.true.opacityvar(--_1i4ikbk2i)
formField.error.base.fontvar(--_1i4ikbk26)
formField.error.base.colorvar(--_1i4ikbkg)
formField.error.variants.disabled.true.opacityvar(--_1i4ikbk2i)
formField.hint.base.fontvar(--_1i4ikbk26)
formField.hint.base.colorvar(--_1i4ikbkv)
formField.hint.variants.disabled.true.opacityvar(--_1i4ikbk2i)
Last updated on