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(material-experimental/mdc-select): float label on focus if there's a placeholder #22187

Merged
merged 1 commit into from
Mar 23, 2021
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
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