From 43c8f3541235a34888760ad45151492d0942c125 Mon Sep 17 00:00:00 2001
From: Liao-js <43257608+Liao-js@users.noreply.github.com>
Date: Fri, 10 May 2024 20:24:11 +0800
Subject: [PATCH] fix(components): [table] selection reference when
toggleAllSelection (#16800)
* fix(components): [table] selection is not updated when length is 0
* fix(components): [table] selection reference when toggleAllSelection
* feat: add test
---
.../components/table/__tests__/table.test.ts | 40 +++++++++++++++++++
.../components/table/src/store/watcher.ts | 4 +-
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/packages/components/table/__tests__/table.test.ts b/packages/components/table/__tests__/table.test.ts
index 1104ff9c18..32cd3a2b1e 100644
--- a/packages/components/table/__tests__/table.test.ts
+++ b/packages/components/table/__tests__/table.test.ts
@@ -790,6 +790,46 @@ describe('Table.vue', () => {
wrapper.unmount()
})
+
+ it('selection reference', async () => {
+ const wrapper = mount({
+ components: {
+ ElTableColumn,
+ ElTable,
+ },
+ template: `
+
+
+
+
+
+
+ `,
+ data() {
+ return {
+ testData: getTestData(),
+ selection: null,
+ }
+ },
+ methods: {
+ handleSelectAll(selection) {
+ this.selection = selection
+ },
+ },
+ })
+
+ const vm = wrapper.vm
+ vm.$refs.table.toggleAllSelection()
+ await doubleWait()
+ const oldSelection = vm.selection
+ vm.$refs.table.toggleAllSelection()
+ await doubleWait()
+ const newSelection = vm.selection
+ vm.$refs.table.clearSelection()
+ expect(oldSelection !== newSelection).toBe(true)
+ wrapper.unmount()
+ })
+
it('sort', async () => {
const wrapper = mount({
components: {
diff --git a/packages/components/table/src/store/watcher.ts b/packages/components/table/src/store/watcher.ts
index 02c87f289b..cd35b5cb88 100644
--- a/packages/components/table/src/store/watcher.ts
+++ b/packages/components/table/src/store/watcher.ts
@@ -155,8 +155,8 @@ function useWatcher() {
const clearSelection = () => {
isAllSelected.value = false
const oldSelection = selection.value
+ selection.value = []
if (oldSelection.length) {
- selection.value = []
instance.emit('selection-change', [])
}
}
@@ -238,7 +238,7 @@ function useWatcher() {
selection.value ? selection.value.slice() : []
)
}
- instance.emit('select-all', selection.value)
+ instance.emit('select-all', (selection.value || []).slice())
}
const updateSelectionByRowKey = () => {