Skip to content

Commit fdb2fb6

Browse files
authored
Merge pull request #173 from phen0menon/feature/row-differentiate
Make it possible to differentiate column row / content row
2 parents 5c9d934 + 8354c4f commit fdb2fb6

5 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/HeaderRow.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import React from "react";
6+
import { render } from "@testing-library/react";
7+
import HeaderRow from "./HeaderRow";
8+
9+
describe("<HeaderRow />", () => {
10+
test("renders", () => {
11+
render(<HeaderRow />);
12+
const row = document.querySelector("tr");
13+
expect(row).not.toBeNull();
14+
});
15+
test("renders with children", () => {
16+
render(
17+
<HeaderRow>
18+
<th></th>
19+
</HeaderRow>
20+
);
21+
const cell = document.querySelector("tr th");
22+
expect(cell).not.toBeNull();
23+
});
24+
});

src/HeaderRow.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from "react";
2+
import * as Types from "./types";
3+
4+
const HeaderRow: Types.HeaderRowComponent = (props) => <tr {...props} />;
5+
6+
export default HeaderRow;

src/Row.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import Row from "./Row";
88

99
describe("<Row />", () => {
1010
test("renders", () => {
11-
render(<Row />);
11+
render(<Row row={0} />);
1212
const row = document.querySelector("tr");
1313
expect(row).not.toBeNull();
1414
});
1515
test("renders with children", () => {
1616
render(
17-
<Row>
17+
<Row row={1}>
1818
<td></td>
1919
</Row>
2020
);

src/Spreadsheet.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Parser as FormulaParser } from "hot-formula-parser";
1010

1111
import DefaultTable from "./Table";
1212
import DefaultRow from "./Row";
13+
import DefaultHeaderRow from "./HeaderRow";
1314
import DefaultCornerIndicator from "./CornerIndicator";
1415
import DefaultColumnIndicator from "./ColumnIndicator";
1516
import DefaultRowIndicator from "./RowIndicator";
@@ -80,6 +81,8 @@ export type Props<CellType extends Types.CellBase> = {
8081
Table?: Types.TableComponent;
8182
/** The Spreadsheet's row component. */
8283
Row?: Types.RowComponent;
84+
/** The spreadsheet's header row component */
85+
HeaderRow?: Types.HeaderRowComponent;
8386
/** The Spreadsheet's cell component. */
8487
Cell?: Types.CellComponent<CellType>;
8588
/** Component rendered for cells in view mode. */
@@ -127,6 +130,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
127130
onKeyDown,
128131
Table = DefaultTable,
129132
Row = DefaultRow,
133+
HeaderRow = DefaultHeaderRow,
130134
CornerIndicator = DefaultCornerIndicator,
131135
DataEditor = DefaultDataEditor,
132136
DataViewer = DefaultDataViewer,
@@ -400,7 +404,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
400404
const tableNode = React.useMemo(
401405
() => (
402406
<Table columns={size.columns} hideColumnIndicators={hideColumnIndicators}>
403-
<Row>
407+
<HeaderRow>
404408
{!hideRowIndicators && !hideColumnIndicators && <CornerIndicator />}
405409
{!hideColumnIndicators &&
406410
range(size.columns).map((columnNumber) =>
@@ -418,9 +422,9 @@ const Spreadsheet = <CellType extends Types.CellBase>(
418422
<ColumnIndicator key={columnNumber} column={columnNumber} />
419423
)
420424
)}
421-
</Row>
425+
</HeaderRow>
422426
{range(size.rows).map((rowNumber) => (
423-
<Row key={rowNumber}>
427+
<Row key={rowNumber} row={rowNumber}>
424428
{!hideRowIndicators &&
425429
(rowLabels ? (
426430
<RowIndicator
@@ -451,6 +455,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
451455
size.columns,
452456
hideColumnIndicators,
453457
Row,
458+
HeaderRow,
454459
hideRowIndicators,
455460
CornerIndicator,
456461
columnLabels,

src/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,20 @@ export type TableProps = React.PropsWithChildren<{
149149
export type TableComponent = React.ComponentType<TableProps>;
150150

151151
/** Type of the Spreadsheet Row component props */
152-
export type RowProps = React.PropsWithChildren<{}>;
152+
export type RowProps = React.PropsWithChildren<{
153+
/** The row index of the table */
154+
row: number;
155+
}>;
153156

154157
/** Type of the Row component */
155158
export type RowComponent = React.ComponentType<RowProps>;
156159

160+
/** Type of the Spreadsheet HeaderRow component props */
161+
export type HeaderRowProps = React.PropsWithChildren<{}>;
162+
163+
/** Type of the HeaderRow component */
164+
export type HeaderRowComponent = React.ComponentType<HeaderRowProps>;
165+
157166
/** Type of the Spreadsheet RowIndicator component props */
158167
export type RowIndicatorProps = {
159168
/** The row the indicator indicates */

0 commit comments

Comments
 (0)