Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,16 @@ function Table<RecordType extends DefaultRecordType>(
});
} 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
Expand Down
9 changes: 2 additions & 7 deletions tests/refs.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
});
});

Expand Down Expand Up @@ -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);
});
});