Skip to content

Commit e1daa01

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 d4ab3d3 commit e1daa01

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class SelectDemo {
1212
isDisabled = false;
1313
showSelect = false;
1414
currentDrink: string;
15+
floatingPlaceholder = true;
1516
foodControl = new FormControl('pizza-1');
1617

1718
foods = [

src/lib/select/select.html

+7-2
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

+44-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ describe('MdSelect', () => {
1616
beforeEach(async(() => {
1717
TestBed.configureTestingModule({
1818
imports: [MdSelectModule.forRoot(), ReactiveFormsModule, FormsModule],
19-
declarations: [BasicSelect, NgModelSelect, ManySelects, NgIfSelect],
19+
declarations: [
20+
BasicSelect,
21+
NgModelSelect,
22+
ManySelects,
23+
NgIfSelect,
24+
FloatingPlaceholderSelect
25+
],
2026
providers: [
2127
{provide: OverlayContainer, useFactory: () => {
2228
overlayContainerElement = document.createElement('div') as HTMLElement;
@@ -1139,6 +1145,20 @@ describe('MdSelect', () => {
11391145
});
11401146
}));
11411147

1148+
it('should be able to disable the floating placeholder', () => {
1149+
let fixture = TestBed.createComponent(FloatingPlaceholderSelect);
1150+
let placeholder = fixture.debugElement.query(By.css('.md-select-placeholder')).nativeElement;
1151+
1152+
fixture.detectChanges();
1153+
1154+
expect(placeholder.style.visibility).toBe('visible');
1155+
1156+
fixture.componentInstance.control.setValue('pizza-1');
1157+
fixture.detectChanges();
1158+
1159+
expect(placeholder.style.visibility).toBe('hidden');
1160+
});
1161+
11421162
});
11431163

11441164
});
@@ -1235,6 +1255,29 @@ class NgIfSelect {
12351255
@ViewChild(MdSelect) select: MdSelect;
12361256
}
12371257

1258+
@Component({
1259+
selector: 'floating-placeholder-select',
1260+
template: `
1261+
<md-select placeholder="Food I want to eat right now" [formControl]="control"
1262+
[floatingPlaceholder]="floatingPlaceholder">
1263+
<md-option *ngFor="let food of foods" [value]="food.value">
1264+
{{ food.viewValue }}
1265+
</md-option>
1266+
</md-select>
1267+
`,
1268+
1269+
})
1270+
class FloatingPlaceholderSelect {
1271+
floatingPlaceholder: boolean = false;
1272+
foods: any[] = [
1273+
{ value: 'steak-0', viewValue: 'Steak' },
1274+
{ value: 'pizza-1', viewValue: 'Pizza' },
1275+
{ value: 'tacos-2', viewValue: 'Tacos'}
1276+
];
1277+
control = new FormControl();
1278+
1279+
@ViewChild(MdSelect) select: MdSelect;
1280+
}
12381281

12391282

12401283
/**

src/lib/select/select.ts

+6
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
213213
get required() { return this._required; }
214214
set required(value: any) { this._required = coerceBooleanProperty(value); }
215215

216+
/** Whether to float the placeholder text. */
217+
@Input()
218+
get floatingPlaceholder(): boolean { return this._floatingPlaceholder; }
219+
set floatingPlaceholder(value) { this._floatingPlaceholder = coerceBooleanProperty(value); }
220+
private _floatingPlaceholder: boolean = true;
221+
216222
/** Event emitted when the select has been opened. */
217223
@Output() onOpen = new EventEmitter();
218224

0 commit comments

Comments
 (0)