input-safe.vue 877 Bytes
<template>
    <Input :value="currentValue" v-bind="$attrs" v-on="$listeners" />
</template>

<script>
import xss from 'util/xss';

export default {
    name: 'input-safe',
    props: ['value'],
    created() {
        this.$listeners.input = this.input;
    },
    data() {
        return {
            currentValue: this.value
        };
    },
    watch: {
        value(val) {
            this.currentValue = val;
        }
    },
    methods: {
        input(val) {
            if (typeof val === 'string') {
                this.currentValue = xss.replaceIllegal(val);
            } else {
                this.currentValue = val;
            }
            this.$emit('input', this.currentValue);
            if (this.currentValue !== val) {
                this.$Message.error('输入内容有敏感字符,已自动清除');
            }
        }
    }
};
</script>