editable.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <template>
  2. <el-tag v-for="tag in dynamicTags" :key="tag" class="mx-1" closable :disable-transitions="false" @close="handleClose(tag)">
  3. {{ tag }}
  4. </el-tag>
  5. <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue" class="ml-1 w-20" size="small" @keyup.enter="handleInputConfirm" @blur="handleInputConfirm" />
  6. <el-button v-else class="button-new-tag ml-1" size="small" @click="showInput"> + New Tag </el-button>
  7. </template>
  8. <script lang="ts" setup>
  9. import { nextTick, ref } from 'vue'
  10. import type { ElInput } from 'element-plus'
  11. const inputValue = ref('')
  12. const dynamicTags = ref(['Tag 1', 'Tag 2', 'Tag 3'])
  13. const inputVisible = ref(false)
  14. const InputRef = ref<InstanceType<typeof ElInput>>()
  15. const handleClose = (tag: string) => {
  16. dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1)
  17. }
  18. const showInput = () => {
  19. inputVisible.value = true
  20. nextTick(() => {
  21. InputRef.value!.input!.focus()
  22. })
  23. }
  24. const handleInputConfirm = () => {
  25. if (inputValue.value) {
  26. dynamicTags.value.push(inputValue.value)
  27. }
  28. inputVisible.value = false
  29. inputValue.value = ''
  30. }
  31. </script>