Skip to content

Commit 8354c4f

Browse files
committed
Create HeaderRow component for table header row
1 parent b48dc0a commit 8354c4f

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";
@@ -78,6 +79,8 @@ export type Props<CellType extends Types.CellBase> = {
7879
Table?: Types.TableComponent;
7980
/** The Spreadsheet's row component. */
8081
Row?: Types.RowComponent;
82+
/** The spreadsheet's header row component */
83+
HeaderRow?: Types.HeaderRowComponent;
8184
/** The Spreadsheet's cell component. */
8285
Cell?: Types.CellComponent<CellType>;
8386
/** Component rendered for cells in view mode. */
@@ -124,6 +127,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
124127
onKeyDown,
125128
Table = DefaultTable,
126129
Row = DefaultRow,
130+
HeaderRow = DefaultHeaderRow,
127131
CornerIndicator = DefaultCornerIndicator,
128132
DataEditor = DefaultDataEditor,
129133
DataViewer = DefaultDataViewer,
@@ -397,7 +401,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
397401
const tableNode = React.useMemo(
398402
() => (
399403
<Table columns={size.columns} hideColumnIndicators={hideColumnIndicators}>
400-
<Row>
404+
<HeaderRow>
401405
{!hideRowIndicators && !hideColumnIndicators && <CornerIndicator />}
402406
{!hideColumnIndicators &&
403407
range(size.columns).map((columnNumber) =>
@@ -415,9 +419,9 @@ const Spreadsheet = <CellType extends Types.CellBase>(
415419
<ColumnIndicator key={columnNumber} column={columnNumber} />
416420
)
417421
)}
418-
</Row>
422+
</HeaderRow>
419423
{range(size.rows).map((rowNumber) => (
420-
<Row key={rowNumber}>
424+
<Row key={rowNumber} row={rowNumber}>
421425
{!hideRowIndicators &&
422426
(rowLabels ? (
423427
<RowIndicator
@@ -448,6 +452,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
448452
size.columns,
449453
hideColumnIndicators,
450454
Row,
455+
HeaderRow,
451456
hideRowIndicators,
452457
CornerIndicator,
453458
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)