From 1bdd97e9b0a5fdb51d33d77aa12bc42b8e7862de Mon Sep 17 00:00:00 2001 From: Dmitry Artemov Date: Fri, 25 Oct 2024 14:54:14 +0200 Subject: [PATCH] feat(BaseTable): add renderHeaderCellContent prop --- .../BaseHeaderCell/BaseHeaderCell.tsx | 76 +++++++++++-------- .../BaseHeaderRow/BaseHeaderRow.tsx | 3 + src/components/BaseTable/BaseTable.tsx | 4 + src/components/BaseTable/README.md | 2 + 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/components/BaseHeaderCell/BaseHeaderCell.tsx b/src/components/BaseHeaderCell/BaseHeaderCell.tsx index d3bd4ca..21350be 100644 --- a/src/components/BaseHeaderCell/BaseHeaderCell.tsx +++ b/src/components/BaseHeaderCell/BaseHeaderCell.tsx @@ -22,6 +22,7 @@ export interface BaseHeaderCellProps { | ((header: Header, parentHeader?: Header) => string); header: Header; parentHeader?: Header; + renderHeaderCellContent?: (props: {header: Header}) => React.ReactNode; renderResizeHandle?: (props: BaseResizeHandleProps) => React.ReactNode; renderSortIndicator?: (props: BaseSortIndicatorProps) => React.ReactNode; resizeHandleClassName?: string; @@ -38,6 +39,7 @@ export const BaseHeaderCell = ({ className: classNameProp, header, parentHeader, + renderHeaderCellContent, renderResizeHandle, renderSortIndicator, resizeHandleClassName, @@ -54,6 +56,49 @@ export const BaseHeaderCell = ({ const rowSpan = header.isPlaceholder ? header.getLeafHeaders().length : 1; + const renderContent = () => { + if (renderHeaderCellContent) { + return renderHeaderCellContent({ + header, + }); + } + + return ( + + {header.column.getCanSort() ? ( + + {flexRender(header.column.columnDef.header, header.getContext())}{' '} + {renderSortIndicator ? ( + renderSortIndicator({ + className: b('sort-indicator', sortIndicatorClassName), + header, + }) + ) : ( + + )} + + ) : ( + flexRender(header.column.columnDef.header, header.getContext()) + )} + {header.column.getCanResize() && + (renderResizeHandle ? ( + renderResizeHandle({ + className: b('resize-handle', resizeHandleClassName), + header, + }) + ) : ( + + ))} + + ); + }; + return ( ({ {...attributes} style={getCellStyles(header, attributes?.style)} > - {header.column.getCanSort() ? ( - - {flexRender(header.column.columnDef.header, header.getContext())}{' '} - {renderSortIndicator ? ( - renderSortIndicator({ - className: b('sort-indicator', sortIndicatorClassName), - header, - }) - ) : ( - - )} - - ) : ( - flexRender(header.column.columnDef.header, header.getContext()) - )} - {header.column.getCanResize() && - (renderResizeHandle ? ( - renderResizeHandle({ - className: b('resize-handle', resizeHandleClassName), - header, - }) - ) : ( - - ))} + {renderContent()} ); }; diff --git a/src/components/BaseHeaderRow/BaseHeaderRow.tsx b/src/components/BaseHeaderRow/BaseHeaderRow.tsx index 7cb5c7b..97748e2 100644 --- a/src/components/BaseHeaderRow/BaseHeaderRow.tsx +++ b/src/components/BaseHeaderRow/BaseHeaderRow.tsx @@ -16,6 +16,7 @@ export interface BaseHeaderRowProps | ((headerGroup: HeaderGroup, parentHeaderGroup?: HeaderGroup) => string); headerGroup: HeaderGroup; parentHeaderGroup?: HeaderGroup; + renderHeaderCellContent?: BaseHeaderCellProps['renderHeaderCellContent']; renderResizeHandle?: (props: BaseResizeHandleProps) => React.ReactNode; renderSortIndicator: BaseHeaderCellProps['renderSortIndicator']; resizeHandleClassName?: BaseHeaderCellProps['resizeHandleClassName']; @@ -34,6 +35,7 @@ export const BaseHeaderRow = ({ className: classNameProp, headerGroup, parentHeaderGroup, + renderHeaderCellContent, renderResizeHandle, renderSortIndicator, resizeHandleClassName, @@ -65,6 +67,7 @@ export const BaseHeaderRow = ({ className={cellClassName} header={header as Header} parentHeader={parentHeader} + renderHeaderCellContent={renderHeaderCellContent} renderResizeHandle={renderResizeHandle} renderSortIndicator={renderSortIndicator} resizeHandleClassName={resizeHandleClassName} diff --git a/src/components/BaseTable/BaseTable.tsx b/src/components/BaseTable/BaseTable.tsx index 88c7e4c..fdd8cd4 100644 --- a/src/components/BaseTable/BaseTable.tsx +++ b/src/components/BaseTable/BaseTable.tsx @@ -81,6 +81,8 @@ export interface BaseTableProps['renderGroupHeader']; /** Function to override the default rendering of the entire group header row */ renderGroupHeaderRowContent?: BaseRowProps['renderGroupHeaderRowContent']; + /** Function to override the default rendering of header cell content */ + renderHeaderCellContent?: BaseHeaderRowProps['renderHeaderCellContent']; /** Function to override the default rendering of resize handles */ renderResizeHandle?: BaseHeaderRowProps['renderResizeHandle']; /** Function to override the default rendering of sort indicators */ @@ -137,6 +139,7 @@ export const BaseTable = React.forwardRef( renderCustomRowContent, renderGroupHeader, renderGroupHeaderRowContent, + renderHeaderCellContent, renderResizeHandle, renderSortIndicator, resizeHandleClassName, @@ -252,6 +255,7 @@ export const BaseTable = React.forwardRef( className={headerRowClassName} headerGroup={headerGroup} parentHeaderGroup={headerGroups[index - 1]} + renderHeaderCellContent={renderHeaderCellContent} renderResizeHandle={renderResizeHandle} renderSortIndicator={renderSortIndicator} resizeHandleClassName={resizeHandleClassName} diff --git a/src/components/BaseTable/README.md b/src/components/BaseTable/README.md index 1339066..ee86988 100644 --- a/src/components/BaseTable/README.md +++ b/src/components/BaseTable/README.md @@ -8,6 +8,7 @@ | attributes | HTML attributes for the `` element | `React.TableHTMLAttributes` | | bodyAttributes | HTML attributes for the `` element | `React.HTMLAttributes` | | bodyClassName | CSS classes for the `` element | `string` | +| bodyRef | Ref for the `` element | `React.Ref` | | cellAttributes | HTML attributes for the `` | `React.TdHTMLAttributes`
`((cell?: Cell) => React.TdHTMLAttributes)` | | cellClassName | CSS classes for the `
` | `string`
`((cell?: Cell) => string)` | | className | CSS classes for the `
` elements inside `
` elements inside `
` element | `string` | @@ -32,6 +33,7 @@ | renderCustomRowContent | Function to render custom rows | `(props: {row: Row; Cell: React.FunctionComponent>; cellClassName?: BaseCellProps['className']}) => React.ReactNode` | | renderGroupHeader | Function to override the default rendering of group headers | `(props: {row: Row; className?: string; getGroupTitle?: (row: Row) => React.ReactNode}) => React.ReactNode` | | renderGroupHeaderRowContent | Function to override the default rendering of the entire group header row | `(props: {row: Row; Cell: React.FunctionComponent>; cellClassName?: BaseCellProps['className']; getGroupTitle?: (row: Row) => React.ReactNode;}) => React.ReactNode` | +| renderHeaderCellContent | Function to override the default rendering of header cell content | `(props: {header: Header}) => React.ReactNode` | | renderResizeHandle | Function to override the default rendering of resize handles | `(props: {header: Header; className?: string}) => React.ReactNode` | | renderSortIndicator | Function to override the default rendering of sort indicators | `(props: {header: Header; className?: string}) => React.ReactNode` | | resizeHandleClassName | CSS classes for resize handles in `
` elements | `string` |