Skip to content

Added a cell renderer to GroupColumnHeaderCell #3451

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/GroupedColumnHeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function GroupedColumnHeaderCell<R, SR>({
isCellSelected,
selectCell
}: GroupedColumnHeaderCellProps<R, SR>) {
const { tabIndex, onFocus } = useRovingTabIndex(isCellSelected);
const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellSelected);
const { colSpan } = column;
const rowSpan = getHeaderCellRowSpan(column, rowIdx);
const index = column.idx + 1;
Expand All @@ -48,7 +48,7 @@ export default function GroupedColumnHeaderCell<R, SR>({
onFocus={onFocus}
onClick={onClick}
>
{column.name}
{column.renderGroupHeaderCell ? column.renderGroupHeaderCell({ column, tabIndex: childTabIndex }) : column.name}
</div>
);
}
3 changes: 2 additions & 1 deletion src/hooks/useCalculatedColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export function useCalculatedColumns<R, SR>({
idx: -1,
colSpan: 0,
level: 0,
headerCellClass: rawColumn.headerCellClass
headerCellClass: rawColumn.headerCellClass,
renderGroupHeaderCell: rawColumn.renderGroupHeaderCell
};

collectColumns(rawColumn.children, level + 1, calculatedColumnParent);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type {
RenderGroupCellProps,
RenderEditCellProps,
RenderHeaderCellProps,
RenderHeaderGroupCellProps,
CellRendererProps,
RenderRowProps,
RowsChangeData,
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export interface ColumnGroup<R, SR = unknown> {
readonly name: string | ReactElement;
readonly headerCellClass?: Maybe<string>;
readonly children: readonly ColumnOrColumnGroup<R, SR>[];
/** Render function used to render the content of the group column's header cell */
readonly renderGroupHeaderCell?: Maybe<(props: RenderHeaderGroupCellProps<R, SR>) => ReactNode>;
}

export interface CalculatedColumnParent<R, SR> {
Expand All @@ -88,6 +90,8 @@ export interface CalculatedColumnParent<R, SR> {
readonly colSpan: number;
readonly level: number;
readonly headerCellClass?: Maybe<string>;
/** Render function used to render the content of the group column's header cell */
readonly renderGroupHeaderCell?: Maybe<(props: RenderHeaderGroupCellProps<R, SR>) => ReactNode>;
}

export type ColumnOrColumnGroup<R, SR = unknown> = Column<R, SR> | ColumnGroup<R, SR>;
Expand Down Expand Up @@ -140,6 +144,11 @@ export interface RenderHeaderCellProps<TRow, TSummaryRow = unknown> {
tabIndex: number;
}

export interface RenderHeaderGroupCellProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumnParent<TRow, TSummaryRow>;
tabIndex: number;
}

export interface CellRendererProps<TRow, TSummaryRow>
extends Pick<RenderRowProps<TRow, TSummaryRow>, 'row' | 'rowIdx' | 'selectCell'>,
Omit<
Expand Down
46 changes: 46 additions & 0 deletions test/column/renderGroupHeaderCell.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { ColumnOrColumnGroup } from '../../src';
import { setup } from '../utils';
import { screen, within } from '@testing-library/react';

interface Row {
id: number;
firstName: string;
lastName: string;
age: number;
birthdayDay: number;
birthdayMonth: number;
birthdayYear: number;
}

test('renderGroupHeaderCell is either undefined or a component', () => {
const columns: readonly ColumnOrColumnGroup<Row>[] = [
{ key: 'id', name: 'ID' },
{
name: 'Full Name',
renderGroupHeaderCell: ({ column }) => `👤 ${column.name}`,
children: [
{ key: 'firstName', name: 'Name' },
{ key: 'lastName', name: 'Surname' }
]
},
{ key: 'age', name: 'Age' },
{
name: 'Birthday',
children: [
{ key: 'birthdayDay', name: 'Day' },
{ key: 'birthdayMonth', name: 'Month' },
{ key: 'birthdayYear', name: 'Year' }
]
}
];

setup({ columns, rows: [] });
const rows = screen.getAllByRole('row');
expect(rows).toHaveLength(2);
const groupedHeaders = within(rows[0]).getAllByRole('columnheader');
const columnHeaders = within(rows[1]).getAllByRole('columnheader');
expect(groupedHeaders).toHaveLength(2);
expect(columnHeaders).toHaveLength(7);
expect(groupedHeaders[0]).toHaveTextContent('👤 Full Name');
expect(groupedHeaders[1]).toHaveTextContent('Birthday');
});