Skip to content

Commit

Permalink
New updates on dynamic fixed column widths and stretch mode
Browse files Browse the repository at this point in the history
for last and all calculation
  • Loading branch information
underfisk committed Oct 1, 2020
1 parent cb384ed commit 0b90a27
Show file tree
Hide file tree
Showing 25 changed files with 15,002 additions and 767 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

12 changes: 12 additions & 0 deletions .idea/wonder-grid.iml

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

170 changes: 170 additions & 0 deletions .idea/workspace.xml

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">= 8"
},
"scripts": {
"lint": "eslint src",
"lint": "eslint src",
"lint-fix": "eslint src --fix",
"storybook": "cross-env NODE_ENV=development start-storybook -p 9001 -c storybook -s ./storybook/public",
"prettier": "pretty-quick --staged"
Expand All @@ -25,6 +25,9 @@
"dependencies": {
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@types/react-virtualized": "^9.21.10",
"babel-loader": "^8.1.0",
"babel-preset-react-app": "^9.1.2",
"clsx": "^1.1.1",
"json2csv": "^5.0.1",
"react-virtualized": "^9.21.2"
Expand Down
150 changes: 150 additions & 0 deletions src/column-grid/ColumnGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import React, {
useRef,
useEffect,
useMemo,
useCallback,
useImperativeHandle,
forwardRef,
} from "react";
import {Grid, CellMeasurerCache} from "react-virtualized";
import CellMeasurer from "../core/CellMeasureWrapper";
import useLazyRef from "../hooks/useLazyRef";
import {Column} from "./types/header.type";
import {insertDummyCells} from "../utils/helpers";
import clsx from "clsx";
import {ColumnGridProps} from "./column-grid-props";

const ColumnGrid = React.memo(
forwardRef((props: ColumnGridProps, componentRef) => {
const {current: cache} = useLazyRef(
() =>
new CellMeasurerCache({
defaultWidth: props.defaultColumnWidth,
defaultHeight: props.minRowHeight,
fixedWidth: true,
minHeight: props.minRowHeight,
//We might use another approach in here
minWidth: props.defaultColumnWidth,
})
);

useImperativeHandle(componentRef, () => ({
recomputeGridSize: () => {
gridRef.current?.recomputeGridSize();
},
forceUpdate: () => {
gridRef.current?.forceUpdate();
},
}));
const gridRef = useRef<Grid | null>(null);

const data = useMemo(() => {
return insertDummyCells(props.headers);
}, [props.headers]);

// clear cache and recompute when data changes
useEffect(() => {
cache?.clearAll();
gridRef.current?.recomputeGridSize();
}, [data]);


const headerRendererWrapper = useCallback(
({style, cell, ref, columnIndex, rowIndex}) => {
const {title, renderer} = cell as Column;
/** @todo Cache cell renderer result because if may have not changed so no need to invoke again **/
const children = renderer ? (renderer(cell) as any) : title;

return (
<div
ref={ref}
/** @todo We ensure its the first rowIndex to avoid nested getting this but i have to discuss with Pedro **/
className={
props.coords.colIndex === columnIndex && rowIndex === 0
? clsx(
props.theme?.headerClass,
props.theme?.currentColumnClass
)
: props.theme?.headerClass
}
style={{
display: "flex",
justifyContent: "center",
padding: "5px",
boxSizing: "border-box",
background: "#efefef",
border: "1px solid #ccc",
cursor: "default",
...style,
}}
>
{children}
</div>
);
},
[props.coords, props.theme, props.width]
);

const cellMeasurerWrapperRenderer = useCallback(
(args) => {
const cell = data[args.rowIndex]?.[args.columnIndex];
if (!cell) {
return null;
}
const style = {
...args.style,
...cell["style"],
width: props.getColumnWidth({index: args.columnIndex}),
userSelect: "none",
};

const rendererProps = {
...args,
cell,
};
return (
<CellMeasurer
cache={cache}
columnIndex={args.columnIndex}
key={args.key}
parent={args.parent}
rowIndex={args.rowIndex}
rowSpan={cell.rowSpan}
colSpan={cell.colSpan}
cellRenderer={headerRendererWrapper}
rendererProps={rendererProps}
style={style}
/>
);
},
[data, props.theme, props.coords, props.width]
);

const columnCount = useMemo(() => {
return data.length ? data[0].length : 0;
}, [data]);

const onRefMount = useCallback((instance) => {
gridRef.current = instance;
}, []);

return (
<Grid
{...(props as any)}
ref={onRefMount}
cellRenderer={cellMeasurerWrapperRenderer}
deferredMeasurementCache={cache}
rowHeight={cache.rowHeight}
rowCount={data.length}
columnCount={columnCount}
overscanRowCount={2}
overscanColumnCount={2}
width={props.width}
columnWidth={props.getColumnWidth}
autoHeight
/>
);
})
);

export default ColumnGrid;
Loading

0 comments on commit 0b90a27

Please sign in to comment.