Skip to content

Instantly share code, notes, and snippets.

@codemonkey76
Created November 15, 2024 03:36
Show Gist options
  • Save codemonkey76/a25b191f255f83ea1446a168645e0cb8 to your computer and use it in GitHub Desktop.
Save codemonkey76/a25b191f255f83ea1446a168645e0cb8 to your computer and use it in GitHub Desktop.
<script setup lang="ts">
import {ref, watch} from "vue";
const props = defineProps<{
modelValue?: boolean | undefined
}>();
const emit = defineEmits<{
(event: 'update:modelValue', value: boolean|undefined): void
}>();
const currentValue = ref<boolean|undefined>(props.modelValue);
watch(
() => props.modelValue,
(newValue) => currentValue.value = newValue,
{ immediate: true }
);
const toggleState = () => {
console.log("ToggleState()", currentValue.value);
currentValue.value = currentValue.value === false ? true : false;
emit("update:modelValue", currentValue.value);
}
</script>
<template>
<input
type="checkbox"
:checked="currentValue === true"
:indeterminate="typeof currentValue === 'undefined'"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
@change.prevent="toggleState" />
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment