Skip to content

Commit 9c43fe0

Browse files
committed
fix(input): prevent overlapping placeholder on inputs with native masking
Prevents the Material placeholder overlapping with native input masking (e.g. on `input[type="time"]`. Previously, this was hardcoded for `date`, however it didn't take into account that not all browsers support `date` inputs. This change runs a feature test on each of the types that should have input masking and only triggers the special behavior if the type is supported. Fixes #1853.
1 parent 009046f commit 9c43fe0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/lib/input/input.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ const MD_INPUT_INVALID_INPUT_TYPE = [
4040
'checkbox',
4141
];
4242

43+
let featureTestInput: HTMLInputElement = document.createElement('input');
44+
45+
/** Native input types, supported by the current browser, that have input masking. */
46+
const TYPES_WITH_MASK = [
47+
'range', 'date', 'month', 'week', 'time', 'datetime', 'datetime-local'
48+
].filter(value => {
49+
featureTestInput.setAttribute('type', value);
50+
return featureTestInput.type === value;
51+
});
52+
53+
featureTestInput = null;
54+
4355

4456
let nextUniqueId = 0;
4557

@@ -139,7 +151,10 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
139151

140152
/** Readonly properties. */
141153
get focused() { return this._focused; }
142-
get empty() { return (this._value == null || this._value === '') && this.type !== 'date'; }
154+
get empty() {
155+
return (this._value == null || this._value === '') &&
156+
TYPES_WITH_MASK.indexOf(this.type) === -1;
157+
}
143158
get characterCount(): number {
144159
return this.empty ? 0 : ('' + this._value).length;
145160
}

0 commit comments

Comments
 (0)