diff --git a/packages/components/table-v2/__tests__/table-v2.test.tsx b/packages/components/table-v2/__tests__/table-v2.test.tsx index e53217a1fa..a29ca34a70 100644 --- a/packages/components/table-v2/__tests__/table-v2.test.tsx +++ b/packages/components/table-v2/__tests__/table-v2.test.tsx @@ -1,9 +1,10 @@ import { h, nextTick, ref } from 'vue' import { mount } from '@vue/test-utils' import { describe, expect, test } from 'vitest' -import TableV2 from '../src/table-v2' +import TableV2, { type TableV2Instance } from '../src/table-v2' import { TableV2SortOrder } from '../index' +import type { ScrollPos } from '../src/composables/use-scrollbar' import type { TableV2HeaderRowCellRendererParams, TableV2RowCellRenderParam, @@ -261,6 +262,38 @@ describe('TableV2.vue', () => { expect(cell.find('div [style^=margin-inline-star]').exists()).toBe(false) }) + test('scrollToRow keeps horizontal offset unchanged', async () => { + const columns = ref(generateColumns(10)) + const data = ref(generateData(columns.value, 200)) + const scrollLogs: ScrollPos[] = [] + const wrapper = mount(() => ( + scrollLogs.push({ ...pos })} + /> + )) + + const tableVm = wrapper.findComponent(TableV2).vm.$ + .exposed as TableV2Instance + tableVm.scrollToLeft(150) + await nextTick() + await nextTick() + + const prevScrollLeft = scrollLogs[scrollLogs.length - 1]?.scrollLeft ?? 0 + expect(prevScrollLeft).toBeGreaterThan(0) + + tableVm.scrollToRow(50) + await nextTick() + await nextTick() + + const lastScrollLeft = scrollLogs[scrollLogs.length - 1]?.scrollLeft ?? 0 + expect(lastScrollLeft).toBe(prevScrollLeft) + }) + describe('a11y', () => { test('expand button', async () => { const columns = generateColumns(10) diff --git a/packages/components/table-v2/src/table-grid.tsx b/packages/components/table-v2/src/table-grid.tsx index bfbd6719a7..c5d873c138 100644 --- a/packages/components/table-v2/src/table-grid.tsx +++ b/packages/components/table-v2/src/table-grid.tsx @@ -114,7 +114,18 @@ const useTableGrid = (props: TableV2GridProps) => { } function scrollToRow(row: number, strategy: ScrollStrategy) { - unref(bodyRef)?.scrollToItem(row, 1, strategy) + const body = unref(bodyRef) + if (!body) return + + const prevScrollLeft = scrollLeft.value + + body.scrollToItem(row, 0, strategy) + + if (prevScrollLeft) { + scrollTo({ + scrollLeft: prevScrollLeft, + }) + } } function forceUpdate() {