import { findAimClassDom } from '@/utils/utils';
* 注册一个全局自定义指令 `v-clearable`
* 注意:必要三属性:
* 1. v-clearable
* 2. clearable 因为自定义的那个清空图标没有办法传点击事件,所以需要使用自带的那个清空按钮来实现点击事件,
* 只不过把其样式设置为透明了,自定义的那个组件的鼠标事件移除了
* 3. 使用后缀图标插槽#suffix将清空按钮组件和报错图标组件引入和使用
*
* 如果是表单校验,不需要做什么(因为校验失败的时候el-form-item上会自动加上is-error类名),
* 如果不是表单,需要在el-input的外层标签根据错误情况加上is-error类名,错误的时候加
*/
export function clearDirective(app: any) {
app.directive('clearable', {
mounted(el: any, binding: any) {
mountedAction(el, binding);
},
unmounted(el: any, binding: any) {
if (binding.value !== false) {
el.removeEventListener('mouseenter', mouseenterCallBack);
el.removeEventListener('mouseleave', mouseleaveCallBack);
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom) {
inputDom.removeEventListener('input', inputCallBack);
inputDom.removeEventListener('focus', focusCallBack);
inputDom.removeEventListener('blur', blurCallBack);
}
const suffixBox = findAimClassDom(el, 'el-input__suffix');
if (suffixBox) {
const innerBox = findAimClassDom(suffixBox, 'el-input__suffix-inner');
if (innerBox) {
innerBox.removeEventListener('click', inputCallBack);
}
}
}
}
});
}
function mountedAction(el: any, binding: any) {
if (binding.value !== false) {
inputCallBack(el);
changeClassList(el, 3);
el.addEventListener('mouseenter', () => {
return mouseenterCallBack(el);
});
el.addEventListener('mouseleave', () => {
return mouseleaveCallBack(el);
});
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom) {
inputDom.addEventListener('input', () => {
return inputCallBack(el);
});
inputDom.addEventListener('focus', () => {
return focusCallBack(el);
});
inputDom.addEventListener('blur', () => {
return blurCallBack(el);
});
}
const suffixBox = findAimClassDom(el, 'el-input__suffix');
if (suffixBox) {
const innerBox = findAimClassDom(suffixBox, 'el-input__suffix-inner');
if (innerBox) {
innerBox.addEventListener('click', () => {
return inputCallBack(el);
});
}
}
}
}
function inputCallBack(el: any) {
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom && !inputDom.value) {
changeClassList(el, 2);
} else {
changeClassList(el, 1);
}
}
function focusCallBack(el: any) {
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom === document.activeElement && inputDom.value) {
changeClassList(el, 1);
} else {
changeClassList(el, 2);
}
}
function blurCallBack(el: any) {
changeClassList(el, 2);
}
function mouseenterCallBack(el: any) {
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom?.value) {
changeClassList(el, 1);
} else {
changeClassList(el, 3);
}
}
function mouseleaveCallBack(el: any) {
const inputDom = findAimClassDom(el, 'el-input__inner');
if (inputDom !== document.activeElement) {
changeClassList(el, 2);
}
}
* 添加和移除类名
* @param el 输入框的父div盒子
* @param type 类型,1的时候展示清空图标,2的时候展示报错图标, 3的时候什么图标都没有
*/
function changeClassList(el: any, type: number) {
if (type === 1) {
el.classList.remove('show-err-icon-input');
el.classList.add('show-clear-icon-input');
} else if (type === 2) {
el.classList.add('show-err-icon-input');
el.classList.remove('show-clear-icon-input');
} else {
el.classList.remove('show-err-icon-input');
el.classList.remove('show-clear-icon-input');
}
}