| 1 | <template> |
| 2 | <div class="kv-editor"> |
| 3 | <template v-if="readonly"> |
| 4 | <div v-if="modelValue.length === 0" class="kv-empty">—</div> |
| 5 | <div v-for="(entry, index) in modelValue" :key="index" class="kv-readonly-row"> |
| 6 | <span class="kv-readonly-key">{{ entry.key }}</span> |
| 7 | <span class="kv-readonly-value">{{ entry.value }}</span> |
| 8 |
| </div> |
| 9 | </template> |
| 10 | <template v-else> |
| 11 | <div v-for="(entry, index) in modelValue" :key="index" class="kv-row"> |
| 12 | <el-input |
| 13 | :model-value="entry.key" |
| 14 | :placeholder="keyPlaceholder" |
| 15 | class="kv-input" |
| 16 | @update:model-value="updateEntry(index, 'key', $event)" |
| 17 | /> |
| 18 | <el-input |
| 19 | :model-value="entry.value" |
| 20 | :placeholder="valuePlaceholder" |
| 21 | class="kv-input" |
| 22 | @update:model-value="updateEntry(index, 'value', $event)" |
| 23 | /> |
| 24 | <button class="kv-remove-btn" @click="removeEntry(index)"> |
| 25 | <svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 26 | <path |
| 27 | d="M5.5 5.5A.5.5 0 016 6v6a.5.5 0 01-1 0V6a.5.5 0 01.5-.5zm2.5 0a.5.5 0 01.5.5v6a.5.5 0 01-1 0V6a.5.5 0 01.5-.5zm3 .5a.5.5 0 00-1 0v6a.5.5 0 001 0V6z" |
| 28 | fill="currentColor" |
| 29 | /> |
| 30 | <path |
| 31 | fill-rule="evenodd" |
| 32 | clip-rule="evenodd" |
| 33 | d="M14.5 3a1 1 0 01-1 1H13v9a2 2 0 01-2 2H5a2 2 0 01-2-2V4h-.5a1 1 0 010-2H6a1 1 0 011-1h2a1 1 0 011 1h3.5a1 1 0 011 1zM4.118 4L4 4.059V13a1 1 0 001 1h6a1 1 0 001-1V4.059L11.882 4H4.118zM6 2h4v1H6V2z" |
| 34 | fill="currentColor" |
| 35 | /> |
| 36 | </svg> |
| 37 | </button> |
| 38 | </div> |
| 39 | <button class="kv-add-btn" @click="addEntry"> |
| 40 | <svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 41 | <path |
| 42 | d="M8 2a.5.5 0 01.5.5v5h5a.5.5 0 010 1h-5v5a.5.5 0 01-1 0v-5h-5a.5.5 0 010-1h5v-5A.5.5 0 018 2z" |
| 43 | fill="currentColor" |
| 44 | /> |
| 45 | </svg> |
| 46 | Add |
| 47 | </button> |
| 48 | </template> |
| 49 | </div> |
| 50 | </template> |
| 51 |
| 52 | <script setup lang="ts"> |
| 53 | interface KVEntry { |
| 54 | key: string |
| 55 | value: string |
| 56 | } |
| 57 |
| 58 | interface Props { |
| 59 | modelValue: KVEntry[] |
| 60 | keyPlaceholder?: string |
| 61 | valuePlaceholder?: string |
| 62 | readonly?: boolean |
| 63 | } |
| 64 |
| 65 | const props = withDefaults(defineProps<Props>(), { |
| 66 | keyPlaceholder: 'Key', |
| 67 | valuePlaceholder: 'Value', |
| 68 | readonly: false, |
| 69 | }) |
| 70 |
| 71 | const emit = defineEmits<{ |
| 72 | 'update:modelValue': [value: KVEntry[]] |
| 73 | }>() |
| 74 |
| 75 | const updateEntry = (index: number, field: 'key' | 'value', val: string) => { |
| 76 | const updated = [...props.modelValue] |
| 77 | updated[index] = { ...updated[index], [field]: val } |
| 78 | emit('update:modelValue', updated) |
| 79 | } |
| 80 |
| 81 | const addEntry = () => { |
| 82 | emit('update:modelValue', [...props.modelValue, { key: '', value: '' }]) |
| 83 | } |
| 84 |
| 85 | const removeEntry = (index: number) => { |
| 86 | const updated = props.modelValue.filter((_, i) => i !== index) |
| 87 | emit('update:modelValue', updated) |
| 88 | } |
| 89 | </script> |
| 90 |
| 91 | <style scoped> |
| 92 | .kv-editor { |
| 93 | display: flex; |
| 94 | flex-direction: column; |
| 95 | gap: 8px; |
| 96 | width: 100%; |
| 97 | } |
| 98 |
| 99 | .kv-row { |
| 100 | display: flex; |
| 101 | gap: 8px; |
| 102 | align-items: center; |
| 103 | } |
| 104 |
| 105 | .kv-input { |
| 106 | flex: 1; |
| 107 | } |
| 108 |
| 109 | .kv-remove-btn { |
| 110 | display: flex; |
| 111 | align-items: center; |
| 112 | justify-content: center; |
| 113 | width: 32px; |
| 114 | height: 32px; |
| 115 | border: none; |
| 116 | border-radius: 8px; |
| 117 | background: var(--el-fill-color); |
| 118 | color: var(--el-text-color-secondary); |
| 119 | cursor: pointer; |
| 120 | transition: all 0.15s ease; |
| 121 | flex-shrink: 0; |
| 122 | } |
| 123 |
| 124 | .kv-remove-btn svg { |
| 125 | width: 14px; |
| 126 | height: 14px; |
| 127 | } |
| 128 |
| 129 | .kv-remove-btn:hover { |
| 130 | background: rgba(239, 68, 68, 0.1); |
| 131 | color: #ef4444; |
| 132 | } |
| 133 |
| 134 | html.dark .kv-remove-btn:hover { |
| 135 | background: rgba(248, 113, 113, 0.15); |
| 136 | color: #f87171; |
| 137 | } |
| 138 |
| 139 | .kv-add-btn { |
| 140 | display: inline-flex; |
| 141 | align-items: center; |
| 142 | gap: 6px; |
| 143 | padding: 5px 12px; |
| 144 | border: 1px solid var(--color-border); |
| 145 | border-radius: 6px; |
| 146 | background: transparent; |
| 147 | color: var(--color-text-secondary); |
| 148 | font-size: 13px; |
| 149 | cursor: pointer; |
| 150 | transition: all 0.15s; |
| 151 | align-self: flex-start; |
| 152 | } |
| 153 |
| 154 | .kv-add-btn svg { |
| 155 | width: 13px; |
| 156 | height: 13px; |
| 157 | } |
| 158 |
| 159 | .kv-add-btn:hover { |
| 160 | background: var(--color-bg-hover); |
| 161 | } |
| 162 |
| 163 | .kv-empty { |
| 164 | color: var(--el-text-color-secondary); |
| 165 | font-size: 13px; |
| 166 | } |
| 167 |
| 168 | .kv-readonly-row { |
| 169 | display: flex; |
| 170 | gap: 8px; |
| 171 | padding: 4px 0; |
| 172 | font-size: 13px; |
| 173 | } |
| 174 |
| 175 | .kv-readonly-key { |
| 176 | color: var(--el-text-color-secondary); |
| 177 | min-width: 100px; |
| 178 | } |
| 179 |
| 180 | .kv-readonly-value { |
| 181 | color: var(--el-text-color-primary); |
| 182 | font-family: ui-monospace, SFMono-Regular, Menlo, monospace; |
| 183 | } |
| 184 | </style> |