From 32933e57a9d90452d8edc754f051850340c1f1e6 Mon Sep 17 00:00:00 2001 From: Fatpandac Date: Wed, 27 Dec 2023 17:55:20 +0800 Subject: [PATCH 1/3] fix: when using scrollTo the outer layer also scrolls at the same time --- src/Table.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Table.tsx b/src/Table.tsx index 7b7751554..a5930624d 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -328,7 +328,16 @@ function Table( }); } else { const mergedKey = key ?? getRowKey(mergedData[index]); - scrollBodyRef.current.querySelector(`[data-row-key="${mergedKey}"]`)?.scrollIntoView(); + const { offsetTop } = + (scrollBodyRef.current.querySelector( + `[data-row-key="${mergedKey}"]`, + ) as HTMLElement) || {}; + + if (offsetTop !== undefined) { + scrollBodyRef.current?.scrollTo({ + top: offsetTop, + }); + } } } else if ((scrollBodyRef.current as any)?.scrollTo) { // Pass to proxy From aac3ae0492f273744690690db49d4c24f01e978c Mon Sep 17 00:00:00 2001 From: Fatpandac Date: Wed, 27 Dec 2023 21:31:23 +0800 Subject: [PATCH 2/3] fix(test): remove the useless test case --- tests/refs.spec.tsx | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/refs.spec.tsx b/tests/refs.spec.tsx index f291741e5..9417b17e3 100644 --- a/tests/refs.spec.tsx +++ b/tests/refs.spec.tsx @@ -5,17 +5,12 @@ import Table, { type Reference } from '../src'; describe('Table.Ref', () => { let scrollParam: any = null; - let scrollIntoViewElement: HTMLElement = null; beforeAll(() => { spyElementPrototypes(HTMLElement, { scrollTo: (_: any, param: any) => { scrollParam = param; }, - scrollIntoView() { - // eslint-disable-next-line @typescript-eslint/no-this-alias - scrollIntoViewElement = this; - }, }); }); @@ -49,17 +44,5 @@ describe('Table.Ref', () => { }); expect(scrollParam.top).toEqual(903); - - // Scroll index - ref.current.scrollTo({ - index: 0, - }); - expect(scrollIntoViewElement.textContent).toEqual('light'); - - // Scroll key - ref.current.scrollTo({ - key: 'bamboo', - }); - expect(scrollIntoViewElement.textContent).toEqual('bamboo'); }); }); From c93290a2345db0e74cb146f35b025433c11a3141 Mon Sep 17 00:00:00 2001 From: Fatpandac Date: Wed, 27 Dec 2023 21:39:25 +0800 Subject: [PATCH 3/3] fix(test): add the test case for scrolling by index and key --- tests/refs.spec.tsx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/refs.spec.tsx b/tests/refs.spec.tsx index 9417b17e3..7b0efec35 100644 --- a/tests/refs.spec.tsx +++ b/tests/refs.spec.tsx @@ -44,5 +44,17 @@ describe('Table.Ref', () => { }); expect(scrollParam.top).toEqual(903); + + // Scroll index + ref.current.scrollTo({ + index: 0, + }); + expect(scrollParam.top).toEqual(0); + + // Scroll key + ref.current.scrollTo({ + key: 'bamboo', + }); + expect(scrollParam.top).toEqual(0); }); });