This commit is contained in:
guochunsi
2026-01-20 17:33:36 +08:00
parent 39c07a03ee
commit 48f65a6d1b
26 changed files with 946 additions and 590 deletions

View File

@@ -1,5 +1,54 @@
import type { App } from 'vue';
/**
* 输入过滤指令
* @directive v-input-filter如 `<el-input v-model="phone" v-input-filter="'number'" />`
* @param number - 纯数字
* @param alphanumeric - 数字+字母
* @param idcard - 数字+字母+括号(支持大陆、港澳台身份证)
* @param tel - 固话格式(数字+横杠+括号+空格)
*/
export function inputFilterDirective(app: App) {
app.directive('input-filter', {
mounted(el, binding) {
const input = el.querySelector('input') || el;
const filterType = binding.value || 'number';
const filters: { [key: string]: RegExp } = {
number: /[^\d]/g, // 纯数字
alphanumeric: /[^0-9A-Za-z]/g, // 数字+字母
idcard: /[^0-9A-Za-z()]/g, // 数字+字母+括号(支持港澳台)
tel: /[^\d\-() ]/g, // 固话:数字+横杠+括号+空格
};
const handleInput = (e: Event) => {
const target = e.target as HTMLInputElement;
const regex = filters[filterType];
if (regex && target.value) {
const newValue = target.value.replace(regex, '');
if (newValue !== target.value) {
target.value = newValue;
// 触发 input 事件,确保 v-model 更新
target.dispatchEvent(new Event('input'));
}
}
};
input.addEventListener('input', handleInput);
// 保存 handler用于 unmounted 时清理
(el as any)._inputFilterHandler = handleInput;
(el as any)._inputFilterElement = input;
},
unmounted(el) {
const handler = (el as any)._inputFilterHandler;
const input = (el as any)._inputFilterElement;
if (handler && input) {
input.removeEventListener('input', handler);
}
},
});
}
/**
* 按钮波浪指令
* @directive 默认方式v-waves如 `<div v-waves></div>`

View File

@@ -1,17 +1,20 @@
import type { App } from 'vue';
import { authDirective } from '/@/directive/authDirective';
import { wavesDirective } from '/@/directive/customDirective';
import { wavesDirective, inputFilterDirective } from '/@/directive/customDirective';
/**
* 导出指令方法v-xxx
* @methods authDirective 用户权限指令用法v-auth
* @methods wavesDirective 按钮波浪指令用法v-waves
* @methods inputFilterDirective 输入过滤指令用法v-input-filter
*/
export function directive(app: App) {
// 用户权限指令
authDirective(app);
// 按钮波浪指令
wavesDirective(app);
// 输入过滤指令
inputFilterDirective(app);
// focus
app.directive('focus', {
mounted(el) {