input失去焦点事件和点击事件冲突的解决思路

首先说一下应用场景

 

el-input框为禁用状态时,点击取消禁用,并显示后方两个按钮,

点击绿色按钮保存input框当前值,失去焦点或点击红色框时恢复改变前的值

思路就是不在input上的失去焦点方法,而是手动调用失去焦点方法

注意mouseleave 事件不支持冒泡 。

mouseout 父元素内如果有子元素,鼠标离开子元素且未离开父元素,也会触发mouseout

<template>
    <div>
        <div
            style="display: flex; align-items: center"
            @mouseleave="handlemouseout"
        >
            <el-input
                ref="elInputFocus"
                :value="value"
                @input="elInputValue"
                :disabled="disabled"
                autofocus
            ></el-input>
            <el-button
                type="success"
                icon="el-icon-check"
                circle
                size="mini"
                v-if="!disabled"
                @click="handleConfirm"
            ></el-button>
            <el-button
                type="danger"
                icon="el-icon-close"
                circle
                size="mini"
                v-if="!disabled"
                @click="handleClose"
            ></el-button>
        </div>
    </div>
</template>

<script>
import { Input } from "element-ui";
export default {
    extends: Input,
    model: {
        prop: ["value"],
    },
    components: {},
    props: {
        value: {
            type: String,
            default: "",
        },
        disabled: {
            type: Boolean,
            default: false,
        },
    },
    watch: {
        disabled(val) {
            if (!val) {
                this.$nextTick(() => {
                    this.$refs.elInputFocus.focus();
                });
            }
            else{
                this.flag=false
            }
            
        },
        value(newval, oldval) {
            this.oldValue = oldval;
            this.newValue = newval;
        },
    },
    computed: {},
    data() {
        return {
            oldValue: "",
            newValue: "",
            flag: false,
        };
    },
    methods: {
        handlemouseout(e){
            console.log(e);
            this.flag = true
            this.handleBlur()
        },
        elInputValue(e) {
            this.$emit("input", e);
        },
        handleConfirm() {
            this.flag = true;
            this.$emit("input", this.newValue);
            this.$emit("confirm");
        },
        handleClose() {
            this.$refs.elInputFocus.blur();
            if (this.oldValue) {
                this.$emit("input", this.oldValue);
            }
            this.$emit("close");
            
        },
        handleBlur() {
          
            if (this.disabled && this.flag) {
                return;
            }
            this.$refs.elInputFocus.blur();
            if (this.oldValue) {
                this.$emit("input", this.oldValue);
            }
            this.$emit("blur");
        },
    },
    created() {},
    mounted() {},
};
</script>
<style lang="scss" scoped>
.el-input {
    margin-right: 10px;
}
.el-button {
    width: 26px;
    height: 26px;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值