Files
portal/resources/js/components/Form/SelectorField.vue
2020-09-12 08:52:20 +02:00

63 lines
1.7 KiB
Vue

<template>
<div class="relative mb-2">
<label v-if="label" :for="name" class="pb-2 font-bold text-xl ml-1">{{ label }}</label>
<select :name="name"
v-model="selected"
:class="'block w-full p-2 ' + errorClassObject()">
<option v-for="option in options"
:key="option.value"
:value="option.value">{{ option.label }}</option>
</select>
<p class="text-red no-indent m-0" v-text="errorMessage()">Error Here</p>
</div>
</template>
<script>
export default {
name: 'SelectorField',
props: {
classes: String,
label: String,
name: {
type: String,
required: true,
},
options: Array,
selected: Number,
required: {
type: Boolean,
default: false
},
errors: Object,
},
computed: {
hasError: function () {
return this.required && this.errors && this.errors[this.name] && this.errors[this.name].length > 0
}
},
methods: {
errorMessage: function () {
if (this.hasError) {
return this.errors[this.name][0]
}
},
clearErrors: function () {
if (this.hasError) {
this.errors[this.name] = null
}
},
errorClassObject: function () {
if (this.hasError) {
return 'border-red'
}
}
},
watch: {
selected: function () {
this.clearErrors(this.name)
this.$emit('update:field', this.selected)
}
}
}
</script>