Created
February 18, 2025 19:44
-
-
Save marcoarruda/129f264e5cd671eb4de5766a035d40bb to your computer and use it in GitHub Desktop.
Vue3 Checkbox Select All composable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { type Ref, computed } from 'vue' | |
export const useCheckboxSelectAll = <T>( | |
items: Ref<T[]>, | |
selected: Ref<T[]> | |
) => { | |
const checked = computed({ | |
get() { | |
if (indeterminate.value) return true | |
return selected.value.length > 0 && | |
selected.value.length === items.value.length | |
}, | |
set(value) { | |
if (value) selected.value = items.value | |
else selected.value = [] | |
} | |
}) | |
const indeterminate = computed<boolean>(() => { | |
return selected.value.length > 0 && | |
selected.value.length < items.value.length | |
}) | |
return { | |
checked, | |
indeterminate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment