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

效果示意图:

组件代码:

<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);
  };
相关推荐
一路向前的月光1 小时前
React(6)
前端·javascript·react.js
众智创新团队1 小时前
singleTaskAndroid的Activity启动模式知识点总结
前端
祁许2 小时前
【Vue】打包vue3+vite项目发布到github page的完整过程
前端·javascript·vue.js·github
我的86呢!2 小时前
uniapp开发h5部署到服务器
前端·javascript·vue.js·uni-app
小爬的老粉丝2 小时前
基于AIOHTTP、Websocket和Vue3一步步实现web部署平台,无延迟控制台输出,接近原生SSH连接
前端
程序员晚天2 小时前
算法中的冒泡排序
前端
~央千澈~3 小时前
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
前端·测试工具·postman
LBJ辉3 小时前
3. CSS中@scope
前端·css
懒人村杂货铺3 小时前
forwardRef
前端
115432031q3 小时前
基于SpringBoot养老院平台系统功能实现十七
java·前端·后端