From 107eac00a36e1c79bd4447a8707a884e133a4ba8 Mon Sep 17 00:00:00 2001 From: betavs <34408516+betavs@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:45:12 +0800 Subject: [PATCH] fix(components): [table] selection fixed unexpected (#17904) * fix(components): [table] selection fixed unexpected * perf(components): [table] selection fixed unexpected * perf(components): [table] selection fixed unexpected --- .../table/__tests__/table-column.test.ts | 6 +++- .../components/table/src/store/watcher.ts | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/components/table/__tests__/table-column.test.ts b/packages/components/table/__tests__/table-column.test.ts index 1383a2c65d..8ec81641da 100644 --- a/packages/components/table/__tests__/table-column.test.ts +++ b/packages/components/table/__tests__/table-column.test.ts @@ -1121,7 +1121,8 @@ describe('table column', () => { }, template: ` - + + @@ -1144,6 +1145,9 @@ describe('table column', () => { wrapper.vm.fixed = true await doubleWait() expect(wrapper.find('.el-table-fixed-column--left').exists()).toBeTruthy() + wrapper.vm.fixed = false + await doubleWait() + expect(wrapper.find('.el-table-fixed-column--left').exists()).toBeFalsy() wrapper.unmount() }) diff --git a/packages/components/table/src/store/watcher.ts b/packages/components/table/src/store/watcher.ts index fa34d64c4c..8fc4c5cafb 100644 --- a/packages/components/table/src/store/watcher.ts +++ b/packages/components/table/src/store/watcher.ts @@ -1,7 +1,7 @@ // @ts-nocheck import { getCurrentInstance, ref, toRefs, unref, watch } from 'vue' import { isEqual } from 'lodash-unified' -import { hasOwn } from '@element-plus/utils' +import { hasOwn, isUndefined } from '@element-plus/utils' import { getColumnById, getColumnByKey, @@ -94,6 +94,8 @@ function useWatcher() { }) } + let selectionInitialFixed = undefined + // 更新列 const updateColumns = () => { _columns.value.forEach((column) => { @@ -105,14 +107,35 @@ function useWatcher() { rightFixedColumns.value = _columns.value.filter( (column) => column.fixed === 'right' ) + + if ( + isUndefined(selectionInitialFixed) && + _columns.value[0] && + _columns.value[0].type === 'selection' + ) { + selectionInitialFixed = Boolean(_columns.value[0].fixed) + } + if ( fixedColumns.value.length > 0 && _columns.value[0] && - _columns.value[0].type === 'selection' && - !_columns.value[0].fixed + _columns.value[0].type === 'selection' ) { - _columns.value[0].fixed = true - fixedColumns.value.unshift(_columns.value[0]) + if (!_columns.value[0].fixed) { + _columns.value[0].fixed = true + fixedColumns.value.unshift(_columns.value[0]) + } else { + const hasNotSelectionColumns = fixedColumns.value.some( + (column) => column.type !== 'selection' + ) + + if (!hasNotSelectionColumns) { + _columns.value[0].fixed = selectionInitialFixed + if (!selectionInitialFixed) fixedColumns.value.shift() + } else { + selectionInitialFixed = undefined + } + } } const notFixedColumns = _columns.value.filter((column) => !column.fixed)