Form
Forms allow users to enter data that can be submitted while providing alignment and styling for form fields.
install | yarn add @adobe/react-spectrum |
---|---|
added | 3.0.0 |
usage | import {Form} from '@adobe/react-spectrum' |
Example#
<Form maxWidth="size-3600">
<TextField label="Email" />
<TextField label="Password" />
<Checkbox>Remember me</Checkbox>
</Form>
<Form maxWidth="size-3600">
<TextField label="Email" />
<TextField label="Password" />
<Checkbox>Remember me</Checkbox>
</Form>
<Form maxWidth="size-3600">
<TextField label="Email" />
<TextField label="Password" />
<Checkbox>
Remember me
</Checkbox>
</Form>
Content#
Form accepts one or more children to render as its form elements. Label alignment, positioning, and other properties are set on the Form itself and propagated to its children to generate a cohesive design. To override these top level Form props on individual children, set them directly on the child instead.
<Form maxWidth="size-3600" isRequired necessityIndicator="label">
<TextField label="Name" />
<TextField label="Email" />
<TextField label="Address" isRequired={false} />
</Form>
<Form
maxWidth="size-3600"
isRequired
necessityIndicator="label"
>
<TextField label="Name" />
<TextField label="Email" />
<TextField label="Address" isRequired={false} />
</Form>
<Form
maxWidth="size-3600"
isRequired
necessityIndicator="label"
>
<TextField label="Name" />
<TextField label="Email" />
<TextField
label="Address"
isRequired={false}
/>
</Form>
Labeling#
Accessibility#
A label should be provided to the Form by adding either the aria-label
or aria-labelledby
prop, so that the element will be identified to assistive
technology as a form landmark region to which assistive technology can navigate.
<h3 id="label-3">Personal Information</h3>
<Form maxWidth="size-3600" aria-labelledby="label-3">
<TextField label="First Name" />
<TextField label="Last Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
<Radio value="dragons">Dragons</Radio>
</RadioGroup>
</Form>
<h3 id="label-3">Personal Information</h3>
<Form maxWidth="size-3600" aria-labelledby="label-3">
<TextField label="First Name" />
<TextField label="Last Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
<Radio value="dragons">Dragons</Radio>
</RadioGroup>
</Form>
<h3 id="label-3">
Personal Information
</h3>
<Form
maxWidth="size-3600"
aria-labelledby="label-3"
>
<TextField label="First Name" />
<TextField label="Last Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
<Radio value="dragons">
Dragons
</Radio>
</RadioGroup>
</Form>
Props#
Name | Type | Default | Description |
children | ReactElement<SpectrumLabelableProps> | ReactElement<SpectrumLabelableProps>[] | — | The contents of the Form. |
isQuiet | boolean | — | Whether the Form elements are displayed with their quiet style. |
isEmphasized | boolean | — | Whether the Form elements are rendered with their emphasized style. |
isDisabled | boolean | — | Whether the Form elements are disabled. |
isRequired | boolean | — | Whether user input is required on each of the Form elements before Form submission. |
isReadOnly | boolean | — | Whether the Form elements can be selected but not changed by the user. |
validationState | ValidationState | 'valid' | Whether the Form elements should display their "valid" or "invalid" visual styling. |
action | string | — | Where to send the form-data when the form is submitted. |
encType | 'application/x-www-form-urlencoded'
| 'multipart/form-data'
| 'text/plain' | — | The enctype attribute specifies how the form-data should be encoded when submitting it to the server. |
method | 'get' | 'post' | — | The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post"). |
target | '_blank'
| '_self'
| '_parent'
| '_top' | — | The target attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form. |
labelPosition | LabelPosition | 'top' | The label's overall position relative to the element it is labeling. |
labelAlign | Alignment | 'start' | The label's horizontal alignment relative to the element it is labeling. |
necessityIndicator | NecessityIndicator | 'icon' | Whether the required state should be shown as an icon or text. |
Events
Name | Type | Default | Description |
onSubmit | FormEventHandler | — | Fired on form submission. |
Layout
Name | Type | Default | Description |
flex | Responsive<string
| number
| boolean> | — | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | — | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | — | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | — | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'center'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'stretch'> | — | Overrides the alignItems property of a flex or grid container. See MDN. |
justifySelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch'> | — | Specifies how the element is justified inside a flex or grid container. See MDN. |
order | Responsive<number> | — | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | — | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | — | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | — | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | — | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | — | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | — | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | — | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
Spacing
Name | Type | Default | Description |
margin | Responsive<DimensionValue> | — | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | — | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | — | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | — | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | — | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | — | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | — | The margin for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Default | Description |
width | Responsive<DimensionValue> | — | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | — | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | — | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | — | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | — | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | — | The maximum height of the element. See MDN. |
Positioning
Name | Type | Default | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | — | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | — | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | — | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | — | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | — | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | — | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | — | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | — | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | — | Hides the element. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Advanced
Name | Type | Default | Description |
UNSAFE_className | string | — | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | — | Sets inline style for the element. Only use as a last resort. Use style props instead. |
Visual options#
Label position and alignment#
Top label position, start label alignment
<Form
labelPosition="top"
labelAlign="start"
aria-label="Top position, start alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="top"
labelAlign="start"
aria-label="Top position, start alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="top"
labelAlign="start"
aria-label="Top position, start alignment example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Side label position, start label alignment
<Form
labelPosition="side"
labelAlign="start"
aria-label="Side position, start alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="side"
labelAlign="start"
aria-label="Side position, start alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="side"
labelAlign="start"
aria-label="Side position, start alignment example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Side label position, end label alignment
<Form
labelPosition="side"
labelAlign="end"
aria-label="Side position, end alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="side"
labelAlign="end"
aria-label="Side position, end alignment example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
labelPosition="side"
labelAlign="end"
aria-label="Side position, end alignment example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Quiet#
<Form
isQuiet
aria-label="Quiet example"
maxWidth="size-3600">
<TextField label="Name" />
<TextField label="Address" />
</Form>
<Form
isQuiet
aria-label="Quiet example"
maxWidth="size-3600">
<TextField label="Name" />
<TextField label="Address" />
</Form>
<Form
isQuiet
aria-label="Quiet example"
maxWidth="size-3600"
>
<TextField label="Name" />
<TextField label="Address" />
</Form>
Emphasized#
<Form
isEmphasized
aria-label="Emphasized example"
maxWidth="size-3600">
<TextField label="Name"/>
<RadioGroup label="Favorite pet" defaultValue="dogs">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isEmphasized
aria-label="Emphasized example"
maxWidth="size-3600">
<TextField label="Name"/>
<RadioGroup label="Favorite pet" defaultValue="dogs">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isEmphasized
aria-label="Emphasized example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup
label="Favorite pet"
defaultValue="dogs"
>
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Disabled#
<Form
isDisabled
aria-label="Disabled example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isDisabled
aria-label="Disabled example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isDisabled
aria-label="Disabled example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Required and necessity indicator#
Optional label
<Form
necessityIndicator="label"
aria-label="Optional with label example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
necessityIndicator="label"
aria-label="Optional with label example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
necessityIndicator="label"
aria-label="Optional with label example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Required label
<Form
isRequired
necessityIndicator="label"
aria-label="Required with label example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isRequired
necessityIndicator="label"
aria-label="Required with label example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isRequired
necessityIndicator="label"
aria-label="Required with label example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Required with asterisk
<Form
isRequired
necessityIndicator="icon"
aria-label="Required with asterisk example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isRequired
necessityIndicator="icon"
aria-label="Required with asterisk example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isRequired
necessityIndicator="icon"
aria-label="Required with asterisk example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Read only#
<Form
isReadOnly
aria-label="isReadOnly example"
maxWidth="size-3600">
<TextField label="Name" value="John Smith" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isReadOnly
aria-label="isReadOnly example"
maxWidth="size-3600">
<TextField label="Name" value="John Smith" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
isReadOnly
aria-label="isReadOnly example"
maxWidth="size-3600"
>
<TextField
label="Name"
value="John Smith"
/>
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
Validation state#
<Form
validationState="invalid"
aria-label="Invalid validationState example"
maxWidth="size-3600"
marginBottom="size-300">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
validationState="invalid"
aria-label="Invalid validationState example"
maxWidth="size-3600"
marginBottom="size-300">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
validationState="invalid"
aria-label="Invalid validationState example"
maxWidth="size-3600"
marginBottom="size-300"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>
<Form
validationState="valid"
aria-label="Valid validationState example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
validationState="valid"
aria-label="Valid validationState example"
maxWidth="size-3600">
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</RadioGroup>
</Form>
<Form
validationState="valid"
aria-label="Valid validationState example"
maxWidth="size-3600"
>
<TextField label="Name" />
<RadioGroup label="Favorite pet">
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</RadioGroup>
</Form>