|
1 | 1 | <template> |
2 | 2 | <div class="form-item"> |
3 | | - <input-label :label="label" :has-description="hasDescription"></input-label> |
| 3 | + <input-label :label="label" :hasDescription="hasDescription"></input-label> |
4 | 4 |
|
5 | 5 | <div class="form-item__value"> |
6 | | - <div class="input-option__field"> |
7 | | - <select :id="field" v-model="element" class="form-item__input" :disabled="!availableEnumValues.length"> |
8 | | - <option v-for="(enumValue, name) in enumValues" v-show="!value.includes(enumValue)" :key="name" :value="enumValue"> |
9 | | - {{ name }} |
10 | | - </option> |
11 | | - <option v-if="!availableEnumValues.length" :value="undefined" disabled> |
12 | | - {{ $t('input-all-selected') }} |
13 | | - </option> |
14 | | - </select> |
15 | | - |
16 | | - <button class="button" @click.prevent="addElement"> |
17 | | - {{ $t('add') }} |
18 | | - </button> |
19 | | - </div> |
| 6 | + <select :id="field" v-model="selectedElement" class="form-item__input" :disabled="!availableEnumValues.length" @change="addElement($event.target.value)"> |
| 7 | + <option :value="null" disabled selected hidden>{{ $t('input-select-enum-value') }}</option> |
| 8 | + <option v-for="(enumValue, name) in enumValues" v-show="!value.includes(enumValue)" :key="name" :value="enumValue"> |
| 9 | + {{ name }} |
| 10 | + </option> |
| 11 | + <option v-if="!availableEnumValues.length" :value="undefined" disabled> |
| 12 | + {{ $t('input-all-selected') }} |
| 13 | + </option> |
| 14 | + </select> |
20 | 15 |
|
21 | 16 | <div class="input-option__items"> |
22 | | - <button v-for="(item, index) in value" :key="index" class="button input-option__item" @click.prevent="removeElement(index)"> |
| 17 | + <button v-for="(item, index) in value" :key="index" class="button input-option__item" @click.prevent="removeElement(item)"> |
23 | 18 | {{ resolveOption(item) }} |
24 | 19 | </button> |
25 | 20 | </div> |
|
37 | 32 | mixins: [Input], |
38 | 33 | data() { |
39 | 34 | return { |
40 | | - element: null, |
| 35 | + selectedElement: null, |
41 | 36 | }; |
42 | 37 | }, |
43 | 38 | computed: { |
|
57 | 52 | }, |
58 | 53 | created() { |
59 | 54 | this.value.sort(); |
60 | | - this.element = this.getDefaultElement(); |
61 | 55 | }, |
62 | 56 | methods: { |
63 | | - getDefaultElement() { |
64 | | - return this.availableEnumValues[0]; |
65 | | - }, |
66 | | - addElement() { |
67 | | - if (!this.element && this.element !== 0) return; |
68 | | - if (this.value.includes(this.element)) return; |
| 57 | + addElement(input) { |
| 58 | + const parsedInput = typeof (input) !== (typeof (0)) ? parseInt(input, 10) : input; |
| 59 | +
|
| 60 | + if (this.value.includes(parsedInput)) return; |
69 | 61 |
|
70 | | - this.value.push(this.element); |
| 62 | + this.value.push(parsedInput); |
71 | 63 | this.value.sort(); |
72 | 64 |
|
73 | | - this.element = this.getDefaultElement(); |
| 65 | + this.selectedElement = null; |
74 | 66 | }, |
75 | | - removeElement(index) { |
76 | | - this.value.splice(index, 1); |
77 | | - this.element = this.getDefaultElement(); |
| 67 | + removeElement(input) { |
| 68 | + if (typeof (input) !== (typeof (0))) { |
| 69 | + input = parseInt(input); |
| 70 | + } |
| 71 | +
|
| 72 | + this.value = this.value.filter(item => item !== input); |
78 | 73 | }, |
79 | 74 | resolveOption(value) { |
80 | 75 | return Object.keys(this.enumValues).find(key => this.enumValues[key] === value); |
|
0 commit comments