Skip to content

Commit df17aab

Browse files
committed
use header height
1 parent 40b8fae commit df17aab

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-fluid-table",
3-
"version": "0.4.8",
3+
"version": "0.4.9",
44
"description": "A React table inspired by react-window",
55
"author": "Mckervin Ceme <mckervinc@live.com>",
66
"license": "MIT",

src/AutoSizer.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { findFooterByUuid, findHeaderByUuid } from "./util";
66
interface AutoSizerProps {
77
numRows: number;
88
rowHeight?: number;
9+
headerHeight?: number;
910
tableWidth?: number;
1011
tableHeight?: number;
1112
minTableHeight?: number;
@@ -60,13 +61,22 @@ const findCorrectHeight = ({
6061
return containerHeight || computedHeight;
6162
};
6263

63-
const calculateHeight = (rowHeight: number, uuid: string, size: number, hasFooter: boolean) => {
64+
const calculateHeight = (
65+
rowHeight: number,
66+
headerHeight: number,
67+
uuid: string,
68+
size: number,
69+
hasFooter: boolean
70+
) => {
6471
// get the header, footer and nodes
6572
const header = findHeaderByUuid(uuid);
6673
const nodes = [...(header?.nextElementSibling?.children || [])] as HTMLElement[];
6774
let footerHeight = findFooterByUuid(uuid)?.offsetHeight || 0;
6875

6976
if (!!header && !!nodes.length) {
77+
// header height to use
78+
const headerOffset = headerHeight > 0 ? headerHeight : header.offsetHeight;
79+
7080
// get border height
7181
let borders = 0;
7282
const table = header.parentElement?.parentElement;
@@ -76,12 +86,12 @@ const calculateHeight = (rowHeight: number, uuid: string, size: number, hasFoote
7686

7787
// perform calculation
7888
if (rowHeight > 0) {
79-
return header.offsetHeight + nodes.length * rowHeight + footerHeight + borders;
89+
return headerOffset + nodes.length * rowHeight + footerHeight + borders;
8090
}
8191

8292
let overscan = 0;
8393
return (
84-
header.offsetHeight +
94+
headerOffset +
8595
nodes.reduce((pv, c) => {
8696
overscan = c.offsetHeight;
8797
return pv + c.offsetHeight;
@@ -92,14 +102,15 @@ const calculateHeight = (rowHeight: number, uuid: string, size: number, hasFoote
92102
);
93103
}
94104

95-
// try and guess the footer height
105+
// try and guess the header and footer height
106+
const headerOffset = headerHeight || DEFAULT_HEADER_HEIGHT;
96107
if (!footerHeight && hasFooter) {
97-
footerHeight = DEFAULT_HEADER_HEIGHT;
108+
footerHeight = headerOffset;
98109
}
99110

100111
// if the header and nodes are not specified, guess the height
101112
const height = Math.max(rowHeight || DEFAULT_ROW_HEIGHT, 10);
102-
return height * Math.min(size || 10, 10) + DEFAULT_HEADER_HEIGHT + footerHeight;
113+
return height * Math.min(size || 10, 10) + headerOffset + footerHeight;
103114
};
104115

105116
/**
@@ -115,6 +126,7 @@ const AutoSizer = ({
115126
tableHeight,
116127
minTableHeight,
117128
maxTableHeight,
129+
headerHeight,
118130
children
119131
}: AutoSizerProps) => {
120132
// hooks
@@ -137,8 +149,8 @@ const AutoSizer = ({
137149
return tableHeight;
138150
}
139151

140-
return calculateHeight(rowHeight || 0, uuid, numRows, hasFooter);
141-
}, [tableHeight, rowHeight, numRows, uuid, hasFooter]);
152+
return calculateHeight(rowHeight || 0, headerHeight || 0, uuid, numRows, hasFooter);
153+
}, [tableHeight, rowHeight, headerHeight, numRows, uuid, hasFooter]);
142154

143155
// calculate the actual height of the table
144156
const height = findCorrectHeight({

src/Table.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const ListComponent = forwardRef(
5757
ref: React.ForwardedRef<TableRef>
5858
) => {
5959
// hooks
60-
const heightRef = useRef(-1);
6160
const timeoutRef = useRef(0);
6261
const prevRef = useRef(width);
6362
const cacheRef = useRef<any>({});
@@ -173,15 +172,6 @@ const ListComponent = forwardRef(
173172
// check if we should use the row width when width changes
174173
useEffect(() => shouldUseRowWidth(), [width]);
175174

176-
// every time the height changes, clear the height cache
177-
useLayoutEffect(() => {
178-
if (heightRef.current >= 0 && heightRef.current !== height) {
179-
clearSizeCache(-1, true);
180-
}
181-
182-
heightRef.current = height;
183-
}, [height]);
184-
185175
// manually alter the height of each row if height is incorrect
186176
// to help with flicker on resize
187177
useLayoutEffect(() => {
@@ -411,6 +401,7 @@ const Table = forwardRef(
411401
rowHeight={rest.rowHeight}
412402
minTableHeight={minTableHeight}
413403
maxTableHeight={maxHeight}
404+
headerHeight={rest.headerHeight}
414405
>
415406
{({ height, width }) => {
416407
return (

0 commit comments

Comments
 (0)