2020-07-27 18:22:34 +02:00
|
|
|
<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"
|
2020-07-27 18:22:34 +02:00
|
|
|
/>
|
|
|
|
|
|
|
|
<b-input-group-append>
|
2020-08-03 19:32:53 +02:00
|
|
|
<b-input-group-text>{{ separator }}</b-input-group-text>
|
2020-07-27 18:22:34 +02:00
|
|
|
</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" />
|
2020-07-27 18:22:34 +02:00
|
|
|
</b-input-group-append>
|
|
|
|
</b-input-group>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
2020-08-03 19:32:53 +02:00
|
|
|
name: 'AdressInputSelect',
|
|
|
|
|
2020-07-27 18:22:34 +02:00
|
|
|
props: {
|
2020-08-03 19:32:53 +02:00
|
|
|
// `value` is actually passed as the `v-model` directive
|
2020-07-27 18:22:34 +02:00
|
|
|
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 },
|
2020-07-27 18:22:34 +02:00
|
|
|
id: { type: String, default: null },
|
|
|
|
state: { type: null, default: null },
|
2020-08-03 19:32:53 +02:00
|
|
|
feedbackId: { type: String, default: null },
|
2020-08-03 20:53:53 +02:00
|
|
|
defaultOption: { type: Number, default: 0 }
|
2020-07-27 18:22:34 +02:00
|
|
|
},
|
2020-08-03 19:32:53 +02:00
|
|
|
|
2020-07-27 18:22:34 +02:00
|
|
|
data () {
|
2020-08-03 19:32:53 +02:00
|
|
|
// static value that are updated by the computed setters.
|
2020-07-27 18:22:34 +02:00
|
|
|
return {
|
2020-08-03 19:32:53 +02:00
|
|
|
input: '',
|
|
|
|
option: ''
|
2020-07-27 18:22:34 +02:00
|
|
|
}
|
|
|
|
},
|
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) {
|
2020-08-03 20:53:53 +02:00
|
|
|
// 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 () {
|
2020-08-03 20:53:53 +02:00
|
|
|
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-07-27 18:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-08-03 19:32:53 +02:00
|
|
|
|
2020-07-27 18:22:34 +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.
|
2020-07-27 18:22:34 +02:00
|
|
|
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
|
|
|
}
|
2020-07-27 18:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|