9595 :prop =" key"
9696 >
9797 <!-- 分情况讨论 -->
98+ <!-- number property -->
9899 <el-input-number
99100 v-if =" schema.properties[key].type === 'integer' || schema.properties[key].type === 'number'"
100101 v-model =" data[key]"
104105 @change =" onPropertyChange(key, $event)"
105106 />
106107
108+ <!-- enum property -->
107109 <el-select
108110 v-if =" schema.properties[key].hasOwnProperty('enum')"
109111 v-model =" data[key]"
110112 :clearable =" true"
111- :placeholder =" & quot ; Select a & quot ; + key"
113+ :placeholder =" ` Select a ${ key}` "
112114 @change =" onPropertyChange(key, $event)"
113115 >
114116 <el-option
119121 />
120122 </el-select >
121123
124+ <!-- string property -->
122125 <el-input
123126 v-if =" schema.properties[key].type === 'string' && !schema.properties[key].hasOwnProperty('enum')"
124127 v-model =" data[key]"
125128 :placeholder =" key"
126129 @input =" onPropertyChange(key, $event)"
127130 />
128131
132+ <!-- boolean property -->
129133 <el-switch
130134 v-if =" schema.properties[key].type === 'boolean' && !schema.properties[key].hasOwnProperty('enum')"
131135 v-model =" data[key]"
132136 active-color =" #13ce66"
133137 inactive-color =" #ff4949"
134138 />
139+
140+ <!-- array property -->
141+ <div
142+ v-if =" schema.properties[key].type === 'array'"
143+ class =" array-input-container"
144+ >
145+ <el-input
146+ v-for =" (arrayIndex) in arrayPropertiesLength[key]"
147+ :key =" arrayIndex"
148+ v-model =" data[key][arrayIndex]"
149+ :placeholder =" `${key} [${arrayIndex}]`"
150+ @input =" isDataChanged = true"
151+ />
152+
153+ <el-button
154+ @click =" addArrayItem(key)"
155+ >
156+ {{ $t('button.addValue') }}
157+ </el-button >
158+ </div >
135159 </el-form-item >
136160 </el-form >
137161 <span
141165 <el-button
142166 @click =" onCancel"
143167 >
144- Cancel
168+ {{ $t('button.cancel') }}
145169 </el-button >
146170 <el-button
147171 type =" primary"
148172 :disabled =" !isDataChanged && oneOfPropHasEmptyValue"
149173 @click =" onSave"
150174 >
151- Confirm
175+ {{ $t('button.confirm') }}
152176 </el-button >
153177 </span >
154178 </el-dialog >
@@ -177,6 +201,7 @@ export default class extends Vue {
177201 private data: any = {}
178202 private isDataChanged: boolean = false
179203 private showDialog: boolean = false
204+ private arrayPropertiesLength = {}
180205
181206 @Watch (' show' )
182207 private onShowChange(value : boolean ) {
@@ -242,8 +267,34 @@ export default class extends Vue {
242267
243268 this .rules = rules
244269
270+ // Generate initial data and merge current data
271+ let schemaKeys = {}
272+ for (let key in schema .properties ) {
273+ if (schema .properties [key ].default ) {
274+ schemaKeys [key ] = schema .properties [key ].default
275+ continue
276+ }
277+
278+ switch (schema .properties [key ].type ) {
279+ case ' array' :
280+ schemaKeys [key ] = []
281+ this .arrayPropertiesLength [key ] = [... new Array (this .pluginData [key ] ? this .pluginData [key ].length : schema .properties [key ].minItems ).keys ()]
282+ break
283+ case ' object' :
284+ schemaKeys [key ] = {}
285+ break
286+ case ' boolean' :
287+ schemaKeys [key ] = false
288+ break
289+ default :
290+ schemaKeys [key ] = ' '
291+ }
292+ }
293+
245294 if (this .pluginData ) {
246- this .data = Object .assign ({}, this .pluginData )
295+ this .data = Object .assign (schemaKeys , this .pluginData )
296+ } else {
297+ this .data = schemaKeys
247298 }
248299
249300 if (this .name === ' key-auth' && ! this .pluginData ) {
@@ -284,13 +335,26 @@ export default class extends Vue {
284335 if (valid ) {
285336 this .data = this .processOneOfProp (this .data )
286337 this .$emit (' save' , this .name , this .data )
287- this .$message .warning (' Your data will be saved after you click the Save button ' )
338+ this .$message .warning (` ${ this . $t ( ' message.clickSaveButton ' )} ` )
288339 } else {
289340 return false
290341 }
291342 })
292343 }
293344
345+ /**
346+ * Add item to array property
347+ * @param key
348+ */
349+ private addArrayItem(key : any ) {
350+ if (this .arrayPropertiesLength [key ].length < this .schema .properties [key ].maxItems ) {
351+ this .arrayPropertiesLength [key ].push (this .arrayPropertiesLength [key ].length )
352+ this .$forceUpdate ()
353+ } else {
354+ this .$message .warning (` ${this .$t (' message.cannotAddMoreItems' )} ` )
355+ }
356+ }
357+
294358 private onPropertyChange(key : any , value : any ) {
295359 this .data [key ] = value
296360 this .isDataChanged = true
@@ -314,6 +378,15 @@ export default class extends Vue {
314378
315379 private processOneOfProp(data : any ) {
316380 if (! this .schema .oneOf ) {
381+ // remove empty field
382+ for (let key in data ) {
383+ if (data [key ] === ' ' ) {
384+ delete data [key ]
385+ }
386+ if (typeof data [key ] === ' object' && Object .keys (data [key ]).length === 0 ) {
387+ delete data [key ]
388+ }
389+ }
317390 return data
318391 }
319392
@@ -341,5 +414,10 @@ export default class extends Vue {
341414 margin-left : 10px ;
342415 }
343416 }
417+
418+ .array-input-container > * {
419+ display : flex ;
420+ margin-top : 5px ;
421+ }
344422}
345423 </style >
0 commit comments