Skip to content

Commit 64af679

Browse files
Getting cell height in grid.
1 parent 4553538 commit 64af679

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/components/Grid/Cell.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo } from "react";
1+
import { memo, useEffect, useRef } from "react";
22
import { GridChildComponentProps, areEqual } from "react-window";
33
import { ItemDataType } from "./types";
44
import { StyledCell } from "./StyledCell";
@@ -54,11 +54,30 @@ export const Cell = memo(
5454

5555
const selectionBorderLeft = rightOfSelectionBorder || rightOfFocus || isFocused;
5656
const selectionBorderTop = belowSelectionBorder || belowFocus || isFocused;
57+
58+
const cellRef = useRef<HTMLDivElement>(null);
59+
60+
// useEffect(() => {
61+
// console.log("Current ref form cell: ", cellRef.current?.getBoundingClientRect().height)
62+
// })
63+
64+
useEffect(() => {
65+
console.log("Current ref form cell: ", cellRef.current?.getBoundingClientRect().height)
66+
if (cellRef.current && data.updateRowHeight) {
67+
const height = cellRef.current.getBoundingClientRect().height;
68+
data.updateRowHeight(rowIndex, height);
69+
}
70+
}, [cellRef, data.updateRowHeight, rowIndex]);
71+
5772
return (
5873
<div
59-
style={style}
74+
style={{
75+
...style,
76+
height: "max-content"
77+
}}
6078
data-row={currentRowIndex}
6179
data-column={columnIndex}
80+
ref={cellRef}
6281
>
6382
<StyledCell
6483
as={CellData}

src/components/Grid/Grid.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
213213
onCopyCallback,
214214
]);
215215

216+
const rowHeightsRef = useRef(new Map());
217+
218+
219+
const updateRowHeight = useCallback((rowIndex: number, height: number) => {
220+
console.log("Updating row height!");
221+
const prevHeight = rowHeightsRef.current.get(rowIndex) || 0;
222+
if (height > prevHeight) {
223+
rowHeightsRef.current.set(rowIndex, height);
224+
gridRef.current?.resetAfterRowIndex(rowIndex);
225+
}
226+
}, []);
227+
228+
216229
const customOnCopy: () => Promise<void> = useMemo(() => {
217230
const result = async () => {
218231
if (onCopyProp) {
@@ -407,6 +420,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
407420
rowNumberWidth,
408421
rowStart,
409422
rowAutoHeight,
423+
updateRowHeight
410424
};
411425

412426
const InnerElementType = forwardRef<HTMLDivElement, InnerElementTypeTypes>(
@@ -758,7 +772,6 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
758772
const onItemsRendered = useCallback(
759773
(props: GridOnItemsRenderedProps) => {
760774
lastItemsRenderedProps.current = props;
761-
762775
return onItemsRenderedProp?.({
763776
...props,
764777
visibleRowStartIndex: props.visibleRowStartIndex + rowStart,
@@ -794,9 +807,30 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
794807
useEffect(() => {
795808
if (gridRef.current) {
796809
gridRef.current.resetAfterRowIndex(0);
810+
console.log("Inner: ", innerCellRef.current?.scrollHeight)
811+
console.log("Bounding: ", innerCellRef.current?.scrollHeight)
797812
}
798813
}, [rowCount]);
799814

815+
816+
const innerCellRef = useRef<HTMLDivElement>(null);
817+
818+
useEffect(() => {
819+
console.log("Inner: ", innerCellRef.current?.getBoundingClientRect().height)
820+
if (innerCellRef.current) {
821+
// gridRef.current.resetAfterRowIndex(0);
822+
console.log("Is current")
823+
}
824+
}, [onItemsRendered]);
825+
826+
const getRowHeight = useCallback((index: number) => {
827+
console.log(`GetRowHeight: from ref: ${rowHeightsRef.current.get(index)}`)
828+
return rowHeightsRef.current.get(index) || rowHeight;
829+
}, [rowHeight]);
830+
831+
832+
// console.log("Inner ref? ", innerCellRef.current?.scrollHeight)
833+
// console.log("Bounding client height? ", innerCellRef.current?.getBoundingClientRect().height)
800834
return (
801835
<ContextMenu
802836
modal={false}
@@ -831,7 +865,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
831865
height={height}
832866
width={width}
833867
columnCount={columnCount}
834-
rowHeight={() => rowHeight}
868+
rowHeight={getRowHeight}
835869
useIsScrolling={useIsScrolling}
836870
innerElementType={InnerElementType}
837871
itemData={data}
@@ -847,6 +881,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
847881
outerRef={outerRef}
848882
outerElementType={OuterElementType}
849883
onItemsRendered={onItemsRendered}
884+
innerRef={innerCellRef}
850885
{...props}
851886
>
852887
{CellWithWidth}

src/components/Grid/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export interface ItemDataType {
158158
rowNumberWidth: number;
159159
rowStart: number;
160160
rowAutoHeight?: boolean;
161+
updateRowHeight: (rowIndex: number, height: number) => void;
161162
}
162163

163164
export interface GridContextMenuItemProps extends Omit<ContextMenuItemProps, "children"> {

0 commit comments

Comments
 (0)