From ed164c889a3681e545645765147cd375ee1275a6 Mon Sep 17 00:00:00 2001 From: jedi Date: Mon, 23 Dec 2019 21:09:30 +0100 Subject: [PATCH] refactor InputCombo --- src/components/EditItem.vue | 9 ++--- src/components/inputs/InputCombo.vue | 50 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/components/EditItem.vue b/src/components/EditItem.vue index 5d8de32..0f56151 100644 --- a/src/components/EditItem.vue +++ b/src/components/EditItem.vue @@ -10,13 +10,14 @@ label="description" :model="item" field="description" - :validation-fn="str => str.length > 0" + :validation-fn="str => str && str.length > 0" /> @@ -34,7 +35,7 @@ export default { computed: { ...mapGetters(['getBoxes']), boxes({ getBoxes }) { - return getBoxes.map(box => box.name); + return getBoxes.map(obj => ({cid: obj.cid, box: obj.name})); } }, methods: { diff --git a/src/components/inputs/InputCombo.vue b/src/components/inputs/InputCombo.vue index cab87bc..5890e85 100644 --- a/src/components/inputs/InputCombo.vue +++ b/src/components/inputs/InputCombo.vue @@ -11,22 +11,25 @@ - - + +
+ +
@@ -38,27 +41,34 @@ import Addon from './Addon'; export default { name: 'InputCombo', components: {Addon}, - props: [ 'label', 'model', 'field', 'options', 'onOptionAdd' ], - data: ({ model, field }) => ({ - internalValue: model[field], + props: [ 'label', 'model', 'nameKey', 'uniqueKey', 'options', 'onOptionAdd' ], + data: ({ options, model, nameKey, uniqueKey }) => ({ + internalName: model[nameKey], + selectedOption: options.filter(e => e[uniqueKey] === model[uniqueKey])[0], addingOption: false }), computed: { - isValid: ({ internalValue, options }) => options.includes(internalValue) + isValid: ({options, nameKey, internalName}) => options.some(e => e[nameKey] === internalName) }, watch: { - internalValue(newValue, oldValue) { + internalName(newValue, oldValue) { + if (this.isValid) { + if(newValue!=this.selectedOption[this.nameKey]){ + this.selectedOption = this.options.filter(e => e[this.nameKey] === this.internalName)[0]; + } + this.model[this.nameKey] = this.selectedOption[this.nameKey]; + this.model[this.uniqueKey] = this.selectedOption[this.uniqueKey]; + } console.log(oldValue, newValue, this.isValid); - if (this.isValid) - this.model[this.field] = newValue; } }, methods: { - setInternalValue(value) { - this.internalValue = value; + setInternalValue(option) { + this.selectedOption = option; + this.internalName = option[this.nameKey]; }, addOption() { - this.onOptionAdd(this.internalValue); + this.onOptionAdd({[this.nameKey]: this.internalName}); } } };