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 diff --git a/tests/refs.spec.tsx b/tests/refs.spec.tsx index f291741e5..7b0efec35 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; - }, }); }); @@ -54,12 +49,12 @@ describe('Table.Ref', () => { ref.current.scrollTo({ index: 0, }); - expect(scrollIntoViewElement.textContent).toEqual('light'); + expect(scrollParam.top).toEqual(0); // Scroll key ref.current.scrollTo({ key: 'bamboo', }); - expect(scrollIntoViewElement.textContent).toEqual('bamboo'); + expect(scrollParam.top).toEqual(0); }); });