Skip to content

Commit 57025be

Browse files
committed
fix(material-experimental/mdc-select): float label on focus if there's a placeholder
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`.
1 parent 4316787 commit 57025be

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

scripts/check-mdc-tests-config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ export const config = {
140140
'element is inside an ngIf'
141141
],
142142
'mdc-select': [
143-
// TODO(crisbeto): remove this exception once #22187 lands.
144-
'should float the label on focus if it has a placeholder',
145-
146143
// These tests are excluded, because they're verifying the functionality that positions
147144
// the select panel over the trigger which isn't supported in the MDC select.
148145
'should set the width of the overlay based on a larger trigger width',

src/material-experimental/mdc-select/select.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,22 @@ describe('MDC-based MatSelect', () => {
23642364
expect(label.classList.contains('mdc-floating-label--float-above'))
23652365
.toBe(true, 'Label should be floating');
23662366
}));
2367+
2368+
it('should float the label on focus if it has a placeholder', fakeAsync(() => {
2369+
const fixture = TestBed.createComponent(FloatLabelSelect);
2370+
fixture.detectChanges();
2371+
expect(fixture.componentInstance.placeholder).toBeTruthy();
2372+
2373+
fixture.componentInstance.floatLabel = 'auto';
2374+
fixture.detectChanges();
2375+
2376+
dispatchFakeEvent(fixture.nativeElement.querySelector('.mat-mdc-select'), 'focus');
2377+
fixture.detectChanges();
2378+
2379+
const label = fixture.nativeElement.querySelector('.mat-mdc-form-field label');
2380+
expect(label.classList.contains('mdc-floating-label--float-above'))
2381+
.toBe(true, 'Label should be floating');
2382+
}));
23672383
});
23682384

23692385
describe('with a sibling component that throws an error', () => {
@@ -4123,7 +4139,7 @@ class BasicSelectOnPushPreselected {
41234139
template: `
41244140
<mat-form-field [floatLabel]="floatLabel">
41254141
<mat-label>Select a food</mat-label>
4126-
<mat-select placeholder="Food I want to eat right now" [formControl]="control">
4142+
<mat-select [placeholder]="placeholder" [formControl]="control">
41274143
<mat-option *ngFor="let food of foods" [value]="food.value">
41284144
{{ food.viewValue }}
41294145
</mat-option>
@@ -4134,6 +4150,7 @@ class BasicSelectOnPushPreselected {
41344150
class FloatLabelSelect {
41354151
floatLabel: FloatLabelType | null = 'auto';
41364152
control = new FormControl();
4153+
placeholder = 'Food I want to eat right now';
41374154
foods: any[] = [
41384155
{ value: 'steak-0', viewValue: 'Steak' },
41394156
{ value: 'pizza-1', viewValue: 'Pizza' },
@@ -4238,7 +4255,7 @@ class BasicSelectWithTheming {
42384255
template: `
42394256
<mat-form-field>
42404257
<mat-label>Select a food</mat-label>
4241-
<mat-select placeholder="Food" [formControl]="control">
4258+
<mat-select [formControl]="control">
42424259
<mat-option *ngFor="let food of foods" [value]="food.value">
42434260
{{ food.viewValue }}
42444261
</mat-option>

src/material-experimental/mdc-select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit
116116
get shouldLabelFloat(): boolean {
117117
// Since the panel doesn't overlap the trigger, we
118118
// want the label to only float when there's a value.
119-
return this.panelOpen || !this.empty;
119+
return this.panelOpen || !this.empty || (this.focused && !!this.placeholder);
120120
}
121121

122122
ngOnInit() {

0 commit comments

Comments
 (0)