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 structure

<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)

Form elements

Form

Form
Element <form>
Class usa-form
Variants usa-form--large
Modifiers

Variants

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.

Guidance

Accessibility guidance

  • 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.

Identifying required fields

  • 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.

Storybook

  • Forms
    • Form elements
      • Form
        • Default
          • Default state (See note)
        • Variants
          • Large form (See note)
        • Modifiers
        • Test cases
    • Form controls

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

Fieldset
Element <fieldset>
Class usa-fieldset
Variants
Modifiers

Guidance

  • Fieldsets are not required to be used in forms. However, if a fieldset is used, a single legend must be present.

Accessibility guidance

  • 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.

Storybook

  • Forms
    • Form elements
      • Fieldset
        • Default
          • Default state (See note)
          • Disabled state (See note)
        • Variants
        • Modifiers
        • Test cases
    • Form controls

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

Legend
Element <legend>
Class usa-legend
Variants usa-legend--large
Modifiers

Variants

The large legend sets the text to the xl size token.

Guidance

  • Fieldsets are not required to be used in forms. However, if a fieldset is used, a single legend must be present.

Accessibility guidance

  • 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.

Storybook

  • Forms
    • Form elements
      • Legend
        • Default
          • Default state
        • Variants
          • Large legend
        • Modifiers
        • Test cases
    • Form controls

Form group

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

Modifiers

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.

Guidance

  • All form controls must be wrapped in this component for to ensure correct default and error styling.

Storybook

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

Label
Element <label>
Class usa-label
Variants
Modifiers usa-label--error
usa-label--required

Modifiers

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).

Guidance

Accessibility guidance

  • 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.

Defining an implicit label

<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.

Defining an explicit label with the for attribute

<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>

Storybook

  • Forms
    • Form elements
      • Label
        • Default
          • Default state
        • Variants
        • Modifiers
          • Error state
          • Required state
        • Test cases
    • Form controls

Hint

Hint
Element
Class usa-hint
Variants
Modifiers usa-hint--required
  • Not an HTML element
  • USWDS on the hint component: undocumented component

Modifiers

The required hint sets the hint text to the error color token to call attention to a required field (for sighted users).

Guidance

  • Use hint text to highlight input restrictions or to show users the expected format.

Storybook

  • Forms
    • Form elements
      • Hint
        • Default
          • Default state
        • Variants
        • Modifiers
          • Required state
        • Test cases
    • Form controls

Error message

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

Modifiers

The inverse error message allows the error message to pass contrast for forms used on dark backgrounds.

Guidance

  • 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.

Storybook

  • Forms
    • Form elements
      • Error message
        • Default
          • Default state
        • Variants
        • Modifiers
          • Inverse (dark backgrounds)
        • Test cases
    • Form controls

[Show] Password

Password
Element
Class usa-show-password
Variants
Modifiers
Javascript ⚠️ Enabled
  • 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.

Storybook

  • Forms
    • Form elements
      • Password
        • Default
          • Default state
          • Toggled state
        • Variants
        • Modifiers
        • Test cases
    • Form controls

Form buttons

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.

Guidance

  • 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, and reset. If no type attribute is defined, the button will behave as a submit button.
  • Use the disabled or aria-disabled attributes to turn on disabled button styles.
  • Avoid reset. This behavior tends to annoy users.

Submit

<input class="usa-button" type="submit" value="Submit" />
<button class="usa-button" type="submit">Submit</button>

Reset

<input class="usa-button" type="reset" value="Reset" />
<button class="usa-button" type="reset">Reset</button>

Form note

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.

Form components

Validation

Validation
Element
Class usa-alert--validation
Variants
Modifiers

This appears to be a variant of usa-alert.

Storybook

  • Validation
    • Default
    • Test Cases
      • Input validation
      • Textarea validation

Form controls

Input

Storybook

  • 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

Textarea

Combo box

Input group (prefix/suffix)

Storybook

  • 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

..

Form templates

Each form template has important usability and accessibility guidance from USWD:

Form patterns

All form patterns have additional guidance from USWDS:

⚠️ **GitHub.com Fallback** ⚠️