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>
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>
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
Reference
Props
The FormField component inherits all of the props from the Column component and adds the following props.
| Name | Type | Default | Description |
|---|---|---|---|
label | string | - | The label for the form field. |
required | boolean | false | If true, the label will have a required indicator. |
indicator | string | - | An indicator, often used for character counts. |
hint | ReactNode | - | A hint message displayed below the input. |
error | ReactNode | - | An error message that replaces the hint when present. |
disabled | boolean | false | If true, the form field and its contents are disabled. |
form | string | - | The form attribute of the label element. |
htmlFor | string | - | The for attribute of the label element. |
Tokens
| Key | Default Value |
|---|---|
formField.column.base.gap | var(--gpqiqd7) |
formField.label.base.font | var(--_1i4ikbk28) |
formField.label.base.color | var(--_1i4ikbkv) |
formField.label.variants.disabled.true.opacity | var(--_1i4ikbk2i) |
formField.indicator.base.font | var(--_1i4ikbk29) |
formField.indicator.base.color | var(--_1i4ikbkv) |
formField.indicator.variants.disabled.true.opacity | var(--_1i4ikbk2i) |
formField.error.base.font | var(--_1i4ikbk26) |
formField.error.base.color | var(--_1i4ikbkg) |
formField.error.variants.disabled.true.opacity | var(--_1i4ikbk2i) |
formField.hint.base.font | var(--_1i4ikbk26) |
formField.hint.base.color | var(--_1i4ikbkv) |
formField.hint.variants.disabled.true.opacity | var(--_1i4ikbk2i) |