Skip to content

Commit

Permalink
fix(Table): checkbox not checked while using props by (#2401)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdjanuar authored Oct 17, 2024
1 parent 3768cd9 commit 1b7e36c
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
<thead :class="ui.thead">
<tr :class="ui.tr.base">
<th v-if="modelValue" scope="col" :class="ui.checkbox.padding">
<UCheckbox :model-value="indeterminate || selected.length === rows.length" :indeterminate="indeterminate" v-bind="ui.default.checkbox" aria-label="Select all" @change="onChange" />
<UCheckbox
:model-value="indeterminate || selected.length === rows.length"
:indeterminate="indeterminate"
v-bind="ui.default.checkbox"
aria-label="Select all"
@change="onChange"
/>
</th>

<th v-if="$slots.expand" scope="col" :class="ui.tr.base">
Expand Down Expand Up @@ -73,7 +79,13 @@
<template v-for="(row, index) in rows" :key="index">
<tr :class="[ui.tr.base, isSelected(row) && ui.tr.selected, $attrs.onSelect && ui.tr.active, row?.class]" @click="() => onSelect(row)">
<td v-if="modelValue" :class="ui.checkbox.padding">
<UCheckbox v-model="selected" :value="row" v-bind="ui.default.checkbox" aria-label="Select row" @click.capture.stop="() => onSelect(row)" />
<UCheckbox
:model-value="isSelected(row)"
v-bind="ui.default.checkbox"
aria-label="Select row"
@change="onChangeCheckbox($event, row)"
@click.capture.stop="() => onSelect(row)"
/>
</td>

<td
Expand Down Expand Up @@ -129,7 +141,7 @@ import { table } from '#ui/ui.config'
const config = mergeConfig<typeof table>(appConfig.ui.strategy, appConfig.ui.table, table)
function defaultComparator<T> (a: T, z: T): boolean {
return a === z
return JSON.stringify(a) === JSON.stringify(z)
}
function defaultSort (a: any, b: any, direction: 'asc' | 'desc') {
Expand Down Expand Up @@ -284,7 +296,7 @@ export default defineComponent({
return props.by(a, z)
}
function isSelected (row) {
function isSelected (row: TableRow) {
if (!props.modelValue) {
return false
}
Expand All @@ -306,7 +318,7 @@ export default defineComponent({
}
}
function onSelect (row) {
function onSelect (row: TableRow) {
if (!$attrs.onSelect) {
return
}
Expand Down Expand Up @@ -338,7 +350,16 @@ export default defineComponent({
}
}
function getRowData (row: Object, rowKey: string | string[], defaultValue: any = '') {
function onChangeCheckbox (checked: boolean, row: TableRow) {
if (checked) {
selected.value.push(row)
} else {
const index = selected.value.findIndex((item) => compare(item, row))
selected.value.splice(index, 1)
}
}
function getRowData (row: TableRow, rowKey: string | string[], defaultValue: any = '') {
return get(row, rowKey, defaultValue)
}
Expand Down Expand Up @@ -386,6 +407,7 @@ export default defineComponent({
emptyState,
// eslint-disable-next-line vue/no-dupe-keys
loadingState,
onChangeCheckbox,
openedRows,
isSelected,
onSort,
Expand All @@ -397,4 +419,4 @@ export default defineComponent({
}
}
})
</script>
</script>

0 comments on commit 1b7e36c

Please sign in to comment.