Column sort broken when headerRenderer prop provided
RDG version 7.0.0-canary.36
When providing the headerRenderer prop in a column definition, sortable: true is no longer valid:
{
name,
key: 'name',
resizable: true,
sortable: true,
sortType: GridColumnSortType.String,
formatter: (props: FormatterProps) => (
<CustomNameFormatter {...props} />
),
headerRenderer: (props: HeaderRendererProps<GridRowProps>) => CustomNameHeader(props),
}
From this PR: https://github.com/adazzle/react-data-grid/pull/2204/files:
function getCell() {
if (column.headerRenderer) {
return createElement(column.headerRenderer, {
column,
sortColumn,
sortDirection,
onSort,
allRowsSelected,
onAllRowsSelectionChange
});
}
if (column.sortable) {
return (
<SortableHeaderCell
column={column}
onSort={onSort}
sortColumn={sortColumn}
sortDirection={sortDirection}
>
{column.name}
</SortableHeaderCell>
);
}
return column.name;
}
if I'm reading this correctly, when a custom headerRenderer is provided, it returns before the condition for column.sortable is read. Any suggestions to work around this?
Update: I was able to create a workaround by importing the SortableHeaderCell component and wrapping my custom header
{
name,
key: 'name',
resizable: true,
sortable: true,
sortType: GridColumnSortType.String,
formatter: (props: FormatterProps) => (
<CustomNameFormatter {...props} />
),
headerRenderer: (props: HeaderRendererProps<GridRowProps>) => (
<SortableHeaderCell {...props}>
{CustomNameHeader(props)}
</SortableHeaderCell>
),
}
However, without digging deep into the source code, this solution is not intuitive.
Hi @sayanything830,
I was wondering if you could share the entire source code on how you implemented the headerRenderer with the columns? I've been having trouble passing in the props for the SortableHeaderCell, specifically the column category as I'm confused on how I pass the column to the SortableHeaderCell if I'm creating the column at the same time.
Thanks.
Hi @sayanything830,
I was wondering if you could share the entire source code on how you implemented the headerRenderer with the columns? I've been having trouble passing in the props for the SortableHeaderCell, specifically the column category as I'm confused on how I pass the column to the SortableHeaderCell if I'm creating the column at the same time.
Thanks.
@rzwc This is the gist of it, HeaderRendererProps from react-data-grid is typed as:
export interface HeaderRendererProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
sortColumn?: string;
sortDirection?: SortDirection;
onSort?: (columnKey: string, direction: SortDirection) => void;
allRowsSelected: boolean;
onAllRowsSelectionChange: (checked: boolean) => void;
}
so I can access the column prop directly, then the specific key defined within my own GridRow
interface GridRow {
name: string; // this matches my 'key' value set in the column definition
<...other keys representing columns>
}
const CustomNameHeader = (props: HeaderRendererProps<GridRow>) => {
const getConditionalTextDeterminedBySort = () => {/* returns conditional text */};
return (
<span>
{props.column.name} {getConditionalTextDeterminedBySort()}
</span>
);
};
This is by design as it allows customizing sort indicators with headerRenderer. Please check this draggable header example
https://github.com/adazzle/react-data-grid/blob/canary/stories/demos/components/HeaderRenderers/DraggableHeaderRenderer.tsx#L37
https://github.com/adazzle/react-data-grid/blob/canary/stories/demos/ColumnsReordering.tsx
We will work on the documentation when v7 stable is out