-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathInputDictionary.vue
More file actions
75 lines (67 loc) · 2.36 KB
/
InputDictionary.vue
File metadata and controls
75 lines (67 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<div class="form-item">
<input-label :label="label" :has-description="hasDescription"></input-label>
<div class="form-item__value">
<div class="input-option__field input-option__field--three">
<input :id="`${field}-key`" v-model="elementKey" class="form-item__input" type="text" @keydown.enter="addElement">
<select :id="`${field}-value`" v-model="elementValue" class="form-item__input">
<option v-for="{ label, value } in availableEnumValues" :value="value">{{ label }}</option>
</select>
<button class="button" @click.prevent="addElement">{{ $t('add') }}</button>
</div>
<div class="input-option__items">
<button v-for="(keyValue, key) in value" class="button input-option__item" @click.prevent="removeElement(key)">{{ key }} => {{ resolveValue(keyValue) }}</button>
</div>
</div>
<input-description v-if="hasDescription" v-show="showDescription" :description="description"></input-description>
</div>
</template>
<script>
import Input from './Input.vue';
export default {
name: 'input-dictionary',
mixins: [Input],
data() {
return {
elementKey: null,
elementValue: null,
};
},
computed: {
availableEnumValues() {
return this.enumValues;
},
enumValues() {
return Object.entries(this.schema.additionalProperties['x-definition']).map(([label, value]) => ({ label, value }));
},
resolveValue() {
return value => {
const enumValue = this.enumValues.find(({ value: enumValue }) => value === enumValue);
if (!enumValue) return value;
return enumValue.label;
};
},
},
created() {
this.elementKey = this.getDefaultKey();
this.elementValue = this.getDefaultValue();
},
methods: {
getDefaultKey() {
return null;
},
getDefaultValue() {
return this.availableEnumValues[0].value;
},
addElement() {
if ((!this.elementValue && this.elementValue !== 0) || (!this.elementKey && this.elementKey !== 0)) return;
this.$set(this.value, this.elementKey, this.elementValue);
this.elementValue = this.getDefaultValue();
this.elementKey = this.getDefaultKey();
},
removeElement(key) {
this.$delete(this.value, key);
},
},
};
</script>