react-data-grid icon indicating copy to clipboard operation
react-data-grid copied to clipboard

Column sort broken when headerRenderer prop provided

Open sayanything830 opened this issue 5 years ago • 4 comments

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?

sayanything830 avatar Feb 10 '21 22:02 sayanything830

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.

sayanything830 avatar Feb 10 '21 22:02 sayanything830

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 avatar Mar 22 '21 22:03 rzwc

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>
    );
};

sayanything830 avatar Mar 23 '21 23:03 sayanything830

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

amanmahajan7 avatar Mar 24 '21 14:03 amanmahajan7