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/select): float label on focus if there's a placeholder #19517

Merged
merged 1 commit into from
Mar 10, 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
20 changes: 17 additions & 3 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ describe('MatSelect', () => {
.toBe(true, 'Label should be floating');
}));

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

TestBed.resetTestingModule();
Expand Down Expand Up @@ -2405,6 +2405,19 @@ describe('MatSelect', () => {
expect(formField.classList.contains('mat-form-field-should-float'))
.toBe(true, 'Label should be floating');
}));

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

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

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

expect(formField.classList.contains('mat-form-field-should-float'))
.toBe(true, 'Label should be floating');
}));
});

describe('with a sibling component that throws an error', () => {
Expand Down Expand Up @@ -4920,7 +4933,7 @@ class BasicSelectOnPushPreselected {
selector: 'floating-label-select',
template: `
<mat-form-field [floatLabel]="floatLabel">
<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 @@ -4930,6 +4943,7 @@ class BasicSelectOnPushPreselected {
})
class FloatLabelSelect {
floatLabel: FloatLabelType | null = 'auto';
placeholder = 'Food I want to eat right now';
control = new FormControl();
foods: any[] = [
{ value: 'steak-0', viewValue: 'Steak' },
Expand Down Expand Up @@ -5034,7 +5048,7 @@ class BasicSelectWithTheming {
selector: 'reset-values-select',
template: `
<mat-form-field>
<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/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A
* @docs-private
*/
get shouldLabelFloat(): boolean {
return this._panelOpen || !this.empty;
return this._panelOpen || !this.empty || (this._focused && !!this._placeholder);
}

static ngAcceptInputType_required: BooleanInput;
Expand Down