Use Form...
- To provide a standardized layout for one or more input elements (e.g., Text Input, Dropdown, Checkbox).
The React components for Form provide accessibility support automatically.
Standard Form
Some props/prop values deprecated in SWAN v3.3 and v3.13.0
Refer to the information on props/prop values provided further down this page. Find more information and migration documentation in the Deprecation roadmap.
Use StandardForm to provide a standardized layout and spacing for a series of FormInputGroup components and their labels.
The preview has been updated.
Form Field
FormField is a utility component whose purpose is to associate an input with its label and helper/error components and include the appropriate html attributes required for accessibility.
It does not provide any styling.
Form Input Group
Use FormInputGroup to provide a standard layout for an input (e.g., Text Input, Text Area, or Dropdown), an optional piece of helper text, and an optional error message.
The preview has been updated.
Tight
skin is deprecated in SWAN v3.8
Both standard and tight produce the same spacing values, making the prop redundant.
Use skin="tight" on StandardForm to reduce vertical space between each FormInputGroup.
The preview has been updated.
Floating Label
Use InputWithFloatingLabel to create a label inside the form element that is positioned at the top (in case of Dropdown and Listbox) or is placed at the position of the placeholder (in case of Combobox, Search Input, Text Input, TextArea) which shifts to the top once the user starts entering text.
When using InputWithFloatingLabel, use FloatingLabel instead of FormLabel, which has the same accessibility benefits.
Note: the floating label has the same appearance as placeholder text but it is not an HTML placeholder; any actual placeholder attribute on the text area is transparent.
The preview has been updated.
You can refer to individual pages to see how floating labels can be used with each one of them.
Horizontal layout
variant prop is deprecated in SWAN v3.13
Omit to use the default layout instead, which better aligns with our visual design standards and promotes consistency for customers
- Use
variant="horizontal"onStandardFormto render the label next to the input rather than above it. - First, render a
GridContainerinside of theStandardForm, and render a separateRowfor each field. - In the React API, take advantage of the component property on
Row. Normally,Rowjust renders a div. Instead, use the component prop to tellRowto render aFormFieldrather than a plaindiv. The result is a component that is both aRowand aFormField. - Next, inside each
Row, render one column element for the label, and another for theFormInputGroup. Pick column widths as appropriate for the content, but do not set explicit column values on extra-small screens. Doing so lets the form assume a vertical arrangement on extra-small screens instead.
The preview has been updated.
Grouping fields
Use Fieldset to group several related form fields together within a single form. Provide a caption that describes the grouping with FieldSetLegend.
If Fieldset can't be used, use a surrounding element with role="group" and an appropriate aria-labelledby or aria-label.
The preview has been updated.
Accessibility
Labels
- Any input field (i.e., Text Input, Search Input, Text Area, Combobox, Listbox or Dropdown) needs an accessible label; the React API
FormLabelcomponent inside aFormFieldwill provide this. Alternatively, you can use aFloatingLabelas shown in the examples above. If you are not usingFormLabelorFloatingLabelyou need to add a label, preferably a<label>tag, with aforattribute whose value is theidof the input. (If a<label>tag is not possible, the input either needs aaria-labelledbyattribute, or anaria-labelattribute with a localized value.)
Required and optional fields
- To indicate required fields:
- Add the attribute
aria-required="true"to the input, to signal to assistive technologies that the field is required. - Add an asterisk "*" inside the
FormLabel.
- Add the attribute
- To indicate optional fields:
- Use
FormLabelOptionalto wrap the "(optional)" text inside theFormLabel.
- Use
The preview has been updated.
Inputs
- If the input is collecting the user's personal data such as mailing address or credit card, it must have an appropriate value for the autocomplete property, based on the list of 53 Input Purposes for User Interface Components. (This allows the browser to autofill these fields with saved data.)
- For inputs with buttons beside them or inset inside them, if the button is simply an icon and has no visible text, it will need to provide assistive devices with information on what the button does:
- If the button has nearby text that explains what it does, the button should have an
aria-labelledbyattribute whose value is theidof the element that describes it. - Otherwise, the button should have an inner
spanthat is visually hidden, and whose contents are localized text explaining what the button does (e.g. "Search").
- If the button has nearby text that explains what it does, the button should have an
Errors and helper text
The preview has been updated.
- When an input field has helper text and/or an error message, that text needs to be associated with the input; the React API
FormFieldcomponent along withFormHelperand/orFormErrorwill provide this. If you are not usingFormField: the input should have anaria-describedbyattribute whose value is a space-separated list of theids of the message(s). - When an input field has an error, the input should have
aria-describedbyandaria-errormessageattributes whose value is theidof the error message; the React APIFormFieldandFormErrorwill provide this. The error message should not be alabeltag with aforattribute pointing to the input. For the React API,FormErrorthe error icon is automatically added. However, in the Vanilla API, users must manually include an error icon alongside the error message. - When a form input field has a visible error message, the input should have
aria-invalid="true"to indicate that its current value is invalid. It should also have both anaria-describedbyattribute and anaria-errormessageattribute, both of whose values are theidof the error message. - If you are dynamically adding an error message after initial page load, then the content area where the error will appear will need to already have
role="status"aria-live="polite"on it. These attributes inform assistive technologies that they need to notify the user when any content inside that area changes, and thus the browser will announce the error message when it appears.