3

In a DetailsList, is it possible to render a specific column header with both text and an icon with a tooltip like this: (I tried with onRenderDetailsHeader but I'm not sure how to handle a specific column).

enter image description here

Eric
  • 107
  • 1
  • 6

3 Answers3

3

A hack way, although the name prop is defined as string type, in fact we can pass a ReactNode to it, just need to cast its type to any.

Example code:

import { Tooltip } from 'antd'
import { InfoCircleOutlined } from '@ant-design/icons'

const columns: IColumn[] = [
{
  name: <div>xxx <Tooltip title="tooltip"><InfoCircleOutlined /></Tooltip></div> as any,
  ...
},
...
]

<DetailsList columns={columns} .../>

Effects:

enter image description here

Spark.Bao
  • 5,573
  • 2
  • 31
  • 36
2

Here is a Codepen I put together real quick. The only issue is that I couldn't get the info icon rendered as a target for the tooltip. Maybe this will give you a good start.

function onRenderDetailsHeader(props, defaultRender?) {
   return defaultRender!({
      ...props,
      onRenderColumnHeaderTooltip: (tooltipHostProps) => {
         return (
            <Fabric.TooltipHost {...tooltipHostProps} />
         )
      }
   })
}
Vitalie Braga
  • 476
  • 2
  • 3
  • 1
    By the way, this doesn't work with versions under 6.102: https://github.com/OfficeDev/office-ui-fabric-react/releases/tag/office-ui-fabric-react_v6.102.0 – Eric Nov 19 '18 at 15:20
  • Good to know. Looked over the PR introducing this feature and it seems that this is an opt-in behavior as it's not wanted by default. – Vitalie Braga Nov 21 '18 at 01:47
  • One issue I am running into with this approach is that this seems to prevent the "AccessibleLabel" from getting rendered on the headers. Take a look at https://github.com/microsoft/fluentui/blob/d85af4b0eb272cadeb80a9ac71e0552d14653e4e/packages/react/src/components/DetailsList/DetailsColumn.base.tsx, in the "_renderAccessibleLabel()" method. How can I get both the tooltip and the accessible label to show up? Appreciate any pointers. – Snooks May 19 '21 at 19:46
1

My solution is similar to Vitalie Braga's, but with a more detailed return of the onRenderColumnHeaderTooltip you can achieve this functionality. I based populating the tooltipHostProps.children from this stack overflow question, and you can also see more about styling the tooltip headers there.

One way you can achieve filtering is by looking at the tooltipHostProps.id to specify that you only want the tooltip icon on certain column header cells. In your example, the code would look something like the following:

function onRenderDetailsHeader(props, defaultRender?) {
   return defaultRender!({
      ...props,
      onRenderColumnHeaderTooltip=(tooltipHostProps) => (
            <>
                <span>
                    {tooltipHostProps?.children}
                </span>
                {tooltipHostProps?.id?.includes('value') &&
                    <span>
                        <TooltipHost content={"This is the tooltip"}>
                            <Icon iconName="info" />
                        </TooltipHost>
                    </span>
                }
            </>
        )}
   );
};
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Sarah D
  • 11
  • 2