adrema/resources/js/components/FSwitch.vue

134 lines
3.9 KiB
Vue
Raw Normal View History

2021-04-10 19:45:11 +02:00
<template>
2023-05-08 15:11:16 +02:00
<label
class="flex relative field-switch cursor-pointer"
:for="id"
:class="{
'items-center flex-row-reverse space-x-3 space-x-reverse justify-end': inline,
'flex-col': !inline,
[sizes[size].wrap]: true,
}"
>
2022-05-20 01:18:11 +02:00
<span
v-if="label"
class="font-semibold leading-none text-gray-400"
:class="{
'text-xs': size == 'sm',
2022-12-02 00:42:06 +01:00
'text-sm': size === 'base',
2022-05-20 01:18:11 +02:00
}"
>{{ label }}</span
>
2023-05-08 15:11:16 +02:00
<div class="relative flex items-center inner-field mt-1" :class="`h-field-${fieldSize}`">
<span>
<input :id="id" type="checkbox" :name="name" :value="value" v-model="v" :disabled="disabled" class="absolute peer" @keypress="$emit('keypress', $event)" />
<span class="relative cursor-pointer peer-focus:bg-red-500 flex grow display" :class="{'bg-switch': v === true, 'bg-gray-700': v === false}">
<span><svg-sprite class="relative text-gray-400 flex-none" :class="{'w-2 h-2': size === 'sm' || size == 'xs', 'w-4 h-4': size === 'base'}" src="check"></svg-sprite></span>
<span><svg-sprite class="relative text-gray-400 flex-none" :class="{'w-2 h-2': size === 'sm' || size == 'xs', 'w-4 h-4': size === 'base'}" src="close"></svg-sprite></span>
<var class="absolute overlay bg-gray-400 rounded top-0"></var>
</span>
2021-04-10 19:45:11 +02:00
</span>
2023-05-08 15:11:16 +02:00
<div v-if="hint" class="ml-2 info-wrap">
<div v-tooltip="hint">
<svg-sprite src="info-button" class="info-button w-4 h-4 text-primary-700"></svg-sprite>
</div>
</div>
2021-04-10 19:45:11 +02:00
</div>
</label>
</template>
<script>
export default {
2022-12-02 00:42:06 +01:00
data: function () {
return {
sizes: {
sm: {
wrap: 'field-wrap-sm',
field: 'size-sm',
},
base: {
wrap: 'field-wrap-base',
field: 'size-base',
},
lg: {
wrap: 'field-wrap-lg',
field: 'size-lg',
},
},
};
},
2021-04-10 19:45:11 +02:00
model: {
prop: 'items',
2022-05-20 01:18:11 +02:00
event: 'input',
2021-04-10 19:45:11 +02:00
},
props: {
2023-05-08 15:11:16 +02:00
hint: {
default: null,
},
2023-02-25 17:19:17 +01:00
inline: {
2022-05-20 01:18:11 +02:00
default: false,
2023-02-25 17:19:17 +01:00
type: Boolean,
2021-04-10 19:45:11 +02:00
},
size: {
2022-12-02 00:42:06 +01:00
default: 'base',
2022-05-20 01:18:11 +02:00
required: false,
2021-04-10 19:45:11 +02:00
},
id: {
2022-05-20 01:18:11 +02:00
required: true,
},
name: {
default: '',
2021-04-10 19:45:11 +02:00
},
disabled: {
type: Boolean,
2022-05-20 01:18:11 +02:00
default: false,
2021-04-10 19:45:11 +02:00
},
value: {
2022-05-20 01:18:11 +02:00
default: false,
2021-04-10 19:45:11 +02:00
},
label: {
2022-05-20 01:18:11 +02:00
default: false,
2021-04-10 19:45:11 +02:00
},
items: {
2022-05-20 01:18:11 +02:00
default: undefined,
},
2021-04-10 19:45:11 +02:00
},
computed: {
v: {
set(v) {
if (this.disabled === true) {
return;
}
if (typeof this.items === 'boolean') {
this.$emit('input', v);
return;
}
2022-05-20 01:18:11 +02:00
var a = this.items.filter((i) => i !== this.value);
2021-04-10 19:45:11 +02:00
if (v) {
a.push(this.value);
}
this.$emit('input', a);
},
get() {
if (typeof this.items === 'boolean') {
return this.items;
}
if (typeof this.items === 'undefined') {
return this.$emit('input', false);
}
return this.items.indexOf(this.value) !== -1;
2022-05-20 01:18:11 +02:00
},
2021-04-10 19:45:11 +02:00
},
fieldSize() {
var sizes = ['xxs', 'xs', 'sm', 'md', 'lg'];
2022-05-20 01:18:11 +02:00
var sizeIndex = sizes.findIndex((s) => s === this.size);
2023-02-25 17:19:17 +01:00
return sizes[sizeIndex - 1];
2022-05-20 01:18:11 +02:00
},
},
2021-04-10 19:45:11 +02:00
};
</script>