Skip to content

Commit

Permalink
fix(Table): indeterminate checkbox with pagination (#2439)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
rdjanuar and benjamincanac authored Oct 24, 2024
1 parent 8e413f0 commit 070d2f8
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr :class="ui.tr.base">
<th v-if="modelValue" scope="col" :class="ui.checkbox.padding">
<UCheckbox
:model-value="indeterminate || selected.length === rows.length"
:model-value="isAllRowChecked"
:indeterminate="indeterminate"
v-bind="ui.default.checkbox"
aria-label="Select all"
Expand Down Expand Up @@ -276,7 +276,23 @@ export default defineComponent({
}
})
const indeterminate = computed(() => selected.value && selected.value.length > 0 && selected.value.length < props.rows.length)
const getStringifiedSet = (arr: TableRow[]) => new Set(arr.map(item => JSON.stringify(item)))
const totalRows = computed(() => props.rows.length)
const countCheckedRow = computed(() => {
const selectedData = getStringifiedSet(selected.value)
const rowsData = getStringifiedSet(props.rows)
return Array.from(selectedData).filter(item => rowsData.has(item)).length
})
const indeterminate = computed(() => {
if (!selected.value || !props.rows) return false
return countCheckedRow.value > 0 && countCheckedRow.value < totalRows.value
})
const isAllRowChecked = computed(() => countCheckedRow.value === totalRows.value)
const emptyState = computed(() => {
if (props.emptyState === null) return null
Expand Down Expand Up @@ -411,6 +427,7 @@ export default defineComponent({
emptyState,
// eslint-disable-next-line vue/no-dupe-keys
loadingState,
isAllRowChecked,
onChangeCheckbox,
openedRows,
isSelected,
Expand Down

0 comments on commit 070d2f8

Please sign in to comment.