Forms inventory - NCIOCPL/ncids GitHub Wiki
Important
All guidance has been yoinked straight from USWDS and MDN or inferred from the two sources. Items up for discussion will be placed in notes.
<form class="usa-form">
<div class="usa-fieldset">
<div class="usa-legend usa-legend--large">
A simple form
</div>
<div class="usa-form-group">
<label class="usa-label" for="input-type-text">Text input label</label>
<input class="usa-input" name="input-type-text" />
</div>
<div class="usa-form-group">
<label class="usa-label" for="input-type-textarea">Text area label</label>
<textarea class="usa-textarea" name="input-type-textarea"></textarea>
</div>
</div>
<button class="usa-button" type="submit">Submit</button>
</form>
- Form
- Fieldset (optional)
- Legend (required if fieldset used)
- Form group (required)
- Label (required)
- Form control (required)
- Submit button (required)
- Fieldset (optional)
Form | |
---|---|
Element | <form> |
Class | usa-form |
Variants | usa-form--large |
Modifiers |
- MDN the form element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
- USWDS on the form component: https://designsystem.digital.gov/components/form/
The purpose of the large form is to constrain the form and its contents. Default sets the max-width of units("mobile")
while large increases the max-width to units("mobile-lg")
on the mobile-lg
breakpoint. See spacing documentation for details on those units.
- Don’t control element order with CSS. Display form controls in the same order in the HTML as they appear on screen. Don’t use CSS to rearrange the form controls. Screen readers narrate form elements in the order they appear in the HTML.
- Align validation with form controls. Visually align validation messages with the form controls so people using screen magnifiers can read them quickly.
- Use simple vertical layouts. Keep your form blocks in a vertical pattern. This approach is ideal, from an accessibility standpoint, because of limited vision that makes it hard to scan from right to left.
-
Avoid placeholder text. Avoid using placeholder text that appears within a text field before a user starts typing. If placeholder text is no longer visible after a user clicks into the field, users will no longer have that text available when they need to review their entries. People who have cognitive or visual disabilities have additional problems with placeholder text. Most browsers’ default rendering of placeholder text does not provide a high enough contrast ratio.
- Use hint or label instead.
- Show whether a field is required or optional. Mark required fields as required by using a red asterisk (). Use text descriptions. Combine red asterisks () with a text description at the top of the form instructing the user of its meaning.
- Example: “A red asterisk (*) indicates a required field.”
- Use the required attribute. Add the required attribute to your form elements to indicate that a field must be filled out before submitting the form.
- Mark optional fields as "optional." Label optional fields with the word “optional” placed in parentheses. Example: (optional)
- Let users know if there is an error. Use a message or alert to notify the user if a field that is required was not completed, and indicate which field was not completed. Additionally, include an error notification in the page title (title element) for screen readers.
- Note: One-field forms are the exception and do not need to be marked with a required label or symbol.
- Forms
- Form elements
- Form
- Default
- Default state (See note)
- Variants
- Large form (See note)
- Modifiers
- Test cases
- Default
- Form
- Form controls
- Form elements
Note
Form and large form do not have stories to copy from USWDS's storybook. We may consider testing the form and its large modifier with form pattern stories.
Fieldset | |
---|---|
Element | <fieldset> |
Class | usa-fieldset |
Variants | |
Modifiers |
- MDN the fieldset element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset
- USWDS on the fieldset component: undocumented component
- Fieldsets are not required to be used in forms. However, if a fieldset is used, a single legend must be present.
- Use proper markup. Group each set of thematically related controls in a fieldset element. Use the legend element to offer a label within each one. The fieldset and legend elements make it easier for those who use screen readers to navigate the form.
- Use legends. Use a single legend for fieldset (this is required). One common use of the fieldset and legend elements is a question with radio-button options for answers. The question text and radio buttons are wrapped in a fieldset, with the question itself being inside the legend tag.
- Embed multiple fieldsets and legends for more complex forms.
- Forms
- Form elements
- Fieldset
- Default
- Default state (See note)
- Disabled state (See note)
- Variants
- Modifiers
- Test cases
- Default
- Fieldset
- Form controls
- Form elements
Note
Fieldset does not have stories to copy from USWDS's storybook. We may consider testing fieldset with form pattern stories. This component imports font styles and overrides browser fieldset settings. See MDN linked above for default browser styles. The disabled state forces all descendant form controls of the fieldset to be disabled. The disabled states of these form controls will be shown in the appropriate story of that form control and this may be not be needed to test additionally with this component.
Legend | |
---|---|
Element | <legend> |
Class | usa-legend |
Variants | usa-legend--large |
Modifiers |
- MDN the legend element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/legend
- USWDS on the legend component: undocumented component
The large legend sets the text to the xl
size token.
- Fieldsets are not required to be used in forms. However, if a fieldset is used, a single legend must be present.
- Use proper markup. Group each set of thematically related controls in a fieldset element. Use the legend element to offer a label within each one. The fieldset and legend elements make it easier for those who use screen readers to navigate the form.
- Use legends. Use a single legend for fieldset (this is required). One common use of the fieldset and legend elements is a question with radio-button options for answers. The question text and radio buttons are wrapped in a fieldset, with the question itself being inside the legend tag.
- Embed multiple fieldsets and legends for more complex forms.
- Forms
- Form elements
- Legend
- Default
- Default state
- Variants
- Large legend
- Modifiers
- Test cases
- Default
- Legend
- Form controls
- Form elements
Form group | |
---|---|
Element | |
Class | usa-form-grop |
Variants | |
Modifiers | usa-form-group--error |
- Not an HTML element
- USWDS on the from group component: undocumented component
The error form group adds a red border plus padding to the left of the form group to call attention to an input with errors.
- All form controls must be wrapped in this component for to ensure correct default and error styling.
Note
Form groups are used to wrap form controls for default styling and error styling. It doesn't make sense to show this on its own, and since we are requiring use of the form group component, can be shown in form control states and form templates.
Label | |
---|---|
Element | <label> |
Class | usa-label |
Variants | |
Modifiers |
usa-label--error usa-label--required
|
- MDN the label element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
- USWDS on the label component: undocumented component
The error label bolds the label text to call attention to an input with errors.
The required label sets the label text to the error color token to call attention to a required field (for sighted users).
- Do not place interactive elements such as anchors or buttons inside a label. Doing so makes it difficult for people to activate the form input associated with the label.
-
Do not place heading elements Placing heading elements within a label. Doing so interferes with many kinds of assistive technology, because headings are commonly used as a navigation aid.
- Use
nci-heading
classes instead if needed for styling.
- Use
<div class="usa-form-group">
<label class="usa-label">
<input class="usa-input" />
</label>
</div>
Note
Implicit labels are infrequently used by USWDS. Although allowed, we may consider suggesting against it for consistency.
<div class="usa-form-group">
<label class="usa-label" for="input-type-text">Text input label</label>
<input class="usa-input" name="input-type-text" />
</div>
- Forms
- Form elements
- Label
- Default
- Default state
- Variants
- Modifiers
- Error state
- Required state
- Test cases
- Default
- Label
- Form controls
- Form elements
Hint | |
---|---|
Element | |
Class | usa-hint |
Variants | |
Modifiers | usa-hint--required |
- Not an HTML element
- USWDS on the hint component: undocumented component
The required hint sets the hint text to the error color token to call attention to a required field (for sighted users).
- Use hint text to highlight input restrictions or to show users the expected format.
- Forms
- Form elements
- Hint
- Default
- Default state
- Variants
- Modifiers
- Required state
- Test cases
- Default
- Hint
- Form controls
- Form elements
Error message | |
---|---|
Element | |
Class | usa-error-message |
Variants | |
Modifiers | usa-error-message--nci-inverse |
- Not an HTML element
- USWDS on the error message component: undocumented component
The inverse error message allows the error message to pass contrast for forms used on dark backgrounds.
- Let users know if there is an error. Use a message or alert to notify the user if a field that is required was not completed, and indicate which field was not completed. Additionally, include an error notification in the page title (title element) for screen readers.
- Long forms. On long forms, always include in-line validation in addition to any error messages that appear at the top of the form.
- Align validation with inputs. Visually align validation messages with the input fields so people using screen magnifiers can read them quickly.
- Forms
- Form elements
- Error message
- Default
- Default state
- Variants
- Modifiers
- Inverse (dark backgrounds)
- Test cases
- Default
- Error message
- Form controls
- Form elements
Password | |
---|---|
Element | |
Class | usa-show-password |
Variants | |
Modifiers | |
Javascript |
- Not an HTML element
- USWDS on the password component: undocumented component
Note
This is found in _usa-password
but also in usa-form
This component uses javascript to be enabled.
This component must import the form component to receive styles.
- Forms
- Form elements
- Password
- Default
- Default state
- Toggled state
- Variants
- Modifiers
- Test cases
- Default
- Password
- Form controls
- Form elements
Form buttons are not components, but are an important part of forms.
They may be used as input or button elements as long as the attribute type="submit"
is correctly applied.
- MDN on the button element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
- USWDS on the button component: https://designsystem.digital.gov/components/button/
-
Always set the type attribute. Define the purpose and behavior of a button with the type attribute. The type attribute can accept three values:
submit
,button
, andreset
. If no type attribute is defined, the button will behave as a submit button. - Use the
disabled
oraria-disabled
attributes to turn on disabled button styles. -
Avoid
reset
. This behavior tends to annoy users.
<input class="usa-button" type="submit" value="Submit" />
<button class="usa-button" type="submit">Submit</button>
- MDN on submit input: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit
- USWDS on submit button: undocumented
<input class="usa-button" type="reset" value="Reset" />
<button class="usa-button" type="reset">Reset</button>
- MDN on reset input: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/reset
- USWDS on reset button: undocumented
Note | |
---|---|
Element | |
Class | usa-form__note |
Variants | |
Modifiers |
Note
We should consider removing this from our patch. It copies some of the styles from the password component, but none of the functionality. And is unused by USWDS.
Validation | |
---|---|
Element | |
Class | usa-alert--validation |
Variants | |
Modifiers |
- USWDS component: https://designsystem.digital.gov/components/validation/
This appears to be a variant of usa-alert.
- Validation
- Default
- Test Cases
- Input validation
- Textarea validation
- Forms
- Form elements
- Form controls
- Input
- Default
- Default state
- Disabled state
- Variants
- 2xs
- xs
- sm, small
- md, medium
- lg
- xl
- 2xl
- Modifiers
- Error state
- Success state
- Test cases
- Default
- Input
- Forms
- Form elements
- Form controls
- Input group (prefix/suffix)
- Default
- Default state
- Disabled state
- Variants
- Modifiers
- 2xs
- xs
- sm, small
- md, medium
- lg
- xl
- 2xl
- Error state
- Success state
- Test cases
- Default
- Input group (prefix/suffix)
Each form template has important usability and accessibility guidance from USWD:
- Address form - https://designsystem.digital.gov/templates/form-templates/address-form/
- Name form - https://designsystem.digital.gov/templates/form-templates/name-form/
- Password reset form - https://designsystem.digital.gov/templates/form-templates/password-reset-form/
- Sign-in form - https://designsystem.digital.gov/templates/form-templates/sign-in-form/
All form patterns have additional guidance from USWDS:
- Create a user profile - https://designsystem.digital.gov/patterns/create-a-user-profile/
- Address
- Contact preferences
- Date of birth
- Email address
- Gender identity and sex
- Name
- Phone number
- Pronouns
- Social Security Number
- Complex form - https://designsystem.digital.gov/patterns/complete-a-complex-form/