Skip to content

Commit

Permalink
fix(material-experimental/mdc-select): float label on focus if there'…
Browse files Browse the repository at this point in the history
…s a placeholder

This is the equivalent of angular#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`.
  • Loading branch information
crisbeto committed Mar 11, 2021
1 parent 97f1ccc commit 84250b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/material-experimental/mdc-chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {MatChipTextControl} from './chip-text-control';

/** Represents an input event on a `matChipInput`. */
export interface MatChipInputEvent {
/**
/**
* The native `<input>` element that the event is being fired for.
* @deprecated Use `MatChipInputEvent#chipInput.inputElement` instead.
* @breaking-change 13.0.0 This property will be removed.
Expand All @@ -34,9 +34,9 @@ export interface MatChipInputEvent {

/** The value of the input. */
value: string;
/**
* Reference to the chip input that emitted the event.

/**
* Reference to the chip input that emitted the event.
* @breaking-change 13.0.0 This property will be made required.
*/
chipInput?: MatChipInput;
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 @@ -2340,6 +2340,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 @@ -4098,7 +4114,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 @@ -4109,6 +4125,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 @@ -4213,7 +4230,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.empty;
return !this.empty || (this.focused && !!this.placeholder);
}

ngOnInit() {
Expand Down
8 changes: 4 additions & 4 deletions src/material/chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {MatChipTextControl} from './chip-text-control';

/** Represents an input event on a `matChipInput`. */
export interface MatChipInputEvent {
/**
/**
* The native `<input>` element that the event is being fired for.
* @deprecated Use `MatChipInputEvent#chipInput.inputElement` instead.
* @breaking-change 13.0.0 This property will be removed.
Expand All @@ -34,9 +34,9 @@ export interface MatChipInputEvent {

/** The value of the input. */
value: string;
/**
* Reference to the chip input that emitted the event.

/**
* Reference to the chip input that emitted the event.
* @breaking-change 13.0.0 This property will be made required.
*/
chipInput?: MatChipInput;
Expand Down

0 comments on commit 84250b0

Please sign in to comment.