Files
dm-manage-web/src/directive/input/phone.js
2023-02-15 09:17:05 +08:00

38 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export default {
inserted(el, bind, vnode, prevVnode) {
const input = el.nodeName.toLowerCase() === 'input' ? el : el.getElementsByTagName('input')[0];
// 改变值过后需要主动触发input时间组件绑定的值才会改变
let triggerBySelf = false;
if (input) {
input.addEventListener('input', () => {
if (triggerBySelf) {
triggerBySelf = false;
return;
}
const val = input.value.replace(/[^0-9]/gi, '');
if (val === '') {
input.value = '';
return;
}
if (bind.expression) {
try {
const option = bind.value;
if (typeof option === 'object') {
console.log(option);
// 改变值过后需要主动触发input时间组件绑定的值才会改变
const ev = new Event('input', { bubbles: true });
input.value = val;
triggerBySelf = true;
input.dispatchEvent(ev);
}
} catch (err) {
input.value = val;
console.error(err);
}
}
});
}
}
};