Skip to content

Commit fe2b493

Browse files
dragerhansl
authored andcommitted
fix(MdInput): Input should not be treated as empty if it is a date field. (#846)
* fix(MdInput): Input should not be treated as empty if it is a date field. Fixes #845 * fix(MdInput): Input should not be treated as empty if it is a date field. Fixes #845 * fix(md-input): Check for type in empty getter instead * test(MdInput): Add tests for empty check * test(MdInput): Update empty check tests to match the other tests new syntax * test(MdInput): Fix linting issue * fix(MdInput): Input should not be treated as empty if it is a date field. Fixes #845 * fix(md-input): Check for type in empty getter instead * fix(md-input): Check for type in empty getter instead * test(MdInput): Add tests for empty check * test(MdInput): Update empty check tests to match the other tests new syntax * test(MdInput): Skip empty check tests for IE11 * test(MdInput): Resolve linting issues * test(MdInput): Update empty() check tests to match new test syntax
1 parent 8099a72 commit fe2b493

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/lib/input/input.spec.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {FormsModule} from '@angular/forms';
77
import {By} from '@angular/platform-browser';
88
import {MdInput, MdInputModule} from './input';
99

10+
function isInternetExplorer11() {
11+
return 'ActiveXObject' in window;
12+
}
1013

1114
describe('MdInput', function () {
1215
beforeEach(async(() => {
@@ -50,6 +53,10 @@ describe('MdInput', function () {
5053
MdInputWithMin,
5154
MdInputWithStep,
5255
MdInputWithTabindex,
56+
MdInputDateTestController,
57+
MdInputTextTestController,
58+
MdInputPasswordTestController,
59+
MdInputNumberTestController,
5360
],
5461
});
5562

@@ -63,6 +70,58 @@ describe('MdInput', function () {
6370
expect(fixture.debugElement.query(By.css('input'))).toBeTruthy();
6471
});
6572

73+
it('should not be treated as empty if type is date', async(() => {
74+
if (isInternetExplorer11()) {
75+
return;
76+
}
77+
let fixture = TestBed.createComponent(MdInputDateTestController);
78+
fixture.componentInstance.placeholder = 'Placeholder';
79+
fixture.detectChanges();
80+
81+
let el = fixture.debugElement.query(By.css('label')).nativeElement;
82+
expect(el).not.toBeNull();
83+
expect(el.className.includes('md-empty')).toBe(false);
84+
}));
85+
86+
it('should treat text input type as empty at init', async(() => {
87+
if (isInternetExplorer11()) {
88+
return;
89+
}
90+
let fixture = TestBed.createComponent(MdInputTextTestController);
91+
fixture.componentInstance.placeholder = 'Placeholder';
92+
fixture.detectChanges();
93+
94+
let el = fixture.debugElement.query(By.css('label')).nativeElement;
95+
expect(el).not.toBeNull();
96+
expect(el.className.includes('md-empty')).toBe(true);
97+
}));
98+
99+
it('should treat password input type as empty at init', async(() => {
100+
if (isInternetExplorer11()) {
101+
return;
102+
}
103+
let fixture = TestBed.createComponent(MdInputPasswordTestController);
104+
fixture.componentInstance.placeholder = 'Placeholder';
105+
fixture.detectChanges();
106+
107+
let el = fixture.debugElement.query(By.css('label')).nativeElement;
108+
expect(el).not.toBeNull();
109+
expect(el.className.includes('md-empty')).toBe(true);
110+
}));
111+
112+
it('should treat number input type as empty at init', async(() => {
113+
if (isInternetExplorer11()) {
114+
return;
115+
}
116+
let fixture = TestBed.createComponent(MdInputNumberTestController);
117+
fixture.componentInstance.placeholder = 'Placeholder';
118+
fixture.detectChanges();
119+
120+
let el = fixture.debugElement.query(By.css('label')).nativeElement;
121+
expect(el).not.toBeNull();
122+
expect(el.className.includes('md-empty')).toBe(true);
123+
}));
124+
66125
// TODO(kara): update when core/testing adds fix
67126
it('support ngModel', async(() => {
68127
let fixture = TestBed.createComponent(MdInputBaseTestController);
@@ -701,3 +760,23 @@ class MdInputWithStep { }
701760

702761
@Component({template: `<md-input [tabindex]="tabIndex"></md-input>`})
703762
class MdInputWithTabindex { }
763+
764+
@Component({template: `<md-input type="date" [placeholder]="placeholder"></md-input>`})
765+
class MdInputDateTestController {
766+
placeholder: string = '';
767+
}
768+
769+
@Component({template: `<md-input type="text" [placeholder]="placeholder"></md-input>`})
770+
class MdInputTextTestController {
771+
placeholder: string = '';
772+
}
773+
774+
@Component({template: `<md-input type="password" [placeholder]="placeholder"></md-input>`})
775+
class MdInputPasswordTestController {
776+
placeholder: string = '';
777+
}
778+
779+
@Component({template: `<md-input type="number" [placeholder]="placeholder"></md-input>`})
780+
class MdInputNumberTestController {
781+
placeholder: string = '';
782+
}

src/lib/input/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
128128

129129
/** Readonly properties. */
130130
get focused() { return this._focused; }
131-
get empty() { return this._value == null || this._value === ''; }
131+
get empty() { return (this._value == null || this._value === '') && this.type !== 'date'; }
132132
get characterCount(): number {
133133
return this.empty ? 0 : ('' + this._value).length;
134134
}

0 commit comments

Comments
 (0)