AddItem.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="add-item" :class="{ active }">
  3. <div class="add-item__prefix">
  4. <slot name="prefix"></slot>
  5. </div>
  6. <img :src="icon || PlaeceHolderAddIcon" alt="" />
  7. <div v-if="isEditing">
  8. <input
  9. ref="inputRef"
  10. v-model="editingContent"
  11. @blur="saveEdit"
  12. @keyup.enter="saveEdit"
  13. @keyup.esc="cancelEdit"
  14. class="add-item__input"
  15. />
  16. </div>
  17. <span v-else @dblclick="icon ? startEdit() : null" class="add-item__content">
  18. {{ content }}
  19. </span>
  20. <div class="add-item__suffix">
  21. <slot name="suffix"></slot>
  22. </div>
  23. </div>
  24. </template>
  25. <script lang="ts" setup>
  26. import { ref } from 'vue';
  27. import PlaeceHolderAddIcon from 'assets/svg/placeholder-add-item.svg';
  28. const props = defineProps<{
  29. icon?: string;
  30. active?: boolean;
  31. content: string;
  32. }>();
  33. const emit = defineEmits<{
  34. (e: 'update:content', value: string): void;
  35. }>();
  36. const isEditing = ref(false);
  37. const editingContent = ref(props.content);
  38. const inputRef = ref<HTMLInputElement | null>(null);
  39. function startEdit() {
  40. if (!props.icon) return;
  41. isEditing.value = true;
  42. editingContent.value = props.content;
  43. setTimeout(() => {
  44. inputRef.value?.focus();
  45. });
  46. }
  47. function saveEdit() {
  48. if (editingContent.value.trim() !== '') {
  49. emit('update:content', editingContent.value);
  50. } else {
  51. editingContent.value = props.content;
  52. }
  53. isEditing.value = false;
  54. }
  55. function cancelEdit() {
  56. editingContent.value = props.content;
  57. isEditing.value = false;
  58. }
  59. </script>
  60. <style lang="scss" scoped>
  61. .add-item {
  62. display: flex;
  63. align-items: center;
  64. gap: 12px;
  65. width: 100%;
  66. height: 24px;
  67. font-size: 14px;
  68. color: $text-color;
  69. img {
  70. height: 100%;
  71. }
  72. &.active {
  73. color: $white-color;
  74. }
  75. &__content {
  76. flex-grow: 1;
  77. }
  78. &__prefix,
  79. &__suffix {
  80. display: flex;
  81. align-items: center;
  82. cursor: pointer;
  83. }
  84. &__suffix {
  85. margin-left: auto;
  86. }
  87. &__input {
  88. width: 100%;
  89. height: 100%;
  90. font-size: 14px;
  91. border: 1px solid $text-color;
  92. border-radius: 4px;
  93. color: $text-color;
  94. padding: 2px 10px;
  95. outline: none;
  96. border: none;
  97. }
  98. }
  99. </style>