Skip to content

Instantly share code, notes, and snippets.

@marcoarruda
Created February 18, 2025 19:44
Show Gist options
  • Save marcoarruda/129f264e5cd671eb4de5766a035d40bb to your computer and use it in GitHub Desktop.
Save marcoarruda/129f264e5cd671eb4de5766a035d40bb to your computer and use it in GitHub Desktop.
Vue3 Checkbox Select All composable
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