gancio-upstream/pages/add/WhereInput.vue

91 lines
2.5 KiB
Vue
Raw Normal View History

2020-10-25 00:31:38 +02:00
<template lang="pug">
v-row
v-combobox.col-md-6(ref='place'
:rules="[$validators.required('common.where')]"
:label="$t('common.where')"
:hint="$t('event.where_description')"
:search-input.sync="placeName"
2020-11-04 10:55:30 +01:00
prepend-icon='mdi-map-marker'
2020-10-25 00:31:38 +02:00
persistent-hint
2020-10-28 01:30:37 +01:00
:value="value.name"
:items="filteredPlaces"
no-filter
2020-10-25 00:31:38 +02:00
item-text='name'
@change='selectPlace')
template(v-slot:item="{ item }")
v-list-item-content(two-line v-if='item.create')
v-list-item-title <v-icon color='primary'>mdi-plus</v-icon> {{item.name}}
v-list-item-content(two-line v-else)
v-list-item-title {{item.name}}
v-list-item-subtitle {{item.address}}
2020-10-25 00:31:38 +02:00
v-text-field.col-md-6(ref='address'
2020-11-04 10:55:30 +01:00
prepend-icon='mdi-map'
2020-10-25 00:31:38 +02:00
:disabled='disableAddress'
:rules="[$validators.required('common.address')]"
:label="$t('common.address')"
@change="changeAddress"
2020-10-27 11:56:47 +01:00
:value="value.address")
2020-10-25 00:31:38 +02:00
</template>
<script>
import { mapState } from 'vuex'
export default {
name: 'WhereInput',
2020-10-27 11:56:47 +01:00
props: {
value: { type: Object, default: () => {} }
},
2020-10-25 00:31:38 +02:00
data () {
return {
place: { },
placeName: '',
2020-10-25 00:31:38 +02:00
disableAddress: true
}
},
computed: {
...mapState(['places']),
filteredPlaces () {
if (!this.placeName) { return this.places }
const placeName = this.placeName.toLowerCase()
let nameMatch = false
const matches = this.places.filter(p => {
const tmpName = p.name.toLowerCase()
const tmpAddress = p.address.toLowerCase()
if (tmpName.includes(placeName)) {
if (tmpName === placeName) { nameMatch = true }
return true
}
if (tmpAddress.includes(placeName)) { return true }
return false
})
if (!nameMatch) {
matches.unshift({ create: true, name: this.placeName })
}
return matches
}
2020-10-25 00:31:38 +02:00
},
methods: {
selectPlace (p) {
if (!p) { return }
if (typeof p === 'object' && !p.create) {
2020-10-25 00:31:38 +02:00
this.place.name = p.name
2020-11-04 10:55:30 +01:00
this.place.address = p.address
2020-10-25 00:31:38 +02:00
this.disableAddress = true
2020-11-04 10:55:30 +01:00
} else { // this is a new place
this.place.name = p.name || p
this.place.address = ''
2020-10-25 00:31:38 +02:00
this.disableAddress = false
this.$refs.place.blur()
this.$refs.address.focus()
}
2020-11-04 10:55:30 +01:00
this.$emit('input', { ...this.place })
2020-10-25 00:31:38 +02:00
},
changeAddress (v) {
this.place.address = v
2020-11-04 10:55:30 +01:00
this.$emit('input', { ...this.place })
2020-10-25 00:31:38 +02:00
}
}
}
</script>