adrema/resources/js/components/FSwitch.vue

149 lines
4.1 KiB
Vue
Raw Normal View History

2021-04-10 19:45:11 +02:00
<template>
2022-12-02 00:42:06 +01:00
<label class="flex flex-col relative field-switch cursor-pointer" :for="id" :class="sizes[size].wrap">
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
>
2021-04-11 20:35:18 +02:00
<div class="relative inner-field mt-1" :class="`h-field-${fieldSize}`">
2022-05-20 01:18:11 +02:00
<input
:id="id"
type="checkbox"
:name="name"
:value="value"
v-model="v"
:disabled="disabled"
class="absolute peer"
@keypress="$emit('keypress', $event)"
2022-05-20 01:18:11 +02:00
/>
<span
class="relative cursor-pointer peer-focus:bg-red-500 flex grow display"
2022-05-20 01:18:11 +02:00
:class="{'bg-switch': v === true, 'bg-gray-700': v === false}"
>
<span
><svg-sprite
class="relative text-gray-400 flex-none"
2022-12-02 00:42:06 +01:00
:class="{'w-2 h-2': size === 'sm' || size == 'xs', 'w-4 h-4': size === 'base'}"
2022-05-20 01:18:11 +02:00
src="check"
></svg-sprite
></span>
<span
><svg-sprite
class="relative text-gray-400 flex-none"
2022-12-02 00:42:06 +01:00
:class="{'w-2 h-2': size === 'sm' || size == 'xs', 'w-4 h-4': size === 'base'}"
2022-05-20 01:18:11 +02:00
src="close"
></svg-sprite
></span>
2021-04-11 20:35:18 +02:00
<var class="absolute overlay bg-gray-400 rounded top-0"></var>
2021-04-10 19:45:11 +02:00
</span>
</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: {
inset: {
type: Boolean,
2022-05-20 01:18:11 +02:00
default: false,
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);
2021-04-10 19:45:11 +02:00
return sizes[this.inset ? sizeIndex : sizeIndex - 1];
},
outerSize() {
var sizes = ['xxs', 'xs', 'sm', 'md', 'lg'];
2022-05-20 01:18:11 +02:00
var sizeIndex = sizes.findIndex((s) => s === this.size);
if (!this.label || this.inset) {
sizeIndex--;
}
2021-04-10 19:45:11 +02:00
return sizes[sizeIndex];
2022-05-20 01:18:11 +02:00
},
},
2021-04-10 19:45:11 +02:00
};
</script>