adrema/resources/js/components/FSelect.vue

115 lines
3.2 KiB
Vue
Raw Normal View History

2021-04-10 19:45:11 +02:00
<template>
2021-08-22 16:15:16 +02:00
<label class="field-wrap" :for="id" :class="`field-wrap-${size}`">
<span v-if="label" class="field-label">
{{ label }}
<span v-show="required" class="text-red-800">&nbsp;*</span>
</span>
<div class="real-field-wrap" :class="`size-${size}`">
<select :disabled="disabled" :value="value" @change="trigger">
<option v-if="placeholder" v-html="placeholder" :value="null"></option>
2021-04-10 19:45:11 +02:00
2021-08-22 16:15:16 +02:00
<option v-for="option in parsedOptions" :key="option.id"
v-html="option.name" :value="option.id"
></option>
</select>
2021-08-22 16:49:38 +02:00
<div class="info-wrap">
2021-08-22 16:15:16 +02:00
<div v-if="hint" v-tooltip="hint">
<sprite src="info-button" class="info-button"></sprite>
</div>
<div class="px-1 relative" v-if="size != 'xs'">
<sprite class="chevron w-3 h-3 fill-current" src="chevron-down"></sprite>
</div>
<div class="px-1 relative" v-if="size == 'xs'">
<sprite class="chevron w-2 h-2 fill-current" src="chevron-down"></sprite>
2021-04-10 19:45:11 +02:00
</div>
</div>
</div>
</label>
</template>
<script>
2021-08-22 16:15:16 +02:00
import mapValues from 'lodash/mapValues';
import keyBy from 'lodash/keyBy';
import map from 'lodash/map';
2021-04-10 19:45:11 +02:00
export default {
props: {
2021-08-22 16:15:16 +02:00
disabled: {
type: Boolean,
default: function() { return false; }
},
2021-04-10 19:45:11 +02:00
id: {},
inset: {
type: Boolean,
default: false
},
size: {
2021-08-22 16:15:16 +02:00
default: function() { return 'base'; }
2021-04-10 19:45:11 +02:00
},
emptyLabel: {
default: false,
type: Boolean
},
value: {
default: undefined
},
label: {
default: null
},
required: {
type: Boolean,
default: false
},
placeholder: {
default: '--kein--',
type: String
},
def: {
required: false,
type: Number,
default: -1
},
hint: {},
options: {
default: function() { return []; }
}
},
computed: {
parsedOptions() {
2021-08-22 16:15:16 +02:00
return Array.isArray(this.options)
? this.options
: map(this.options, (value, key) => {
return {'name': value, id: key};
});
2021-04-10 19:45:11 +02:00
}
},
methods: {
trigger(v) {
this.$emit('input', isNaN(parseInt(v.target.value))
? (v.target.value ? v.target.value : null)
: parseInt(v.target.value)
);
},
clear() {
this.$emit('input', null);
}
},
mounted() {
if (this.def !== -1 && typeof this.value === 'undefined') {
this.$emit('input', this.def);
return;
}
if (this.placeholder && typeof this.value === 'undefined') {
this.$emit('input', null);
}
}
};
</script>
<style scope>
.inset-bg {
background: linear-gradient(to bottom, hsl(247.5, 66.7%, 97.6%) 0%, hsl(247.5, 66.7%, 97.6%) 41%, hsl(0deg 0% 100%) 41%, hsl(180deg 0% 100%) 100%);
}
</style>