Skip to content

Commit 9a1314e

Browse files
committed
Change API to accept string values
1 parent 2055653 commit 9a1314e

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/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
@@ -62,8 +62,8 @@ describe('MdInputContainer', function () {
6262

6363
let inputContainer = fixture.debugElement.query(By.directive(MdInputContainer))
6464
.componentInstance as MdInputContainer;
65-
expect(inputContainer.floatingPlaceholder).toBe(true,
66-
'Expected MdInputContainer to default to having floating placeholders turned on');
65+
expect(inputContainer.floatingPlaceholder).toBe('auto',
66+
'Expected MdInputContainer to set floatingLabel to auto by default.');
6767
});
6868

6969
it('should not be treated as empty if type is date',
@@ -483,20 +483,20 @@ describe('MdInputContainer', function () {
483483
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
484484
fixture.detectChanges();
485485

486-
let inputEl = fixture.debugElement.query(By.css('input'));
486+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
487487
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
488488

489489
expect(labelEl.classList).not.toContain('md-empty');
490490
expect(labelEl.classList).toContain('md-float');
491491

492-
fixture.componentInstance.shouldFloat = null;
492+
fixture.componentInstance.shouldFloat = 'auto';
493493
fixture.detectChanges();
494494

495495
expect(labelEl.classList).toContain('md-empty');
496496
expect(labelEl.classList).toContain('md-float');
497497

498498
// Update the value of the input.
499-
inputEl.nativeElement.value = 'Text';
499+
inputEl.value = 'Text';
500500

501501
// Fake behavior of the `(input)` event which should trigger a change detection.
502502
fixture.detectChanges();
@@ -509,7 +509,7 @@ describe('MdInputContainer', function () {
509509
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
510510
fixture.detectChanges();
511511

512-
let inputEl = fixture.debugElement.query(By.css('input'));
512+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
513513
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
514514

515515
expect(labelEl.classList).not.toContain('md-empty');
@@ -518,7 +518,7 @@ describe('MdInputContainer', function () {
518518
fixture.detectChanges();
519519

520520
// Update the value of the input.
521-
inputEl.nativeElement.value = 'Text';
521+
inputEl.value = 'Text';
522522

523523
// Fake behavior of the `(input)` event which should trigger a change detection.
524524
fixture.detectChanges();
@@ -531,17 +531,17 @@ describe('MdInputContainer', function () {
531531
it('should never float the placeholder when floatingPlaceholder is set to false', () => {
532532
let fixture = TestBed.createComponent(MdInputContainerWithDynamicPlaceholder);
533533

534-
fixture.componentInstance.shouldFloat = false;
534+
fixture.componentInstance.shouldFloat = 'never';
535535
fixture.detectChanges();
536536

537-
let inputEl = fixture.debugElement.query(By.css('input'));
537+
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
538538
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
539539

540540
expect(labelEl.classList).toContain('md-empty');
541541
expect(labelEl.classList).not.toContain('md-float');
542542

543543
// Update the value of the input.
544-
inputEl.nativeElement.value = 'Text';
544+
inputEl.value = 'Text';
545545

546546
// Fake behavior of the `(input)` event which should trigger a change detection.
547547
fixture.detectChanges();
@@ -741,7 +741,7 @@ class MdInputContainerWithValueBinding {
741741

742742
@Component({
743743
template: `
744-
<md-input-container [floatingPlaceholder]="false">
744+
<md-input-container floatingPlaceholder="never">
745745
<input mdInput placeholder="Label">
746746
</md-input-container>
747747
`
@@ -755,7 +755,7 @@ class MdInputContainerWithStaticPlaceholder {}
755755
</md-input-container>`
756756
})
757757
class MdInputContainerWithDynamicPlaceholder {
758-
shouldFloat: boolean = true;
758+
shouldFloat: string = 'always';
759759
}
760760

761761
@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

@@ -254,11 +257,10 @@ export class MdInputContainer implements AfterContentInit {
254257
@Input() dividerColor: 'primary' | 'accent' | 'warn' = 'primary';
255258

256259
/** Whether the floating label should always float or not. */
257-
_shouldAlwaysFloat: boolean = false;
260+
get _shouldAlwaysFloat() { return this._floatingPlaceholder === 'always'; };
258261

259262
/** Whether the placeholder can float or not. */
260-
_floatingPlaceholder: boolean = true;
261-
263+
get _canPlaceholderFloat() { return this._floatingPlaceholder !== 'never'; }
262264

263265
/** Text for the input hint. */
264266
@Input()
@@ -273,15 +275,17 @@ export class MdInputContainer implements AfterContentInit {
273275
_hintLabelId: string = `md-input-hint-${nextUniqueId++}`;
274276

275277
/**
276-
* Whether the placeholder should always float or just show the placeholder when empty.
277-
* If the value is set to null the placeholder will float if text is entered.
278+
* Whether the placeholder should always float, never float or float as the user types.
278279
*/
279280
@Input()
280281
get floatingPlaceholder() { return this._floatingPlaceholder; }
281-
set floatingPlaceholder(value: boolean) {
282-
this._floatingPlaceholder = value == null || coerceBooleanProperty(value);
283-
this._shouldAlwaysFloat = coerceBooleanProperty(value);
282+
set floatingPlaceholder(value: MD_INPUT_PLACEHOLDER_TYPES) {
283+
if (value && MD_INPUT_PLACEHOLDER_VALUES.indexOf(value) === -1) {
284+
throw new MdInputContainerFloatingPlaceholderInvalidError(value);
285+
}
286+
this._floatingPlaceholder = value || 'auto';
284287
}
288+
private _floatingPlaceholder: MD_INPUT_PLACEHOLDER_TYPES = 'auto';
285289

286290
@ContentChild(MdInputDirective) _mdInputChild: MdInputDirective;
287291

src/lib/input/input.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.

0 commit comments

Comments
 (0)