Skip to content

Commit 6c9e594

Browse files
committed
fix(material/select): float label on focus if there's a placeholder
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`. Fixes #19514.
1 parent 7d99c35 commit 6c9e594

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/material/select/select.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ describe('MatSelect', () => {
23742374
.toBe(true, 'Label should be floating');
23752375
}));
23762376

2377-
it ('should default to global floating label type', fakeAsync(() => {
2377+
it('should default to global floating label type', fakeAsync(() => {
23782378
fixture.destroy();
23792379

23802380
TestBed.resetTestingModule();
@@ -2405,6 +2405,19 @@ describe('MatSelect', () => {
24052405
expect(formField.classList.contains('mat-form-field-should-float'))
24062406
.toBe(true, 'Label should be floating');
24072407
}));
2408+
2409+
it('should float the label on focus if it has a placeholder', fakeAsync(() => {
2410+
expect(fixture.componentInstance.placeholder).toBeTruthy();
2411+
2412+
fixture.componentInstance.floatLabel = 'auto';
2413+
fixture.detectChanges();
2414+
2415+
dispatchFakeEvent(fixture.nativeElement.querySelector('.mat-select'), 'focus');
2416+
fixture.detectChanges();
2417+
2418+
expect(formField.classList.contains('mat-form-field-should-float'))
2419+
.toBe(true, 'Label should be floating');
2420+
}));
24082421
});
24092422

24102423
describe('with a sibling component that throws an error', () => {
@@ -4920,7 +4933,7 @@ class BasicSelectOnPushPreselected {
49204933
selector: 'floating-label-select',
49214934
template: `
49224935
<mat-form-field [floatLabel]="floatLabel">
4923-
<mat-select placeholder="Food I want to eat right now" [formControl]="control">
4936+
<mat-select [placeholder]="placeholder" [formControl]="control">
49244937
<mat-option *ngFor="let food of foods" [value]="food.value">
49254938
{{ food.viewValue }}
49264939
</mat-option>
@@ -4930,6 +4943,7 @@ class BasicSelectOnPushPreselected {
49304943
})
49314944
class FloatLabelSelect {
49324945
floatLabel: FloatLabelType | null = 'auto';
4946+
placeholder = 'Food I want to eat right now';
49334947
control = new FormControl();
49344948
foods: any[] = [
49354949
{ value: 'steak-0', viewValue: 'Steak' },
@@ -5034,7 +5048,7 @@ class BasicSelectWithTheming {
50345048
selector: 'reset-values-select',
50355049
template: `
50365050
<mat-form-field>
5037-
<mat-select placeholder="Food" [formControl]="control">
5051+
<mat-select [formControl]="control">
50385052
<mat-option *ngFor="let food of foods" [value]="food.value">
50395053
{{ food.viewValue }}
50405054
</mat-option>

src/material/select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A
10771077
* @docs-private
10781078
*/
10791079
get shouldLabelFloat(): boolean {
1080-
return this._panelOpen || !this.empty;
1080+
return this._panelOpen || !this.empty || (this._focused && !!this._placeholder);
10811081
}
10821082

10831083
static ngAcceptInputType_required: BooleanInput;

0 commit comments

Comments
 (0)