Skip to content

Commit 2d13964

Browse files
author
Amelia Wattenberger
committed
fix: dont wrap columns #20
was causing a row mismatch
1 parent 20e92e8 commit 2d13964

2 files changed

Lines changed: 61 additions & 59 deletions

File tree

src/components/sticky-grid.tsx

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
import React from 'react';
44
import { VariableSizeGrid } from 'react-window';
5+
import tw from 'twin.macro';
56
import { FilterValue } from '../types';
67

78
function getCellIndicies(child) {
89
return { row: child.props.rowIndex, column: child.props.columnIndex };
910
}
1011

1112
function getShownIndicies(children) {
12-
let minRow = Infinity;
13-
let maxRow = 0;
14-
let minColumn = Infinity;
15-
let maxColumn = 0;
16-
17-
React.Children.forEach(children, child => {
18-
const { row, column } = getCellIndicies(child);
19-
minRow = Math.min(minRow, row);
20-
maxRow = Math.max(maxRow, row);
21-
minColumn = Math.min(minColumn, column);
22-
maxColumn = Math.max(maxColumn, column);
23-
});
13+
const firstCell = children[0]
14+
const firstIndices = getCellIndicies(firstCell);
15+
const lastCell = children[children.length - 1];
16+
const lastIndices = getCellIndicies(lastCell);
17+
const minRow = Math.min(firstIndices.row, Infinity)
18+
const maxRow = Math.max(lastIndices.row, 0)
19+
const minColumn = Math.min(firstIndices.column, Infinity)
20+
const maxColumn = Math.max(lastIndices.column, 0)
2421

2522
return {
2623
from: {
@@ -66,7 +63,7 @@ function useInnerElementType(
6663
const shownIndicies = getShownIndicies(props.children);
6764

6865
const shownColumnsCount =
69-
shownIndicies.to.column - shownIndicies.from.column + 1 ||
66+
shownIndicies.to.column - shownIndicies.from.column + 2 ||
7067
itemData.columnNames.length;
7168
const shownRowsCount = shownIndicies.to.row - shownIndicies.from.row;
7269

@@ -88,58 +85,63 @@ function useInnerElementType(
8885
backgroundSize: `100% ${rowHeight(1)}px`,
8986
}}
9087
>
91-
{/* top left cell */}
92-
{numberOfStickiedColumns > 0 && (
93-
<HeaderComponent
94-
key="0:0"
95-
rowIndex={0}
96-
columnIndex={0}
97-
data={itemData}
98-
style={{
99-
display: 'inline-flex',
100-
width: columnWidth(0),
101-
height: rowHeight(0),
102-
position: 'sticky',
103-
float: "left",
104-
top: 0,
105-
left: 0,
106-
zIndex: 200,
107-
}}
108-
/>
109-
)}
110-
111-
{shownColumns.map((_, i) => {
112-
const columnIndex =
113-
i + shownIndicies.from.column + numberOfStickiedColumns;
114-
const rowIndex = 0;
115-
116-
const width = columnWidth(columnIndex);
117-
const height = rowHeight(rowIndex);
118-
const marginLeft =
119-
i === numberOfStickiedColumns
120-
? sumColumnWidths(columnIndex - numberOfStickiedColumns)
121-
: undefined;
122-
123-
// header row
124-
return (
88+
89+
<div
90+
css={[
91+
tw`flex sticky top-0 z-[300]`,
92+
]}>
93+
94+
{/* top left cell */}
95+
{numberOfStickiedColumns > 0 && (
12596
<HeaderComponent
126-
key={`${rowIndex}:${columnIndex}`}
127-
rowIndex={rowIndex}
128-
columnIndex={columnIndex}
97+
key="0:0"
98+
rowIndex={0}
99+
columnIndex={0}
129100
data={itemData}
130101
style={{
131-
marginLeft,
132-
display: 'flex',
133-
float: 'left',
134-
width,
135-
height,
102+
flex: "none",
103+
display: 'inline-flex',
104+
width: columnWidth(0),
105+
height: rowHeight(0),
136106
position: 'sticky',
137107
top: 0,
138-
zIndex: 100,
108+
left: 0,
109+
zIndex: 200,
139110
}}
140111
/>
141-
);
142-
})}
112+
)}
113+
114+
115+
{shownColumns.map((_, i) => {
116+
const columnIndex =
117+
i + shownIndicies.from.column + numberOfStickiedColumns;
118+
const rowIndex = 0;
119+
const width = columnWidth(columnIndex);
120+
const height = rowHeight(rowIndex);
121+
const marginLeft =
122+
i === numberOfStickiedColumns
123+
? sumColumnWidths(columnIndex - numberOfStickiedColumns)
124+
: undefined;
125+
126+
// header row
127+
return (
128+
<HeaderComponent
129+
key={`${rowIndex}:${columnIndex}`}
130+
rowIndex={rowIndex}
131+
columnIndex={columnIndex}
132+
data={itemData}
133+
style={{
134+
flex: "none",
135+
marginLeft,
136+
display: 'flex',
137+
width,
138+
height,
139+
zIndex: 100,
140+
}}
141+
/>
142+
);
143+
})}
144+
</div>
143145

144146
{numberOfStickiedColumns > 0 &&
145147
shownRows.map((_, i) => {

src/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ html,
287287

288288
.cell:hover {
289289
/* to get around an inline style */
290-
z-index: 250 !important;
290+
z-index: 50 !important;
291291
}
292292

293293
.cell:hover .cell__long-value {

0 commit comments

Comments
 (0)