@lui-ui/lui-vue
Version:
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
20 lines (19 loc) • 839 B
JavaScript
import { computed, toRef } from "vue";
export function useGlobalCheckbox(props, attrs) {
const modelValueAsArray = toRef(props, "modelValue");
const handleVModel = function(e) {
if (typeof props.modelValue === "boolean" || props.modelValue === void 0)
return e.target.checked;
if (e.target.checked) {
modelValueAsArray.value.push(e.target.value);
} else {
const index = modelValueAsArray.value.indexOf(e.target.value);
modelValueAsArray.value.splice(index, 1);
}
return modelValueAsArray.value;
};
const isInputChecked = computed(() => {
return props.modelValue === void 0 ? attrs.checked !== void 0 ? attrs.checked : false : typeof props.modelValue === "boolean" ? props.modelValue : modelValueAsArray.value.includes(props.value);
});
return { handleVModel, isInputChecked };
}