Skip to content

Commit eaec56d

Browse files
committed
feat(select): add floatingPlaceholder option
Adds the `floatingPlaceholder` option that can be used to disable the floating placeholders. Fixes #2569.
1 parent 651440f commit eaec56d

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

src/demo-app/select/select-demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<md-card>
2020
<md-select placeholder="Drink" [(ngModel)]="currentDrink" [required]="isRequired" [disabled]="isDisabled"
21-
#drinkControl="ngModel">
21+
[floatingPlaceholder]="floatingPlaceholder" #drinkControl="ngModel">
2222
<md-option *ngFor="let drink of drinks" [value]="drink.value" [disabled]="drink.disabled">
2323
{{ drink.viewValue }}
2424
</md-option>
@@ -30,6 +30,7 @@
3030
<button md-button (click)="currentDrink='sprite-1'">SET VALUE</button>
3131
<button md-button (click)="isRequired=!isRequired">TOGGLE REQUIRED</button>
3232
<button md-button (click)="isDisabled=!isDisabled">TOGGLE DISABLED</button>
33+
<button md-button (click)="floatingPlaceholder=!floatingPlaceholder">TOGGLE FLOATING PLACEHOLDER</button>
3334
<button md-button (click)="drinkControl.reset()">RESET</button>
3435
</md-card>
3536

src/demo-app/select/select-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class SelectDemo {
1414
showSelect = false;
1515
currentDrink: string;
1616
latestChangeEvent: MdSelectChange;
17+
floatingPlaceholder = true;
1718
foodControl = new FormControl('pizza-1');
1819

1920
foods = [

src/lib/select/select.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<div class="md-select-trigger" cdk-overlay-origin (click)="toggle()" #origin="cdkOverlayOrigin" #trigger>
2-
<span class="md-select-placeholder" [class.md-floating-placeholder]="this.selected"
3-
[@transformPlaceholder]="_placeholderState" [style.width.px]="_selectedValueWidth"> {{ placeholder }} </span>
2+
<span
3+
class="md-select-placeholder"
4+
[class.md-floating-placeholder]="selected"
5+
[@transformPlaceholder]="floatingPlaceholder ? _placeholderState : ''"
6+
[style.visibility]="(floatingPlaceholder || !selected) ? 'visible' : 'hidden'"
7+
[style.width.px]="_selectedValueWidth"> {{ placeholder }} </span>
8+
49
<span class="md-select-value" *ngIf="selected"> {{ selected?.viewValue }} </span>
510
<span class="md-select-arrow"></span>
611
</div>

src/lib/select/select.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('MdSelect', () => {
2626
SelectInitWithoutOptions,
2727
SelectWithChangeEvent,
2828
CustomSelectAccessor,
29-
CompWithCustomSelect
29+
CompWithCustomSelect,
30+
FloatingPlaceholderSelect
3031
],
3132
providers: [
3233
{provide: OverlayContainer, useFactory: () => {
@@ -1221,6 +1222,20 @@ describe('MdSelect', () => {
12211222
});
12221223
}));
12231224

1225+
it('should be able to disable the floating placeholder', () => {
1226+
let fixture = TestBed.createComponent(FloatingPlaceholderSelect);
1227+
let placeholder = fixture.debugElement.query(By.css('.md-select-placeholder')).nativeElement;
1228+
1229+
fixture.detectChanges();
1230+
1231+
expect(placeholder.style.visibility).toBe('visible');
1232+
1233+
fixture.componentInstance.control.setValue('pizza-1');
1234+
fixture.detectChanges();
1235+
1236+
expect(placeholder.style.visibility).toBe('hidden');
1237+
});
1238+
12241239
});
12251240

12261241
describe('change event', () => {
@@ -1433,6 +1448,29 @@ class CompWithCustomSelect {
14331448
@ViewChild(CustomSelectAccessor) customAccessor: CustomSelectAccessor;
14341449
}
14351450

1451+
@Component({
1452+
selector: 'floating-placeholder-select',
1453+
template: `
1454+
<md-select placeholder="Food I want to eat right now" [formControl]="control"
1455+
[floatingPlaceholder]="floatingPlaceholder">
1456+
<md-option *ngFor="let food of foods" [value]="food.value">
1457+
{{ food.viewValue }}
1458+
</md-option>
1459+
</md-select>
1460+
`,
1461+
})
1462+
class FloatingPlaceholderSelect {
1463+
floatingPlaceholder: boolean = false;
1464+
foods: any[] = [
1465+
{ value: 'steak-0', viewValue: 'Steak' },
1466+
{ value: 'pizza-1', viewValue: 'Pizza' },
1467+
{ value: 'tacos-2', viewValue: 'Tacos'}
1468+
];
1469+
control = new FormControl();
1470+
1471+
@ViewChild(MdSelect) select: MdSelect;
1472+
}
1473+
14361474

14371475
/**
14381476
* TODO: Move this to core testing utility until Angular has event faking

src/lib/select/select.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
222222
get required() { return this._required; }
223223
set required(value: any) { this._required = coerceBooleanProperty(value); }
224224

225+
/** Whether to float the placeholder text. */
226+
@Input()
227+
get floatingPlaceholder(): boolean { return this._floatingPlaceholder; }
228+
set floatingPlaceholder(value) { this._floatingPlaceholder = coerceBooleanProperty(value); }
229+
private _floatingPlaceholder: boolean = true;
230+
225231
/** Event emitted when the select has been opened. */
226232
@Output() onOpen: EventEmitter<void> = new EventEmitter<void>();
227233

0 commit comments

Comments
 (0)