Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): pre-selected option correctly floats label #2125

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/mdc-select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ style dependencies for both the mdc-list and mdc-menu for this component to func

#### Select with pre-selected option

When dealing with the select component that has pre-selected values, you'll want to ensure that you
render `mdc-select__label` with the `mdc-select__label--float-above` modifier class and the selected
option with `aria-selected`. This will ensure that the label moves out of the way of the select's value
and prevents a Flash Of Un-styled Content (**FOUC**).

```html
<div class="mdc-select" role="listbox">
<div class="mdc-select__surface" tabindex="0">
<div class="mdc-select__label">Pick a Food Group</div>
<div class="mdc-select__label mdc-select__label--float-above">Pick a Food Group</div>
<div class="mdc-select__selected-text"></div>
<div class="mdc-select__bottom-line"></div>
</div>
Expand Down
8 changes: 3 additions & 5 deletions packages/mdc-select/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ export default class MDCSelectFoundation extends MDCFoundation {
};
this.cancelHandler_ = () => {
this.close_();

if (this.selectedIndex_ === -1) {
this.adapter_.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE);
}
};
}

Expand Down Expand Up @@ -157,6 +153,9 @@ export default class MDCSelectFoundation extends MDCFoundation {
if (this.selectedIndex_ >= 0) {
selectedTextContent = this.adapter_.getTextForOptionAtIndex(this.selectedIndex_).trim();
this.adapter_.setAttrForOptionAtIndex(this.selectedIndex_, 'aria-selected', 'true');
this.adapter_.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE);
} else {
this.adapter_.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE);
}
this.adapter_.setSelectedTextContent(selectedTextContent);
}
Expand Down Expand Up @@ -214,7 +213,6 @@ export default class MDCSelectFoundation extends MDCFoundation {
const focusIndex = this.selectedIndex_ < 0 ? 0 : this.selectedIndex_;

this.setMenuStylesForOpenAtIndex_(focusIndex);
this.adapter_.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE);
this.adapter_.addClassToBottomLine(cssClasses.BOTTOM_LINE_ACTIVE);
this.adapter_.addClass(OPEN);
this.animationRequestId_ = requestAnimationFrame(() => {
Expand Down