Skip to content

Commit 5e8c32d

Browse files
fix(AnalyticalTable): Expose React Table Container ref (#341)
Co-authored-by: Marcus Notheis <[email protected]>
1 parent 78ba4a4 commit 5e8c32d

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

packages/main/src/components/AnalyticalTable/AnalyticalTable.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createPassThroughPropsTest, mountThemedComponent } from '@shared/tests/utils';
22
import { AnalyticalTable } from '@ui5/webcomponents-react/lib/AnalyticalTable';
33
import { TableSelectionMode } from '@ui5/webcomponents-react/lib/TableSelectionMode';
4-
import React from 'react';
4+
import { AnalyticalTableScrollMode } from '@ui5/webcomponents-react/lib/AnalyticalTableScrollMode';
5+
import React, { useRef } from 'react';
56

67
const columns = [
78
{
@@ -277,5 +278,25 @@ describe('AnalyticalTable', () => {
277278
expect(wrapper.render()).toMatchSnapshot();
278279
});
279280

281+
test('Check for scrollTo and scrollToItem functions', () => {
282+
let tableRef;
283+
const UsingTable = (props) => {
284+
tableRef = useRef(null);
285+
return <AnalyticalTable ref={tableRef} title="Table Title" data={data} columns={columns} />;
286+
};
287+
288+
mountThemedComponent(<UsingTable />);
289+
290+
// Check existence + type
291+
expect(typeof tableRef.current.scrollTo).toBe('function');
292+
expect(typeof tableRef.current.scrollToItem).toBe('function');
293+
294+
// call functions
295+
const tableInnerRef = tableRef.current.querySelector("div[class^='AnalyticalTable--table'] > div > div");
296+
tableRef.current.scrollToItem(2, AnalyticalTableScrollMode.end);
297+
tableRef.current.scrollTo(2);
298+
expect(tableInnerRef.scrollTop).toBe(2);
299+
});
300+
280301
createPassThroughPropsTest(AnalyticalTable);
281302
});

packages/main/src/components/AnalyticalTable/hooks/useTableScrollHandles.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import { AnalyticalTableDomRef } from '../../../interfaces/AnalyticalTableDomRef
55
export const useTableScrollHandles = (ref) => {
66
const analyticalTableRef: RefObject<AnalyticalTableDomRef> = useConsolidatedRef(ref);
77
const reactWindowRef = useRef(null);
8-
// @ts-ignore
9-
useImperativeHandle(analyticalTableRef, () => ({
10-
scrollTo: (...args) => {
11-
if (reactWindowRef.current) {
12-
reactWindowRef.current.scrollTo(...args);
13-
}
14-
},
15-
scrollToItem: (...args) => {
16-
if (reactWindowRef.current) {
17-
reactWindowRef.current.scrollToItem(...args);
8+
9+
if (analyticalTableRef.current) {
10+
Object.assign(analyticalTableRef.current, {
11+
scrollTo: (...args) => {
12+
if (reactWindowRef.current) {
13+
reactWindowRef.current.scrollTo(...args);
14+
}
15+
},
16+
scrollToItem: (...args) => {
17+
if (reactWindowRef.current) {
18+
reactWindowRef.current.scrollToItem(...args);
19+
}
1820
}
19-
}
20-
}));
21+
});
22+
}
2123

2224
return [analyticalTableRef, reactWindowRef];
2325
};

packages/main/src/components/AnalyticalTable/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface TableProps extends CommonProps {
114114
onRowExpandChange?: (e?: Event) => any;
115115
onColumnsReordered?: (e?: Event) => void;
116116
/**
117-
* additional options which will be passed to [react-table´s useTable hook](https://github.com/tannerlinsley/react-table/blob/master/docs/api.md#table-options)
117+
* additional options which will be passed to [react-table´s useTable hook](https://github.com/tannerlinsley/react-table/blob/master/docs/api/useTable.md#table-options)
118118
*/
119119
reactTableOptions?: object;
120120
tableHooks?: PluginHook<any>[];
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AnalyticalTableScrollMode } from '@ui5/webcomponents-react/lib/AnalyticalTableScrollMode';
22

3-
// also makes use of scrollTo which is already defined somewhere in HTMLDivElement
3+
//@ts-ignore
44
export interface AnalyticalTableDomRef extends HTMLDivElement {
55
scrollToItem: (index: number, align?: AnalyticalTableScrollMode) => void;
6+
scrollTo: (scrollOffset: number) => void; //overrides native scrollTo function
67
}

0 commit comments

Comments
 (0)