Skip to content

Commit

Permalink
Improve table layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed Aug 6, 2024
1 parent e9b3165 commit d3bc4f6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 43 deletions.
21 changes: 9 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"react-select": "4.3.1",
"react-table": "^7.7.0",
"react-virtualized": "9.22.3",
"react-virtualized-auto-sizer": "^1.0.5",
"react-virtualized-auto-sizer": "^1.0.24",
"react-window": "^1.8.6",
"styled-components": "^5.2.1",
"styled-system": "^5.1.5",
Expand Down
6 changes: 6 additions & 0 deletions src/lib/components/tablev2/SingleSelectableContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export function SingleSelectableContent<
const { hasScrollbar, scrollBarWidth, handleScrollbarWidth } =
useTableScrollbar();

console.log('hasScrollbar', {
hasScrollbar,
scrollBarWidth,
handleScrollbarWidth,
});

return (
<>
<div className="thead" role="rowgroup">
Expand Down
10 changes: 5 additions & 5 deletions src/lib/components/tablev2/TableCommon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export const VirtualizedRows = <
listRef,
itemKey,
}: VirtualizedRowsType<DATA_ROW>) => (
<AutoSizer>
{({ height, width }) => {
<AutoSizer disableWidth>
{({ height }) => {
return (
<List
height={height}
height={height - 1}
itemCount={rows.length} // how many items we are going to render
itemSize={convertRemToPixels(tableRowHeight[rowHeight])} // height of each row in pixel
width={width}
width={'100%'}
itemKey={itemKey}
itemData={rows}
ref={listRef}
Expand Down Expand Up @@ -81,7 +81,7 @@ export const VirtualizedRows = <
);

export const useTableScrollbar = () => {
const [hasScrollbar, setHasScrollbar] = useState(false);
const { hasScrollbar, setHasScrollbar } = useTableContext();
const [scrollBarWidth, setScrollBarWidth] = useState(0);

const handleScrollbarWidth = useCallback((node) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/tablev2/Tablestyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const TableHeader = styled.div<{
`;

type HeadRowType = {
hasScrollBar: boolean;
hasScrollBar?: boolean;
scrollBarWidth: number;
rowHeight: TableHeightKeyType;
separationLineVariant: TableVariantType;
Expand Down
14 changes: 14 additions & 0 deletions src/lib/components/tablev2/Tablev2.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ type TableContextType<
en: { singular: string; plural: string };
fr?: { singular: string; plural: string };
};
syncScrollListener: ((event: Event) => void) | null;
setSyncScrollListener: (listener: (event: Event) => void) => void;
setHasScrollbar: React.Dispatch<React.SetStateAction<boolean>>;
hasScrollbar?: boolean;
};
const TableContext = React.createContext<TableContextType | null>(null);

Expand Down Expand Up @@ -215,6 +219,12 @@ function Table<

const [rowHeight, setRowHeight] = React.useState<TableHeightKeyType>('h40');

const [syncScrollListener, setSyncScrollListener] = React.useState<
((event: Event) => void) | null
>(null);

const [hasScrollbar, setHasScrollbar] = React.useState<boolean>(false);

const {
headerGroups,
rows,
Expand Down Expand Up @@ -310,6 +320,10 @@ function Table<
toggleAllRowsSelected,
status,
entityName,
syncScrollListener,
setSyncScrollListener,
setHasScrollbar,
hasScrollbar,
};
return (
<TableContext.Provider
Expand Down
50 changes: 26 additions & 24 deletions src/lib/components/tablev2/useSyncedScroll.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
import { useEffect, useState, useCallback } from 'react';
import { useEffect, useState, useCallback, useRef } from 'react';
import { Row } from 'react-table';
import { FixedSizeList } from 'react-window';
import { useTableContext } from './Tablev2.component';

export default function useSyncedScroll<
DATA_ROW extends Record<string, unknown> = Record<string, unknown>,
>(): {
headerRef: (element: HTMLDivElement) => void;
bodyRef: (tableBody: FixedSizeList<Row<DATA_ROW>[]>) => void;
bodyRef: React.RefObject<FixedSizeList<Row<DATA_ROW>[]>>;
} {
const [listener, setListener] =
useState<((event: Event) => void) | null>(null);
const [tableBody, setTableBody] =
useState<FixedSizeList<Row<DATA_ROW>[]> | null>(null);
const { syncScrollListener, setSyncScrollListener } = useTableContext();

const headerRef = useCallback(
(element: HTMLDivElement) => {
console.log('elementaire', element);
if (element) {
const callback = (event: Event) => {
if (element && event) {
console.log('element', element);
element.scrollTo({
left: (event.target as HTMLDivElement).scrollLeft,
top: 0,
});
}
};
if (!listener) {
setListener(() => {
if (!syncScrollListener) {
console.log('setSyncScrollListener', setSyncScrollListener);
setSyncScrollListener(() => {
console.log('callback', callback);
return callback;
});
}
}
},
[listener],
[syncScrollListener],
);

const bodyRef = useCallback((tableBody: FixedSizeList<Row<DATA_ROW>[]>) => {
setTableBody(tableBody);
}, []);
const bodyRef = useRef<FixedSizeList<Row<DATA_ROW>[]> | null>(null);

useEffect(() => {
if (tableBody && listener) {
if (bodyRef.current && syncScrollListener) {
/*
We intentionally use _outerRef prop here despite the fact that it is
internal use only and not typed, as it is the only way for us to access to the scrollable element
*/
//@ts-expect-error
(tableBody._outerRef as HTMLDivElement).addEventListener(
(bodyRef.current._outerRef as HTMLDivElement).addEventListener(
'scroll',
listener,
syncScrollListener,
);

return () => {
//@ts-expect-error
if (tableBody && tableBody._outerRef) {
//@ts-expect-error
tableBody._outerRef.removeEventListener('scroll', listener);
}
};
}
}, [tableBody, listener]);
return () => {
//@ts-expect-error
if (bodyRef.current && bodyRef.current._outerRef) {
//@ts-expect-error
bodyRef.current._outerRef.removeEventListener(
'scroll',
syncScrollListener,
);
}
};
}, [bodyRef.current, syncScrollListener]);

return { headerRef, bodyRef };
}

0 comments on commit d3bc4f6

Please sign in to comment.