Back to Material Components Web

Select helper text

packages/mdc-select/helper-text/README.md

14.0.05.1 KB
Original Source
<!--docs: title: "Select helper text" layout: detail section: components excerpt: "The helper text provides supplemental information and/or validation messages to users" iconId: menu path: /catalog/input-controls/select-menus/helper-text/ -->

Select helper text

Helper text gives context about a select, such as how the selection will be used. It should be visible either persistently or only on invalid state.

Basic usage

HTML structure

html
<p class="mdc-select-helper-text" aria-hidden="true">

NOTE: Make sure there are no white-space characters before helper text content.

JavaScript instantiation

js
import {MDCSelectHelperText} from '@material/select/helper-text';
const helperText = new MDCSelectHelperText(document.querySelector('.mdc-select-helper-text'));

Accessibility

Note that in every example where the helper text is dependent on the state of the select element, we assign an id to the mdc-select-helper-text element and set that id to the value of the aria-controls and aria-describedby attributes on the element with the mdc-select__selected-text class. We recommend doing this as well as it will help indicate to assistive devices that the display of the helper text is dependent on the interaction with the MDCSelect component.

html
<div class="mdc-select mdc-select--filled">
  <div class="mdc-select__anchor"
       role="button"
       aria-haspopup="listbox"
       aria-labelledby="demo-label demo-selected-text"
       aria-controls="my-helper-text"
       aria-describedby="my-helper-text">
   <!-- rest of main component -->
</div>
<p id="my-helper-text" class="mdc-select-helper-text">Helper text</p>

When using our JS component, if the browser sees that the input element has an aria-controls attribute, it will look for an element with the id specified and treat it as the Select's helper text element, taking care of adding/removing aria-hidden and other accessibility attributes. Adding and removing classes and attributes to the helper text element can also be done using the MDCSelectHelperText API, which is described below.

API

CSS classes

CSS ClassDescription
mdc-select-helper-textMandatory. By default non-validation helper text is always visible.
mdc-select-helper-text--validation-msgIndicates the helper text is a validation message. By default validation message is hidden unless the select is invalid.
mdc-select-helper-text--validation-msg-persistentWhen the helper text is serving as a validation message, make it permanently visible regardless of the select's validity.

Sass mixins

MixinDescription
helper-text-color($color)Customizes the color of the helper text following a select.
disabled-helper-text-color($color)Customizes the color of the helper text following a select when disabled.
helper-text-validation-color($color)Customizes the color of the helper text validation message when the select is invalid.
hover-helper-text-validation-color($color)Customizes the color of the helper text validation message when the select is invalid and hovered.

MDCSelectHelperText properties and methods

PropertyValue TypeDescription
foundationMDCSelectHelperTextFoundationReturns the helper text's foundation. This allows the parent MDCSelect component to access the public methods on the MDCSelectHelperTextFoundation class.

Usage within frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Helper Text for your framework. Depending on your needs, you can use the Simple Approach: Wrapping MDC Web Components, or the Advanced Approach: Using Foundations and Adapters. Please follow the instructions here.

MDCSelectHelperTextAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the helper text element.
removeClass(className: string) => voidRemoves a class from the helper text element.
hasClass(className: string) => booleanReturns true if classname exists for the helper text element.
setAttr(attr: string, value: string) => voidSets an attribute with a given value on the helper text element.
removeAttr(attr: string) => voidRemoves an attribute on the helper text element.
setContent(attr: string) => voidSets the text content for the helper text element.

MDCSelectHelperTextFoundation

Method SignatureDescription
`getId() => stringnull`
isVisible() => booleanReturns whether the helper text is visible.
setContent(content: string) => voidSets the content of the helper text.
setValidation(isValidation: boolean) => voidSets the helper text as a validation message. By default, validation messages are hidden when the select is valid and visible when the select is invalid.
setValidationMsgPersistent(isPersistent: boolean) => voidThis keeps the validation message visible even if the select is valid, though it will be displayed in the normal (grey) color.
setValidity(inputIsValid: boolean) => voidSets the validity of the helper text based on the input validity.