Refactor ItCheckbox

This commit is contained in:
Ramon Wenger 2022-11-23 14:21:43 +01:00
parent 6a2859e641
commit f61ce46fc3
3 changed files with 46 additions and 21 deletions

View File

@ -126,9 +126,9 @@ const learningSequenceBorderClass = computed(() => {
</div> </div>
<ItCheckbox <ItCheckbox
v-else v-else
:model-value="learningContent.completion_status === 'success'" :checked="learningContent.completion_status === 'success'"
:on-toggle="() => toggleCompleted(learningContent)"
:data-cy="`${learningContent.slug}-checkbox`" :data-cy="`${learningContent.slug}-checkbox`"
@toggle="toggleCompleted(learningContent)"
/> />
<div class="flex-auto pt-1 sm:pt-0"> <div class="flex-auto pt-1 sm:pt-0">
<span class="flex gap-4 items-center xl:h-10"> <span class="flex gap-4 items-center xl:h-10">

View File

@ -1,19 +1,30 @@
<script setup lang="ts"> <script setup lang="ts">
import log from "loglevel";
interface Props { interface Props {
modelValue?: boolean; checked?: boolean;
disabled?: boolean; disabled?: boolean;
onToggle?: () => void;
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
modelValue: false, checked: false,
disabled: false, disabled: false,
onToggle: () => {
// do nothing
},
}); });
defineEmits(["update:modelValue"]); const emit = defineEmits(["toggle"]);
const toggle = () => {
emit("toggle");
};
const keydown = (e: KeyboardEvent) => {
log.debug("keydown", e.type, e.key);
if (e.key === " " && !props.disabled) {
e.preventDefault();
toggle();
}
};
const input = (e: Event) => {
log.debug("input", e.type, e.target.checked, e.target.value);
emit("toggle");
};
</script> </script>
<template> <template>
@ -23,17 +34,27 @@ defineEmits(["update:modelValue"]);
'cursor-not-allowed': disabled, 'cursor-not-allowed': disabled,
}" }"
class="w-8 h-8 cursor-pointer" class="w-8 h-8 cursor-pointer"
@click="$emit('update:modelValue', !modelValue)"
> >
<button <label
v-if="modelValue" class="w-8 h-8 block flex-none bg-contain disabled:opacity-50 cy-checkbox cy-checkbox-checked"
class="w-8 h-8 flex-none bg-contain bg-[url('/static/icons/icon-checkbox-checked.svg')] hover:bg-[url('/static/icons/icon-checkbox-checked-hover.svg')] disabled:opacity-50 cy-checkbox cy-checkbox-checked" :class="
@click.stop="props.onToggle()" checked
></button> ? 'bg-[url(\'/static/icons/icon-checkbox-checked.svg\')] hover:bg-[url(\'/static/icons/icon-checkbox-checked-hover.svg\')]'
<button : 'bg-[url(\'/static/icons/icon-checkbox-unchecked.svg\')] hover:bg-[url(\'/static/icons/icon-checkbox-unchecked-hover.svg\')]'
v-else "
class="w-8 h-8 flex-none bg-contain bg-[url('/static/icons/icon-checkbox-unchecked.svg')] hover:bg-[url('/static/icons/icon-checkbox-unchecked-hover.svg')] cy-checkbox cy-checkbox-unchecked" tabindex="0"
@click.stop="props.onToggle()" @keydown.stop="keydown"
></button> >
<input
ref="checkbox"
:checked="checked"
:value="checked"
:disabled="disabled"
class="sr-only"
type="checkbox"
@keydown="keydown"
@input="input"
/>
</label>
</div> </div>
</template> </template>

View File

@ -361,7 +361,11 @@ function log(data: any) {
<h2 class="mt-8 mb-8">Checkbox</h2> <h2 class="mt-8 mb-8">Checkbox</h2>
<ItCheckbox v-model="state.checkboxValue" :disabled="false" class=""> <ItCheckbox
:checked="state.checkboxValue"
:disabled="false"
@toggle="state.checkboxValue = !state.checkboxValue"
>
Label Label
</ItCheckbox> </ItCheckbox>