1
+ <template >
2
+
3
+ <span :style =" { color: isAddMode ? '#000' : 'gray', 'margin-right': '8px' }" @click =" toggleMode('add')" >添加键</span >
4
+ <span :style =" { color: !isAddMode ? '#000' : 'gray' }" @click =" toggleMode('delete')" >删除键</span >
5
+ <div style =" display : flex ; margin-top : 8px ; gap : 16px ; align-items :center " >
6
+ <!-- {{ items }} -->
7
+ <v-text-field v-model =" newItemKey" label =" 键名" style =" max-width : 250px ;" density =" compact"
8
+ prepend-inner-icon =" mdi-alpha-k" variant =" solo-filled" flat hide-details single-line
9
+ rounded =" md" ></v-text-field >
10
+ <v-select v-if =" isAddMode" hint =" 类型" style =" max-width : 150px ;" rounded =" md" density =" compact" v-model =" newItemType" :items =" typeOptions"
11
+ prepend-inner-icon =" mdi-alpha-t" variant =" solo-filled" flat hide-details single-line >
12
+ </v-select >
13
+ <v-btn @click =" apply" variant =" tonal" >
14
+ 确定
15
+ </v-btn >
16
+ <small color =" error" >{{ error }}</small >
17
+ </div >
18
+ </template >
19
+
20
+ <script >
21
+ export default {
22
+ name: ' ObjectConfigItem' ,
23
+ props: {
24
+ object: {
25
+ type: Object ,
26
+ default : () => ({}),
27
+ },
28
+ metadata: {
29
+ type: Object ,
30
+ default : () => ({}),
31
+ },
32
+ },
33
+ data () {
34
+ return {
35
+ items: this .object ,
36
+ metadata: this .metadata ,
37
+ newItemKey: ' ' ,
38
+ newItemType: ' string' ,
39
+ typeOptions: [
40
+ ' string' ,
41
+ ' int' ,
42
+ ' float' ,
43
+ ' text' ,
44
+ ' bool' ,
45
+ ' list' ,
46
+ ' object' ,
47
+ ],
48
+ error: ' ' ,
49
+ isAddMode: true , // 默认模式为添加键
50
+
51
+ defaultValues: {
52
+ string: ' ' ,
53
+ int: 0 ,
54
+ float: 0.0 ,
55
+ text: ' ' ,
56
+ bool: false ,
57
+ list: [],
58
+ object: {},
59
+ },
60
+ };
61
+ },
62
+ methods: {
63
+ toggleMode (mode ) {
64
+ this .isAddMode = (mode === ' add' );
65
+ this .newItemKey = ' ' ;
66
+ this .error = ' ' ;
67
+ },
68
+ apply () {
69
+ if (this .newItemKey === ' ' ) {
70
+ this .error = ' 键不能为空' ;
71
+ return ;
72
+ }
73
+ if (this .isAddMode ) {
74
+ if (this .items [this .newItemKey ]) {
75
+ this .error = ' 键已存在' ;
76
+ return ;
77
+ }
78
+ if (! this .newItemType ) {
79
+ this .error = ' 请选择类型' ;
80
+ return ;
81
+ }
82
+ this .items [this .newItemKey ] = this .defaultValues [this .newItemType ];
83
+ this .metadata [this .newItemKey ] = {
84
+ type: this .newItemType ,
85
+ description: this .newItemKey ,
86
+ };
87
+ this .newItemType = ' string' ;
88
+ } else {
89
+ delete this .items [this .newItemKey ];
90
+ delete this .metadata [this .newItemKey ];
91
+ }
92
+ this .newItemKey = ' ' ;
93
+ this .error = ' ' ;
94
+ },
95
+ },
96
+ };
97
+ </script >
98
+
99
+ <style ></style >
0 commit comments