Skip to content

Commit 63fb316

Browse files
Added support for headerRowClass. (#3405)
* Added support for headerRowClass. Works similar to rowClass but gets applied on the header row. Co-authored-by: Aman Mahajan <[email protected]>
1 parent f30e819 commit 63fb316

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ about: Create a bug report
44
title: ''
55
labels: Feature Request
66
assignees: ''
7-
87
---
98

109
## Describe the bug <!-- A clear and concise description of what the bug is. -->
@@ -17,6 +16,7 @@ assignees: ''
1716
## Expected behavior <!-- A clear and concise description of what you expected to happen. -->
1817

1918
## Link to Minimal Reproducible Example
19+
2020
<!-- Link to a playground, StackBlitz, or GitHub repo with a minimal reproduction of the problem. A minimal reproduction is required. -->
2121

2222
## Environment

.github/ISSUE_TEMPLATE/feature_request.md

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ about: Request a new feature or enhancement
44
title: ''
55
labels: Feature Request
66
assignees: ''
7-
87
---
98

109
## Use case <!-- Please describe what it is you would like to achieve. -->

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ __screenshots__
88

99
npm-debug.log
1010
**.orig
11+
.idea

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ function rowClass(row: Row, rowIdx: number) {
459459
}
460460
```
461461

462+
###### `headerRowClass?: Maybe<string>>`
463+
462464
###### `direction?: Maybe<'ltr' | 'rtl'>`
463465

464466
This property sets the text direction of the grid, it defaults to `'ltr'` (left-to-right). Setting `direction` to `'rtl'` has the following effects:

src/DataGrid.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
222222
*/
223223
renderers?: Maybe<Renderers<NoInfer<R>, NoInfer<SR>>>;
224224
rowClass?: Maybe<(row: NoInfer<R>, rowIdx: number) => Maybe<string>>;
225+
headerRowClass?: Maybe<string>;
225226
/** @default 'ltr' */
226227
direction?: Maybe<Direction>;
227228
'data-testid'?: Maybe<string>;
@@ -275,6 +276,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
275276
className,
276277
style,
277278
rowClass,
279+
headerRowClass,
278280
direction: rawDirection,
279281
// ARIA
280282
role: rawRole,
@@ -1118,6 +1120,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
11181120
/>
11191121
))}
11201122
<HeaderRow
1123+
headerRowClass={headerRowClass}
11211124
rowIdx={headerRowsCount}
11221125
columns={getRowViewportColumns(mainHeaderRowIdx)}
11231126
onColumnResize={handleColumnResizeLatest}

src/HeaderRow.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { css } from '@linaria/core';
33
import clsx from 'clsx';
44

55
import { getColSpan } from './utils';
6-
import type { CalculatedColumn, Direction, Position, ResizedWidth } from './types';
6+
import type { CalculatedColumn, Direction, Maybe, Position, ResizedWidth } from './types';
77
import type { DataGridProps } from './DataGrid';
88
import HeaderCell from './HeaderCell';
99
import { cell, cellFrozen } from './style/cell';
@@ -23,6 +23,7 @@ export interface HeaderRowProps<R, SR, K extends React.Key> extends SharedDataGr
2323
selectedCellIdx: number | undefined;
2424
shouldFocusGrid: boolean;
2525
direction: Direction;
26+
headerRowClass: Maybe<string>;
2627
}
2728

2829
const headerRow = css`
@@ -46,6 +47,7 @@ const headerRow = css`
4647
export const headerRowClassname = `rdg-header-row ${headerRow}`;
4748

4849
function HeaderRow<R, SR, K extends React.Key>({
50+
headerRowClass,
4951
rowIdx,
5052
columns,
5153
onColumnResize,
@@ -91,9 +93,13 @@ function HeaderRow<R, SR, K extends React.Key>({
9193
<div
9294
role="row"
9395
aria-rowindex={rowIdx} // aria-rowindex is 1 based
94-
className={clsx(headerRowClassname, {
95-
[rowSelectedClassname]: selectedCellIdx === -1
96-
})}
96+
className={clsx(
97+
headerRowClassname,
98+
{
99+
[rowSelectedClassname]: selectedCellIdx === -1
100+
},
101+
headerRowClass
102+
)}
97103
>
98104
{cells}
99105
</div>

test/browser/headerRowClass.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { page } from '@vitest/browser/context';
2+
3+
import type { Column } from '../../src';
4+
import { headerRowClassname } from '../../src/HeaderRow';
5+
import { setup } from './utils';
6+
7+
interface Row {
8+
id: number;
9+
}
10+
11+
const columns: readonly Column<Row>[] = [{ key: 'id', name: 'ID' }];
12+
const rows: readonly Row[] = [{ id: 0 }];
13+
14+
test('headerRowClass is undefined', () => {
15+
setup({
16+
columns,
17+
rows,
18+
rowClass: undefined
19+
});
20+
const header = page.getByRole('row').first();
21+
expect(header).toHaveClass(headerRowClassname, { exact: true });
22+
});
23+
24+
test('headerRowClass is a string', () => {
25+
setup({
26+
columns,
27+
rows,
28+
headerRowClass: 'my-header-row'
29+
});
30+
const header = page.getByRole('row').first();
31+
expect(header).toHaveClass(`${headerRowClassname} my-header-row`, { exact: true });
32+
});

0 commit comments

Comments
 (0)