Skip to content

Commit

Permalink
fix(material-experimental/mdc-select): float label on focus if there'…
Browse files Browse the repository at this point in the history
…s a placeholder (#22187)

This is the equivalent of #19517 for the MDC-based select.

Historically we've only floated the `mat-select` label if a value is selected or the panel is
open, because we wouldn't otherwise have anything to show. These changes make it so
that we also float it on focus, if there's `placeholder` text that can be shown. This behavior
is consistent with `MatInput`.
  • Loading branch information
crisbeto committed Mar 23, 2021
1 parent 75e191d commit b994349
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 0 additions & 3 deletions scripts/check-mdc-tests-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ export const config = {
'element is inside an ngIf'
],
'mdc-select': [
// TODO(crisbeto): remove this exception once #22187 lands.
'should float the label on focus if it has a placeholder',

// These tests are excluded, because they're verifying the functionality that positions
// the select panel over the trigger which isn't supported in the MDC select.
'should set the width of the overlay based on a larger trigger width',
Expand Down
21 changes: 19 additions & 2 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,22 @@ describe('MDC-based MatSelect', () => {
expect(label.classList.contains('mdc-floating-label--float-above'))
.toBe(true, 'Label should be floating');
}));

it('should float the label on focus if it has a placeholder', fakeAsync(() => {
const fixture = TestBed.createComponent(FloatLabelSelect);
fixture.detectChanges();
expect(fixture.componentInstance.placeholder).toBeTruthy();

fixture.componentInstance.floatLabel = 'auto';
fixture.detectChanges();

dispatchFakeEvent(fixture.nativeElement.querySelector('.mat-mdc-select'), 'focus');
fixture.detectChanges();

const label = fixture.nativeElement.querySelector('.mat-mdc-form-field label');
expect(label.classList.contains('mdc-floating-label--float-above'))
.toBe(true, 'Label should be floating');
}));
});

describe('with a sibling component that throws an error', () => {
Expand Down Expand Up @@ -4123,7 +4139,7 @@ class BasicSelectOnPushPreselected {
template: `
<mat-form-field [floatLabel]="floatLabel">
<mat-label>Select a food</mat-label>
<mat-select placeholder="Food I want to eat right now" [formControl]="control">
<mat-select [placeholder]="placeholder" [formControl]="control">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{ food.viewValue }}
</mat-option>
Expand All @@ -4134,6 +4150,7 @@ class BasicSelectOnPushPreselected {
class FloatLabelSelect {
floatLabel: FloatLabelType | null = 'auto';
control = new FormControl();
placeholder = 'Food I want to eat right now';
foods: any[] = [
{ value: 'steak-0', viewValue: 'Steak' },
{ value: 'pizza-1', viewValue: 'Pizza' },
Expand Down Expand Up @@ -4238,7 +4255,7 @@ class BasicSelectWithTheming {
template: `
<mat-form-field>
<mat-label>Select a food</mat-label>
<mat-select placeholder="Food" [formControl]="control">
<mat-select [formControl]="control">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{ food.viewValue }}
</mat-option>
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit
get shouldLabelFloat(): boolean {
// Since the panel doesn't overlap the trigger, we
// want the label to only float when there's a value.
return this.panelOpen || !this.empty;
return this.panelOpen || !this.empty || (this.focused && !!this.placeholder);
}

ngOnInit() {
Expand Down

0 comments on commit b994349

Please sign in to comment.