87 lines
1.8 KiB
Vue
87 lines
1.8 KiB
Vue
<template>
|
|
<div class="kv-editor">
|
|
<div v-for="(item, idx) in list" :key="idx" class="kv-row">
|
|
<el-input v-model="item.key" placeholder="key" size="small" class="kv-key" @input="sync" />
|
|
<el-input v-model="item.value" placeholder="value" size="small" class="kv-value" @input="sync" />
|
|
<el-button size="small" text type="danger" class="kv-del" @click="remove(idx)">
|
|
<el-icon><Delete /></el-icon>
|
|
</el-button>
|
|
</div>
|
|
<el-button size="small" class="kv-add" @click="add">
|
|
<el-icon><Plus /></el-icon> 添加
|
|
</el-button>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, watch } from 'vue';
|
|
import { Plus, Delete } from '@element-plus/icons-vue';
|
|
|
|
const props = defineProps<{ modelValue: Record<string, unknown> }>();
|
|
const emit = defineEmits<{ 'update:modelValue': [Record<string, unknown>] }>();
|
|
|
|
const list = ref<{ key: string; value: string }[]>([]);
|
|
|
|
function toList(obj: Record<string, unknown>) {
|
|
return Object.entries(obj || {}).map(([k, v]) => ({
|
|
key: k,
|
|
value: typeof v === 'string' ? v : JSON.stringify(v),
|
|
}));
|
|
}
|
|
|
|
function toRecord() {
|
|
const obj: Record<string, unknown> = {};
|
|
for (const item of list.value) {
|
|
if (item.key.trim()) obj[item.key.trim()] = item.value;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
function sync() {
|
|
emit('update:modelValue', toRecord());
|
|
}
|
|
|
|
function add() {
|
|
list.value.push({ key: '', value: '' });
|
|
}
|
|
|
|
function remove(idx: number) {
|
|
list.value.splice(idx, 1);
|
|
sync();
|
|
}
|
|
|
|
watch(
|
|
() => props.modelValue,
|
|
(val) => {
|
|
list.value = toList(val);
|
|
},
|
|
{ immediate: true, deep: true }
|
|
);
|
|
</script>
|
|
|
|
<style scoped>
|
|
.kv-editor {
|
|
width: 100%;
|
|
}
|
|
.kv-row {
|
|
display: flex;
|
|
gap: 6px;
|
|
margin-bottom: 6px;
|
|
align-items: center;
|
|
}
|
|
.kv-key {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
.kv-value {
|
|
flex: 2;
|
|
min-width: 0;
|
|
}
|
|
.kv-del {
|
|
flex-shrink: 0;
|
|
}
|
|
.kv-add {
|
|
font-size: 12px;
|
|
}
|
|
</style>
|