vue和react 的@功能简易版

在这里插入图片描述

vue

<template>
  <div class="container">
    <div class="text" ref="input" :contenteditable="!flag" />
    <ul class="boxs" v-show="flag">
      <li
        class="box"
        v-for="(item, index) in list"
        :key="index"
        @click="acitveHandle(item)"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const input = ref(null)
const flag = ref(false)
const list = ['list1', 'list2', 'list3', 'list4']

onMounted(() => {
  input.value?.addEventListener('input', ({ data, target}) => {
    flag.value = data === '@'
  })
})

const acitveHandle = (item) => {
  flag.value = false
  const str = input.value.innerHTML.slice(0, -1)
  input.value.innerHTML = str + `<button contenteditable="false" οnclick="return false">@${item}</button>`
}

</script>

<style lang="scss" scoped>
ul,
li {
  list-style: none;
}
a {
  text-decoration: none;
  color: #676767;
}
.container {
  position: relative;
  .text {
    width: 200px;
    height: 30px;
    position: absolute;
    border: 1px solid #676767;
  }
  .boxs {
    position: absolute;
    top: 30px;
    left: 0px;
    background-color: rgba(255, 255, 255, 0.1);
    .box {
      height: 30px;
      width: 100px;
      box-shadow: 0 2px 5px gray;
      cursor: pointer;
      padding: 5px 10px;
      margin-bottom: 2px;
    }
  }
}
</style>

react


const [flag, setFlag] = useState(false)
const inputRef = useRef(null)
const list = ['list1', 'list2', 'list3', 'list4']

function render() {

  const aciveHandle = (text) => {
    setFlag(false)
    const str = inputRef.current.innerHTML.slice(0, -1)
    innerHTML.current = str + `<button contenteditable="false" οnclick="return false">@${text}</button>`
  }

  return (
    <div class="container">
      <div
        class="text"
        ref={ref => {
          inputRef.current = ref
        }}
        contenteditable={flag ? 'false' : 'true'}
      />
      {
        flag && (
          <ul class="boxs">
            {
              list.map(it => {
                return (
                  <li class="box" onClick={() => aciveHandle(it)}>{it}</li>
                )
              })
            }
          </ul>
        )
      }
    </div>
  );
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值