如何在React ui-fabric库中自定义GroupedList组件中的标题

mel*_*bil 5 frontend reactjs office-ui-fabric

我在 React ui-fabric 库中自定义 GroupedList 组件标头时遇到问题。我想做的是删除复选框和数字计数(包括括号)。单击(灰色)行时,行为应相当于单击 V 形图标(展开/折叠子项)。从视觉角度来看,这就是我想要做的:

在此输入图像描述

可以在此处找到此示例的组件及其源代码。

经过一些研究后,我发现实现我想要使用此组件执行的操作的唯一方法是将 groupProps 传递给 GroupedList 组件,如下所示:

public render(): JSX.Element {
return (
  <FocusZone>
    <SelectionZone selection={this._selection} selectionMode={SelectionMode.multiple}>
      <GroupedList
        items={_items}
        onRenderCell={this._onRenderCell}
        selection={this._selection}
        selectionMode={SelectionMode.multiple}
        groups={_groups}
        // adding this overrides the default header render 
        groupProps={{
            onRenderHeader: this._onRenderHeader
          }}
      />
    </SelectionZone>
  </FocusZone>
);}


private _onRenderHeader(props: IGroupDividerProps): JSX.Element {
// code to change the header goes here
return (
    <div className={css('ms-GroupedListExample-header', FontClassNames.xLarge)}>
        Group1
    </div>
);}
Run Code Online (Sandbox Code Playgroud)

我只是不知道在 _onRenderHeader 中写什么来更改标题,使其看起来和行为就像我在图片中描述的那样。非常感谢您的帮助。

Vad*_*hev 5

我过去做过类似的事情,这里是如何自定义GroupedList组件中标头的建议列表。

首先,您走在正确的轨道上,onRenderHeader事件是这种定制的正确切入点。但我建议自定义现有组件,而不是覆盖现有的标头GroupHeader标记

private _onRenderHeader(props: IGroupDividerProps): JSX.Element {

    //your changes goes here..

    return (
            <GroupHeader  {...props} />
    );
}
Run Code Online (Sandbox Code Playgroud)

隐藏检查按钮和计数器信息,请设置display:noneacheckheaderCount类名称,如下所示:

const headerCountStyle = {"display":"none"};
const checkButtonStyle = {"display":"none"};

<GroupHeader styles={{ "check": checkButtonStyle, "headerCount": headerCountStyle }}    {...props}   />
Run Code Online (Sandbox Code Playgroud)

要为组标题添加切换/折叠行为,请注册以下事件:

 const onToggleSelectGroup = () => {
        props.onToggleCollapse!(props.group!);
 }


<GroupHeader  {...props}  onToggleSelectGroup={onToggleSelectGroup} />
Run Code Online (Sandbox Code Playgroud)

这是从原始示例分叉出来的演示