Skip to content

Commit c4acbe4

Browse files
committed
define row container on outside cell
1 parent 2e99f17 commit c4acbe4

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

src/Row.tsx

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import React, { SVGProps, useCallback, useContext, useLayoutEffect, useRef } from "react";
2-
import { CacheFunction, ColumnProps, Generic, RowProps } from "../index";
2+
import {
3+
CacheFunction,
4+
ClickFunction,
5+
ColumnProps,
6+
Generic,
7+
RowProps,
8+
RowRenderProps
9+
} from "../index";
310
//@ts-ignore TS2307
411
import Minus from "./svg/minus-circle.svg";
512
//@ts-ignore TS2307
@@ -16,6 +23,15 @@ interface TableCellProps {
1623
onExpanderClick: (event?: React.MouseEvent<Element, MouseEvent>) => void;
1724
}
1825

26+
interface RowContainerProps {
27+
row: Generic;
28+
index: number;
29+
children: React.ReactNode;
30+
containerStyle: React.CSSProperties;
31+
onRowClick: ClickFunction;
32+
rowRenderer: React.ElementType<RowRenderProps>;
33+
}
34+
1935
type CSSFunction = (index: number) => React.CSSProperties;
2036

2137
const getRowStyle = (index: number, rowStyle?: React.CSSProperties | CSSFunction) => {
@@ -74,6 +90,42 @@ const TableCell = React.memo(
7490
}
7591
);
7692

93+
const RowContainer = ({
94+
row,
95+
index,
96+
children,
97+
onRowClick,
98+
containerStyle,
99+
rowRenderer: RowRenderer
100+
}: RowContainerProps) => {
101+
const onContainerClick = useCallback(
102+
event => {
103+
if (onRowClick) {
104+
onRowClick(event, { index });
105+
}
106+
},
107+
[index, onRowClick]
108+
);
109+
110+
if (RowRenderer) {
111+
const style = {
112+
...containerStyle,
113+
display: "flex"
114+
};
115+
return (
116+
<RowRenderer row={row} index={index} style={style}>
117+
{children}
118+
</RowRenderer>
119+
);
120+
}
121+
122+
return (
123+
<div className="row-container" style={containerStyle} onClick={onContainerClick}>
124+
{children}
125+
</div>
126+
);
127+
};
128+
77129
const Row = ({
78130
row,
79131
index,
@@ -83,10 +135,10 @@ const Row = ({
83135
rowHeight,
84136
onRowClick,
85137
useRowWidth,
138+
rowRenderer,
86139
clearSizeCache,
87140
calculateHeight,
88141
generateKeyFromRow,
89-
rowRenderer: RowRenderer,
90142
subComponent: SubComponent
91143
}: RowProps) => {
92144
// hooks
@@ -118,15 +170,6 @@ const Row = ({
118170
};
119171

120172
// function(s)
121-
const onContainerClick = useCallback(
122-
event => {
123-
if (onRowClick) {
124-
onRowClick(event, { index });
125-
}
126-
},
127-
[index, onRowClick]
128-
);
129-
130173
const onExpanderClick = useCallback(() => {
131174
dispatch({ type: "updateExpanded", key: generateKeyFromRow(row, index) });
132175
expandedCalledRef.current = true;
@@ -155,28 +198,6 @@ const Row = ({
155198
expandedCalledRef.current = false;
156199
}, [isExpanded, expandedCalledRef, resetHeight, index, clearSizeCache]);
157200

158-
// components
159-
// row renderer
160-
const RowContainer = ({ children }: { children: React.ReactNode }) => {
161-
if (RowRenderer) {
162-
const style = {
163-
...containerStyle,
164-
display: "flex"
165-
};
166-
return (
167-
<RowRenderer row={row} index={index} style={style}>
168-
{children}
169-
</RowRenderer>
170-
);
171-
}
172-
173-
return (
174-
<div className="row-container" style={containerStyle} onClick={onContainerClick}>
175-
{children}
176-
</div>
177-
);
178-
};
179-
180201
return (
181202
<div
182203
ref={rowRef}
@@ -185,7 +206,13 @@ const Row = ({
185206
data-row-key={rowKey}
186207
style={{ ...style, borderBottom, width: useRowWidth ? style.width : undefined }}
187208
>
188-
<RowContainer>
209+
<RowContainer
210+
row={row}
211+
index={index}
212+
onRowClick={onRowClick}
213+
rowRenderer={rowRenderer}
214+
containerStyle={containerStyle}
215+
>
189216
{columns.map((c, i) => (
190217
<TableCell
191218
key={`${uuid}-${c.key}-${key}`}

0 commit comments

Comments
 (0)