Skip to content

Commit ba74a58

Browse files
committed
fix(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 4ef3d3f commit ba74a58

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
@@ -2332,7 +2332,7 @@ describe('MatSelect', () => {
23322332
.toBe(true, 'Label should be floating');
23332333
}));
23342334

2335-
it ('should default to global floating label type', fakeAsync(() => {
2335+
it('should default to global floating label type', fakeAsync(() => {
23362336
fixture.destroy();
23372337

23382338
TestBed.resetTestingModule();
@@ -2360,6 +2360,19 @@ describe('MatSelect', () => {
23602360
expect(formField.classList.contains('mat-form-field-should-float'))
23612361
.toBe(true, 'Label should be floating');
23622362
}));
2363+
2364+
it('should float the label on focus if it has a placeholder', fakeAsync(() => {
2365+
expect(fixture.componentInstance.placeholder).toBeTruthy();
2366+
2367+
fixture.componentInstance.floatLabel = 'auto';
2368+
fixture.detectChanges();
2369+
2370+
dispatchFakeEvent(fixture.nativeElement.querySelector('.mat-select'), 'focus');
2371+
fixture.detectChanges();
2372+
2373+
expect(formField.classList.contains('mat-form-field-should-float'))
2374+
.toBe(true, 'Label should be floating');
2375+
}));
23632376
});
23642377

23652378
describe('with a sibling component that throws an error', () => {
@@ -4870,7 +4883,7 @@ class BasicSelectOnPushPreselected {
48704883
selector: 'floating-label-select',
48714884
template: `
48724885
<mat-form-field [floatLabel]="floatLabel">
4873-
<mat-select placeholder="Food I want to eat right now" [formControl]="control">
4886+
<mat-select [placeholder]="placeholder" [formControl]="control">
48744887
<mat-option *ngFor="let food of foods" [value]="food.value">
48754888
{{ food.viewValue }}
48764889
</mat-option>
@@ -4880,6 +4893,7 @@ class BasicSelectOnPushPreselected {
48804893
})
48814894
class FloatLabelSelect {
48824895
floatLabel: FloatLabelType | null = 'auto';
4896+
placeholder = 'Food I want to eat right now';
48834897
control = new FormControl();
48844898
foods: any[] = [
48854899
{ value: 'steak-0', viewValue: 'Steak' },
@@ -4984,7 +4998,7 @@ class BasicSelectWithTheming {
49844998
selector: 'reset-values-select',
49854999
template: `
49865000
<mat-form-field>
4987-
<mat-select placeholder="Food" [formControl]="control">
5001+
<mat-select [formControl]="control">
49885002
<mat-option *ngFor="let food of foods" [value]="food.value">
49895003
{{ food.viewValue }}
49905004
</mat-option>

src/material/select/select.ts

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

10871087
static ngAcceptInputType_required: BooleanInput;

0 commit comments

Comments
 (0)