adrema/resources/js/components/FCheckbox.vue

145 lines
3.7 KiB
Vue
Raw Normal View History

2021-04-10 19:45:11 +02:00
<template>
<label class="flex flex-col relative field-checkbox cursor-pointer" :for="id" :class="{[`size-${size}`]: true}">
<span
v-if="label && inset"
class="z-10 absolute top-0 left-0 -mt-2 px-1 ml-3 inset-bg font-semibold text-gray-700"
>{{ label }}</span
>
2021-04-10 19:45:11 +02:00
<div class="relative flex items-start">
<input :id="id" type="checkbox" v-model="v" :disabled="disabled" class="invisible absolute" />
<span class="display-wrapper flex items-center">
<span
class="relative cursor-pointer flex flex-none justify-center items-center display"
:class="{'bg-terminoto-2': v === true, 'bg-white': v === false}"
>
2022-02-12 15:22:22 +01:00
<svg-sprite src="check" class="w-4 h-4 check-icon text-white"></svg-sprite>
2021-04-10 19:45:11 +02:00
</span>
</span>
<span
v-if="label && !inset"
class="text-sm leading-tight ml-3 text-gray-700 checkbox-label flex items-center"
>
2021-04-10 19:45:11 +02:00
<span>
<span v-text="label" v-if="!html"></span>
<span v-html="label" v-if="html"></span>
<span v-show="required" class="font-semibold text-red-700">*</span>
</span>
</span>
</div>
</label>
</template>
<script>
export default {
model: {
prop: 'items',
event: 'input',
2021-04-10 19:45:11 +02:00
},
props: {
html: {
type: Boolean,
default: false,
2021-04-10 19:45:11 +02:00
},
required: {
type: Boolean,
default: false,
2021-04-10 19:45:11 +02:00
},
inset: {
type: Boolean,
default: false,
2021-04-10 19:45:11 +02:00
},
size: {
default: null,
required: false,
2021-04-10 19:45:11 +02:00
},
id: {
required: true,
2021-04-10 19:45:11 +02:00
},
disabled: {
type: Boolean,
default: false,
2021-04-10 19:45:11 +02:00
},
value: {
default: false,
2021-04-10 19:45:11 +02:00
},
label: {
default: false,
2021-04-10 19:45:11 +02:00
},
items: {
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;
}
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;
},
},
2021-04-10 19:45:11 +02:00
},
created() {
if (typeof this.items === 'undefined') {
this.$emit('input', false);
}
},
2021-04-10 19:45:11 +02:00
};
</script>
<style lang="css">
:root {
--checkbox-width: 30px;
--margin: 0.2rem;
}
.field-checkbox {
input:checked + span {
transition: background 0.2s;
}
.display-wrapper,
.checkbox-label {
2021-04-10 19:45:11 +02:00
min-height: 34px;
}
.display {
width: var(--checkbox-width);
height: var(--checkbox-width);
border-radius: 0.3rem;
border: solid 2px hsl(60, 1.8%, 10.8%);
2021-04-10 19:45:11 +02:00
.check-icon {
opacity: 0;
transition: opacity 0.2s;
}
}
input:checked + .display-wrapper .display .check-icon {
opacity: 1;
transition: opacity 0.2s;
}
}
</style>