Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): prevent overlapping placeholder on inputs with native masking #1860

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import {Component} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {MdInput, MdInputModule} from './input';
import {MdInput, MdInputModule, TYPES_WITH_MASK} from './input';

function isInternetExplorer11() {
return 'ActiveXObject' in window;
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('MdInput', function () {
MdInputWithMin,
MdInputWithStep,
MdInputWithTabindex,
MdInputDateTestController,
MdInputWithMask,
MdInputTextTestController,
MdInputPasswordTestController,
MdInputNumberTestController,
Expand All @@ -80,11 +80,11 @@ describe('MdInput', function () {
.toBe(true, 'Expected MdInput to default to having floating placeholders turned on');
});

it('should not be treated as empty if type is date', () => {
if (isInternetExplorer11()) {
it('should not be treated as empty if the input has native masking', () => {
if (!TYPES_WITH_MASK.length) {
return;
}
let fixture = TestBed.createComponent(MdInputDateTestController);
let fixture = TestBed.createComponent(MdInputWithMask);
fixture.componentInstance.placeholder = 'Placeholder';
fixture.detectChanges();

Expand Down Expand Up @@ -787,8 +787,9 @@ class MdInputWithStep { }
@Component({template: `<md-input [tabindex]="tabIndex"></md-input>`})
class MdInputWithTabindex { }

@Component({template: `<md-input type="date" [placeholder]="placeholder"></md-input>`})
class MdInputDateTestController {
@Component({template:
`<md-input type="${TYPES_WITH_MASK[0] || 'text'}" [placeholder]="placeholder"></md-input>`})
class MdInputWithMask {
placeholder: string = '';
}

Expand Down
17 changes: 16 additions & 1 deletion src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ const MD_INPUT_INVALID_INPUT_TYPE = [
'checkbox',
];

let featureTestInput: HTMLInputElement = document.createElement('input');

/** Native input types, supported by the current browser, that have input masking. */
export const TYPES_WITH_MASK = [
'range', 'date', 'month', 'week', 'time', 'datetime', 'datetime-local'
].filter(value => {
featureTestInput.setAttribute('type', value);
return featureTestInput.type === value;
});

featureTestInput = null;


let nextUniqueId = 0;

Expand Down Expand Up @@ -139,7 +151,10 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange

/** Readonly properties. */
get focused() { return this._focused; }
get empty() { return (this._value == null || this._value === '') && this.type !== 'date'; }
get empty() {
return (this._value == null || this._value === '') &&
TYPES_WITH_MASK.indexOf(this.type) === -1;
}
get characterCount(): number {
return this.empty ? 0 : ('' + this._value).length;
}
Expand Down