Skip to content

Commit 9694507

Browse files
committed
Change API to accept string values
1 parent 7322c84 commit 9694507

File tree

7 files changed

+45
-27
lines changed

7 files changed

+45
-27
lines changed

src/demo-app/input/input-container-demo.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,16 @@ <h4>Textarea</h4>
180180
</md-input-container>
181181
</p>
182182
<p>
183-
<md-checkbox [(ngModel)]="floatingLabel">Toggle Floating Label</md-checkbox>
184-
<button md-button (click)="floatingLabel = null">Reset Floating Label</button>
183+
<md-button-toggle-group [(ngModel)]="floatingLabel">
184+
<md-button-toggle value="auto">Auto Float</md-button-toggle>
185+
<md-button-toggle value="always">Always Float</md-button-toggle>
186+
<md-button-toggle value="never">Never Float</md-button-toggle>
187+
</md-button-toggle-group>
188+
</p>
189+
190+
<p>
185191
<md-input-container [floatingPlaceholder]="floatingLabel">
186-
<input mdInput [placeholder]="floatingLabel ? 'Floating label' : 'Not floating label'">
192+
<input mdInput placeholder="Placeholder">
187193
</md-input-container>
188194
</p>
189195

src/demo-app/input/input-container-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ let max = 5;
1111
styleUrls: ['input-container-demo.css'],
1212
})
1313
export class InputContainerDemo {
14+
floatingLabel: string = 'auto';
1415
dividerColor: boolean;
1516
requiredField: boolean;
16-
floatingLabel: boolean;
1717
ctrlDisabled = false;
1818

1919
name: string;

src/lib/input/OVERVIEW.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ be used with `md-input-container`:
3838
A placeholder is an indicative text displayed in the input zone when the input does not contain
3939
text. When text is present, the indicative text will float above this input zone.
4040

41-
The `floatingPlaceholder` attribute of `md-input-container` can be set to `false` to hide the
41+
The `floatingPlaceholder` attribute of `md-input-container` can be set to `never` to hide the
4242
indicative text instead when text is present in the input.
4343

44+
When setting `floatingPlaceholder` to `always` the floating label will always show above the input.
45+
4446
A placeholder for the input can be specified in one of two ways: either using the `placeholder`
4547
attribute on the `input` or `textarea`, or using an `md-placeholder` element in the
4648
`md-input-container`. Using both will raise an error.

src/lib/input/input-container-errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ export class MdInputContainerMissingMdInputError extends MdError {
2828
'to the native input or textarea element?');
2929
}
3030
}
31+
32+
export class MdInputContainerFloatingPlaceholderInvalidError extends MdError {
33+
constructor(value: string) {
34+
super(`The value "${value}" for the floatingPlaceholder input is not valid.`);
35+
}
36+
}

src/lib/input/input-container.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[attr.for]="_mdInputChild.id"
1010
[class.md-empty]="_mdInputChild.empty && !_shouldAlwaysFloat"
1111
[class.md-focused]="_mdInputChild.focused"
12-
[class.md-float]="floatingPlaceholder"
12+
[class.md-float]="_canPlaceholderFloat"
1313
[class.md-accent]="dividerColor == 'accent'"
1414
[class.md-warn]="dividerColor == 'warn'"
1515
*ngIf="_hasPlaceholder()">

src/lib/input/input-container.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ describe('MdInputContainer', function () {
6060

6161
let inputContainer = fixture.debugElement.query(By.directive(MdInputContainer))
6262
.componentInstance as MdInputContainer;
63-
expect(inputContainer.floatingPlaceholder).toBe(true,
64-
'Expected MdInputContainer to default to having floating placeholders turned on');
63+
expect(inputContainer.floatingPlaceholder).toBe('auto',
64+
'Expected MdInputContainer to set floatingLabel to auto by default.');
6565
});
6666

6767
it('should not be treated as empty if type is date',
@@ -410,20 +410,20 @@ describe('MdInputContainer', function () {
410410
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
411411
fixture.detectChanges();
412412

413-
let inputEl = fixture.debugElement.query(By.css('input'));
413+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
414414
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
415415

416416
expect(labelEl.classList).not.toContain('md-empty');
417417
expect(labelEl.classList).toContain('md-float');
418418

419-
fixture.componentInstance.shouldFloat = null;
419+
fixture.componentInstance.shouldFloat = 'auto';
420420
fixture.detectChanges();
421421

422422
expect(labelEl.classList).toContain('md-empty');
423423
expect(labelEl.classList).toContain('md-float');
424424

425425
// Update the value of the input.
426-
inputEl.nativeElement.value = 'Text';
426+
inputEl.value = 'Text';
427427

428428
// Fake behavior of the `(input)` event which should trigger a change detection.
429429
fixture.detectChanges();
@@ -436,7 +436,7 @@ describe('MdInputContainer', function () {
436436
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
437437
fixture.detectChanges();
438438

439-
let inputEl = fixture.debugElement.query(By.css('input'));
439+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
440440
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
441441

442442
expect(labelEl.classList).not.toContain('md-empty');
@@ -445,7 +445,7 @@ describe('MdInputContainer', function () {
445445
fixture.detectChanges();
446446

447447
// Update the value of the input.
448-
inputEl.nativeElement.value = 'Text';
448+
inputEl.value = 'Text';
449449

450450
// Fake behavior of the `(input)` event which should trigger a change detection.
451451
fixture.detectChanges();
@@ -458,17 +458,17 @@ describe('MdInputContainer', function () {
458458
it('should never float the placeholder when floatingPlaceholder is set to false', () => {
459459
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
460460

461-
fixture.componentInstance.shouldFloat = false;
461+
fixture.componentInstance.shouldFloat = 'never';
462462
fixture.detectChanges();
463463

464-
let inputEl = fixture.debugElement.query(By.css('input'));
464+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
465465
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
466466

467467
expect(labelEl.classList).toContain('md-empty');
468468
expect(labelEl.classList).not.toContain('md-float');
469469

470470
// Update the value of the input.
471-
inputEl.nativeElement.value = 'Text';
471+
inputEl.value = 'Text';
472472

473473
// Fake behavior of the `(input)` event which should trigger a change detection.
474474
fixture.detectChanges();
@@ -646,7 +646,7 @@ class MdInputContainerWithValueBinding {
646646

647647
@Component({
648648
template: `
649-
<md-input-container [floatingPlaceholder]="false">
649+
<md-input-container floatingPlaceholder="never">
650650
<input mdInput placeholder="Label">
651651
</md-input-container>
652652
`
@@ -660,7 +660,7 @@ class MdInputContainerWithStaticPlaceholder {}
660660
</md-input-container>`
661661
})
662662
class MdInputContainerWithDynamicPlaceholder {
663-
shouldFloat: boolean = true;
663+
shouldFloat: string = 'always';
664664
}
665665

666666
@Component({

src/lib/input/input-container.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
MdInputContainerUnsupportedTypeError,
2121
MdInputContainerPlaceholderConflictError,
2222
MdInputContainerDuplicatedHintError,
23-
MdInputContainerMissingMdInputError
23+
MdInputContainerMissingMdInputError, MdInputContainerFloatingPlaceholderInvalidError
2424
} from './input-container-errors';
2525

2626

@@ -38,6 +38,9 @@ const MD_INPUT_INVALID_TYPES = [
3838
'submit'
3939
];
4040

41+
/** Valid options for the floatingPlaceholder input binding. */
42+
export type MD_INPUT_PLACEHOLDER_TYPES = 'always' | 'never' | 'auto';
43+
const MD_INPUT_PLACEHOLDER_VALUES = ['always', 'never', 'auto'];
4144

4245
let nextUniqueId = 0;
4346

@@ -245,11 +248,10 @@ export class MdInputContainer implements AfterContentInit {
245248
@Input() dividerColor: 'primary' | 'accent' | 'warn' = 'primary';
246249

247250
/** Whether the floating label should always float or not. */
248-
_shouldAlwaysFloat: boolean = false;
251+
get _shouldAlwaysFloat() { return this._floatingPlaceholder === 'always'; };
249252

250253
/** Whether the placeholder can float or not. */
251-
_floatingPlaceholder: boolean = true;
252-
254+
get _canPlaceholderFloat() { return this._floatingPlaceholder !== 'never'; }
253255

254256
/** Text for the input hint. */
255257
@Input()
@@ -261,15 +263,17 @@ export class MdInputContainer implements AfterContentInit {
261263
private _hintLabel = '';
262264

263265
/**
264-
* Whether the placeholder should always float or just show the placeholder when empty.
265-
* If the value is set to null the placeholder will float if text is entered.
266+
* Whether the placeholder should always float, never float or float as the user types.
266267
*/
267268
@Input()
268269
get floatingPlaceholder() { return this._floatingPlaceholder; }
269-
set floatingPlaceholder(value: boolean) {
270-
this._floatingPlaceholder = value == null || coerceBooleanProperty(value);
271-
this._shouldAlwaysFloat = coerceBooleanProperty(value);
270+
set floatingPlaceholder(value: MD_INPUT_PLACEHOLDER_TYPES) {
271+
if (value && MD_INPUT_PLACEHOLDER_VALUES.indexOf(value) === -1) {
272+
throw new MdInputContainerFloatingPlaceholderInvalidError(value);
273+
}
274+
this._floatingPlaceholder = value || 'auto';
272275
}
276+
private _floatingPlaceholder: MD_INPUT_PLACEHOLDER_TYPES = 'auto';
273277

274278
@ContentChild(MdInputDirective) _mdInputChild: MdInputDirective;
275279

0 commit comments

Comments
 (0)