ant-design-vue/components/_util/antInputDirective.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* Not type checking this file because flow doesn't like attaching
* properties to Elements.
*/
2019-01-12 11:33:27 +08:00
export const inBrowser = typeof window !== 'undefined';
export const UA = inBrowser && window.navigator.userAgent.toLowerCase();
export const isIE9 = UA && UA.indexOf('msie 9.0') > 0;
function makeMap(str, expectsLowerCase) {
const map = Object.create(null);
const list = str.split(',');
for (let i = 0; i < list.length; i++) {
2019-01-12 11:33:27 +08:00
map[list[i]] = true;
}
2019-01-12 11:33:27 +08:00
return expectsLowerCase ? val => map[val.toLowerCase()] : val => map[val];
}
2019-01-12 11:33:27 +08:00
const isTextInputType = makeMap('text,number,password,search,email,tel,url');
2019-01-12 11:33:27 +08:00
function onCompositionStart(e) {
e.target.composing = true;
}
2019-01-12 11:33:27 +08:00
function onCompositionEnd(e) {
// prevent triggering an input event for no reason
2019-01-12 11:33:27 +08:00
if (!e.target.composing) return;
e.target.composing = false;
trigger(e.target, 'input');
}
2019-01-12 11:33:27 +08:00
function trigger(el, type) {
const e = document.createEvent('HTMLEvents');
e.initEvent(type, true, true);
el.dispatchEvent(e);
}
/* istanbul ignore if */
if (isIE9) {
// http://www.matts411.com/post/internet-explorer-9-oninput/
document.addEventListener('selectionchange', () => {
2019-01-12 11:33:27 +08:00
const el = document.activeElement;
if (el && el.vmodel) {
2019-01-12 11:33:27 +08:00
trigger(el, 'input');
}
2019-01-12 11:33:27 +08:00
});
}
2019-01-12 11:33:27 +08:00
export function antInput(Vue) {
2018-12-13 22:47:23 +08:00
return Vue.directive('ant-input', {
2019-01-12 11:33:27 +08:00
inserted(el, binding, vnode, oldVnode) {
2018-12-13 22:47:23 +08:00
if (vnode.tag === 'textarea' || isTextInputType(el.type)) {
if (!binding.modifiers || !binding.modifiers.lazy) {
2019-01-12 11:33:27 +08:00
el.addEventListener('compositionstart', onCompositionStart);
el.addEventListener('compositionend', onCompositionEnd);
2018-12-13 22:47:23 +08:00
// Safari < 10.2 & UIWebView doesn't fire compositionend when
// switching focus before confirming composition choice
// this also fixes the issue where some browsers e.g. iOS Chrome
// fires "change" instead of "input" on autocomplete.
2019-01-12 11:33:27 +08:00
el.addEventListener('change', onCompositionEnd);
2018-12-13 22:47:23 +08:00
/* istanbul ignore if */
if (isIE9) {
2019-01-12 11:33:27 +08:00
el.vmodel = true;
}
}
2018-12-13 22:47:23 +08:00
}
},
2019-01-12 11:33:27 +08:00
});
2018-12-13 22:47:23 +08:00
}
export default {
install: (Vue, options) => {
2019-01-12 11:33:27 +08:00
antInput(Vue);
},
2019-01-12 11:33:27 +08:00
};