-
Notifications
You must be signed in to change notification settings - Fork 3
/
is-input-type-supported.ts
35 lines (30 loc) · 1.39 KB
/
is-input-type-supported.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export default (inputType: string): boolean => {
const inputElem = document.createElement('input');
const smile = '1)';
let bool;
inputElem.setAttribute('type', inputType);
bool = inputElem.type !== 'text' && 'style' in inputElem;
// First check the type of the input. Is it exactly like we assigned or it was changed to 'text' automatically?
// Then assign invalid input value.
// If the value wasn't assigned then it looks like input's clearing is related to its custom interface.
if (bool) {
inputElem.value = smile;
inputElem.style.cssText = 'position:absolute;visibility:hidden;';
if (/^range$/.test(inputType) && inputElem.style.webkitAppearance !== undefined) {
document.appendChild(inputElem);
bool = window.getComputedStyle
&& window.getComputedStyle(inputElem, null).webkitAppearance !== 'textfield'
&& (inputElem.offsetHeight !== 0);
document.removeChild(inputElem);
} else if (/^(search|tel)$/.test(inputType)) {
// There's no any special behaviour for `search` & `tel` in specs.
} else if (/^(url|email)$/.test(inputType)) {
// For `url` & `email` we should check their internal validation.
bool = inputElem.checkValidity && inputElem.checkValidity() === false;
} else {
// Check whether input value was transformed into ':)' or not.
bool = inputElem.value !== smile;
}
}
return !!bool;
};