可以设置 Office Fabric DetailsList 列 headers 的样式吗?

Can Office Fabric DetailsList column headers be styled?

我正在查看 office fabric 文档,似乎有关于如何在 DetailsList 中设置 items/content 样式的明确信息(https://developer.microsoft.com/en-us/fabric#/components/detailslist/customitemcolumns 有一个示例)但没有关于如何设置样式的信息headers 列(或者如果可能的话)。

这似乎是一个非常常见的用例(我试图将我的列 headers 居中,而不是让它们左对齐并使它们变大),所以不确定我是否只是遗漏了什么?

自定义列 headers 的一个选项是通过 onRenderDetailsHeader 事件 覆盖 headers 的呈现,然后呈现 header 带有自定义样式 的工具提示,如下所示

<DetailsList
    items={sortedItems as any[]}
    setKey="set"
    columns={columns}
    onRenderDetailsHeader={this.renderDetailsHeader}
  />


private renderDetailsHeader(detailsHeaderProps: IDetailsHeaderProps) {
    return (
      <DetailsHeader
        {...detailsHeaderProps}
        onRenderColumnHeaderTooltip={this.renderCustomHeaderTooltip}
      />
    );
  }

  private renderCustomHeaderTooltip(tooltipHostProps: ITooltipHostProps) {
    return (
      <span
        style={{
          display: "flex",
          fontFamily: "Tahoma",
          fontSize: "14px",
          justifyContent: "center",
        }}
      >
        {tooltipHostProps.children}
      </span>
    );
  }

Demo

IColumn 界面有一个名为 headerClassName 的 属性,可用于设置列 header 的样式。示例:

/* CSS */
.headerClass > span {
    /* right aligned header should have padding */
    padding-right: 15px;
}

.headerClass span {
   /* bolder font */
   font-weight: 900;

   /* Right Align the column header */
   justify-content: flex-end;
   text-align: right;

   /* green color */
   color: green;

   /* background color */
   background: pink;
}
//JSX
const columns = [
  {
    key: 'column1',
    name: 'Name',
    fieldName: 'name',
    minWidth: 100,
    maxWidth: 200,
    isResizable: true,
    heaerClassName: 'headerClass',
  },
  {
    key: 'column2',
    name: 'Value',
    fieldName: 'value',
    minWidth: 100,
    maxWidth: 200,
    isResizable: true,
  },
];

<DetailsList
  items={items}
  columns={columns}
  setKey='set'
/>

Demo

您可以使用 IDetailsColumnStyles 界面设置列 headers 的样式。

示例:

...

  const headerStyle: Partial<IDetailsColumnStyles> = {
    cellTitle: {
      color: "#c00"
    }
  }

  const columns: IColumn[] = [
    { styles: headerStyle, key: 'name', name: 'Name', fieldName: 'name', minWidth: 120 },

...

查看 IDetailsColumnStyles 的定义以查看可以设置样式的内容。