Skip to content

Added support for headerRowClass. #3405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Create a bug report
title: ''
labels: Feature Request
assignees: ''

---

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

## Link to Minimal Reproducible Example

<!-- Link to a playground, StackBlitz, or GitHub repo with a minimal reproduction of the problem. A minimal reproduction is required. -->

## Environment
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Request a new feature or enhancement
title: ''
labels: Feature Request
assignees: ''

---

## Use case <!-- Please describe what it is you would like to achieve. -->
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ __screenshots__

npm-debug.log
**.orig
.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ function rowClass(row: Row, rowIdx: number) {
}
```

###### `headerRowClass?: Maybe<string>>`

###### `direction?: Maybe<'ltr' | 'rtl'>`

This property sets the text direction of the grid, it defaults to `'ltr'` (left-to-right). Setting `direction` to `'rtl'` has the following effects:
Expand Down
3 changes: 3 additions & 0 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
*/
renderers?: Maybe<Renderers<NoInfer<R>, NoInfer<SR>>>;
rowClass?: Maybe<(row: NoInfer<R>, rowIdx: number) => Maybe<string>>;
headerRowClass?: Maybe<string>;
/** @default 'ltr' */
direction?: Maybe<Direction>;
'data-testid'?: Maybe<string>;
Expand Down Expand Up @@ -275,6 +276,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
className,
style,
rowClass,
headerRowClass,
direction: rawDirection,
// ARIA
role: rawRole,
Expand Down Expand Up @@ -1118,6 +1120,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
/>
))}
<HeaderRow
headerRowClass={headerRowClass}
rowIdx={headerRowsCount}
columns={getRowViewportColumns(mainHeaderRowIdx)}
onColumnResize={handleColumnResizeLatest}
Expand Down
14 changes: 10 additions & 4 deletions src/HeaderRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { css } from '@linaria/core';
import clsx from 'clsx';

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

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

function HeaderRow<R, SR, K extends React.Key>({
headerRowClass,
rowIdx,
columns,
onColumnResize,
Expand Down Expand Up @@ -91,9 +93,13 @@ function HeaderRow<R, SR, K extends React.Key>({
<div
role="row"
aria-rowindex={rowIdx} // aria-rowindex is 1 based
className={clsx(headerRowClassname, {
[rowSelectedClassname]: selectedCellIdx === -1
})}
className={clsx(
headerRowClassname,
{
[rowSelectedClassname]: selectedCellIdx === -1
},
headerRowClass
)}
>
{cells}
</div>
Expand Down
32 changes: 32 additions & 0 deletions test/browser/headerRowClass.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { page } from '@vitest/browser/context';

import type { Column } from '../../src';
import { headerRowClassname } from '../../src/HeaderRow';
import { setup } from './utils';

interface Row {
id: number;
}

const columns: readonly Column<Row>[] = [{ key: 'id', name: 'ID' }];
const rows: readonly Row[] = [{ id: 0 }];

test('headerRowClass is undefined', () => {
setup({
columns,
rows,
rowClass: undefined
});
const header = page.getByRole('row').first();
expect(header).toHaveClass(headerRowClassname, { exact: true });
});

test('headerRowClass is a string', () => {
setup({
columns,
rows,
headerRowClass: 'my-header-row'
});
const header = page.getByRole('row').first();
expect(header).toHaveClass(`${headerRowClassname} my-header-row`, { exact: true });
});