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

效果示意图:

组件代码:

复制代码
<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);
  };
相关推荐
inxunoffice12 分钟前
批量在多个 PDF 的指定位置插入页,如插入封面、插入尾页
前端·pdf
木木黄木木17 分钟前
HTML5 Canvas绘画板项目实战:打造一个功能丰富的在线画板
前端·html·html5
ElasticPDF_新国产PDF编辑器18 分钟前
React 项目 PDF 批注插件库在线版 API 示例教程
javascript
豆芽81919 分钟前
基于Web的交互式智能成绩管理系统设计
前端·python·信息可视化·数据分析·交互·web·数据可视化
不是鱼19 分钟前
XSS 和 CSRF 为什么值得你的关注?
前端·javascript
顺遂时光23 分钟前
微信小程序——解构赋值与普通赋值
前端·javascript·vue.js
anyeyongzhou25 分钟前
img标签请求浏览器资源携带请求头
前端·vue.js
Captaincc33 分钟前
腾讯云 EdgeOne Pages「MCP Server」正式发布
前端·腾讯·mcp
最新资讯动态1 小时前
想让鸿蒙应用快的“飞起”,来HarmonyOS开发者官网“最佳实践-性能专区”
前端
雾岛LYC听风1 小时前
3. 轴指令(omron 机器自动化控制器)——>MC_GearInPos
前端·数据库·自动化