前端-如何做一个关键字生成组件

效果示意图:

组件代码:

复制代码
<template>
  <div class="custom-tag">
    <input
      v-if="tag.editing"
      v-model="localText"
      @blur="handleBlur"
      @keyup.enter="handleBlur"
      @input="adjustInputWidth"
      ref="inputRef"
      class="tag-input"
    />
    <span v-else>{{ tag.text }}</span>
    <CloseOutlined @click="$emit('remove')" class="small-icon" />
  </div>
</template>

<script setup>
  import { ref, watch, nextTick } from 'vue';
  import { CloseOutlined } from '@ant-design/icons-vue';

  const props = defineProps({
    tag: {
      type: Object,
      required: true
    }
  });

  const emit = defineEmits(['update:tag', 'remove']);

  const localText = ref(props.tag.text);
  const editing = ref(props.tag.editing);
  const inputRef = ref(null);

  watch(
    () => props.tag.text,
    (newText) => {
      localText.value = newText;
    }
  );

  const handleBlur = () => {
    editing.value = false;
    emit('update:tag', {
      ...props.tag,
      text: localText.value,
      editing: editing.value
    });
  };

  const adjustInputWidth = () => {
    nextTick(() => {
      if (inputRef.value) {
        inputRef.value.style.width = `${inputRef.value.scrollWidth}px`;
      }
    });
  };
</script>

<style scoped>
  .custom-tag {
    display: flex;
    align-items: center;
    margin-right: 5px;
    background-color: #ff811a;
    border-radius: 4px;
    padding: 2px 5px;
    color: white;
  }

  .tag-input {
    border: none;
    background-color: #ff811a;
    padding: 0 5px;
    border-radius: 4px;
    color: white;
    outline: none; /* 去掉输入框聚焦时的默认边框 */
    width: 25px; /* 设置初始宽度 */
    min-width: 25px; /* 设置最小宽度 */
    box-sizing: border-box; /* 确保内边距和边框包含在宽度内 */
  }

  .small-icon {
    font-size: 0.5em; /* 将图标大小缩小为原来的一半 */
    cursor: pointer;
    color: white;
    margin-left: 5px; /* 增加一些间距,使图标更明显 */
  }

  .small-icon:hover {
    color: #ffcc00; /* 悬停时改变颜色 */
  }
</style>

父组件中使用方法:

复制代码
<div style="display: flex; margin-bottom: 5px">
          关键字:
          <tags
            v-for="(tag, index) in tagsList"
            :key="index"
            :tag="tag"
            @remove="removeTag(index)"
          />
          <div
            v-show="tagsList.length < 5"
            style="color: #ff811a"
            @click="addKey"
          >
            +关键字
          </div>
        </div>



  const addKey = () => {
    tagsList.value.push({ text: '', editing: true });
  };

  const removeTag = (index) => {
    console.log('tagsList', tagsList);
    tagsList.value.splice(index, 1);
  };
相关推荐
前端市界16 分钟前
当20个并发请求拖垮你的应用:从TCP握手到HTTP/2的终极排错指南
前端
量子-Alex16 分钟前
【大模型与智能体论文】REACT:协同语言模型中的推理与行动
前端·react.js·语言模型
冷崖34 分钟前
QML-动画
前端
太过平凡的小蚂蚁35 分钟前
适配器模式:让不兼容的接口协同工作
java·前端·javascript
涔溪42 分钟前
在 Electron 框架中实现数据库的连接、读取和写入
javascript·数据库·electron
锈儿海老师1 小时前
超越平台:Vercel 的野心是定义编程语言的未来吗?
前端·javascript·架构
泷羽Sec-静安2 小时前
Less-7 GET-Dump into outfile-String
android·前端·网络·sql·安全·web安全
深入理解GEE云计算2 小时前
遥感生态指数(RSEI):理论发展、方法论争与实践进展
javascript·人工智能·算法·机器学习
IT_陈寒2 小时前
从2秒到200ms:我是如何用JavaScript优化页面加载速度的🚀
前端·人工智能·后端
天天向上10242 小时前
vue 网站导航栏
前端·javascript·vue.js