yunohost-admin/app/src/components/AdressInputSelect.vue

83 lines
2.4 KiB
Vue
Raw Normal View History

<template>
<b-input-group>
<b-input
2020-08-03 19:32:53 +02:00
:id="id" :placeholder="placeholder"
:state="state" :aria-describedby="feedbackId"
v-model="reactiveInput" @input="updateValue"
/>
<b-input-group-append>
2020-08-03 19:32:53 +02:00
<b-input-group-text>{{ separator }}</b-input-group-text>
</b-input-group-append>
<b-input-group-append>
2020-08-03 19:32:53 +02:00
<b-select v-model="reactiveOption" :options="options" @change="updateValue" />
</b-input-group-append>
</b-input-group>
</template>
<script>
export default {
2020-08-03 19:32:53 +02:00
name: 'AdressInputSelect',
props: {
2020-08-03 19:32:53 +02:00
// `value` is actually passed as the `v-model` directive
value: { type: String, required: true },
2020-08-03 19:32:53 +02:00
options: { type: Array, required: true },
separator: { type: String, default: '@' },
placeholder: { type: String, default: null },
id: { type: String, default: null },
state: { type: null, default: null },
2020-08-03 19:32:53 +02:00
feedbackId: { type: String, default: null },
defaultOption: { type: Number, default: 0 }
},
2020-08-03 19:32:53 +02:00
data () {
2020-08-03 19:32:53 +02:00
// static value that are updated by the computed setters.
return {
2020-08-03 19:32:53 +02:00
input: '',
option: ''
}
},
2020-08-03 19:32:53 +02:00
// Those 'reactive' properties allows two-way value binding. If input and option where
// only static properties, it would be impossible to detect that `value` has changed
// from outside this scope.
computed: {
reactiveInput: {
get () {
return this.value.split(this.separator)[0]
},
set (value) {
// FIXME, ugly hack since the `reactiveOption` v-model isn't set when `value` change
if (this.reactiveOption !== this.option) {
this.option = this.reactiveOption
}
2020-08-03 19:32:53 +02:00
this.input = value
}
},
reactiveOption: {
get () {
return this.value.split(this.separator)[1] || this.options[this.defaultOption]
2020-08-03 19:32:53 +02:00
},
set (value) {
// FIXME, ugly hack since the `reactiveInput` v-model isn't set when `value` change
if (this.reactiveInput !== this.input) {
this.input = this.reactiveInput
}
this.option = value
}
}
},
2020-08-03 19:32:53 +02:00
methods: {
2020-08-03 19:32:53 +02:00
// Emit an input event of the concatened data.
// Since the consumer of this component will use the `v-model` directive, this event
// will automaticly update the value of the given variable.
updateValue () {
2020-08-03 19:32:53 +02:00
this.$emit('input', this.input + this.separator + this.option)
2020-07-27 21:25:41 +02:00
}
}
}
</script>