实用组件:双击重命名

该代码段展示了一个Vue组件,它处理元素的双击事件来切换编辑模式,使用`<el-input>`进行编辑,并在输入后更新模型值。同时,它还防止了双击事件触发单击事件,并在用户完成后调用successRename函数。
摘要由CSDN通过智能技术生成
<template>
<div @dblclick="rename(true, $event)" @click="dbDiff" :class="isEdit ? 'is-sedit': ''" ref="reDom">
  <el-input @blur="rename(false, $event)" maxlength="40" @keyup.enter="rename(false, $event)" v-model="label" v-if="isEdit && !props.disabled" ref="inpRef" />
  <popover :list="modelValue" v-else></popover>
</div>
</template>
<script setup>
import { ref, defineProps, nextTick, defineEmits } from 'vue'
// import $ from 'jquery'
const props = defineProps({
  modelValue: String,
  successRename: Function,
  disabled: Boolean
})
const emits = defineEmits(['update:modelValue', 'edit', 'singleClick'])
const isEdit = ref(false)
const label = ref(props.modelValue)
const inpRef = ref(null)
const reDom = ref()
let timeout = null
const rename = (edit, e) => {
  e.stopPropagation()
  e.preventDefault()
  clearTimeout(timeout)
  // 避免重复触发
  if (isEdit.value === edit) return
  isEdit.value = edit
  emits('edit', edit)
  nextTick(() => {
    if (isEdit.value) {
      inpRef.value.focus()
      label.value = props.modelValue
    } else {
      if (label.value && label.value !== props.modelValue) {
        emits('update:modelValue', label.value)
        props.successRename && props.successRename(label.value)
      }
    }
  })
}

// 为了确保双击不会触发单击的事件
const dbDiff = (e) => {
  e.stopPropagation()
  clearTimeout(timeout)
  if (isEdit.value) return
  timeout = setTimeout(function() {
    emits('singleClick', e)
    reDom.value.parentNode.click()
  }, 300)
}

</script>
<style lang="scss" scoped>
.is-edit {

}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值