如何使OverflowSet中的按钮执行?

How to make button in OverflowSet execute?

我如何在 Office ui fabric 的 OverflowSet 中获得一个按钮来做一些事情。 https://developer.microsoft.com/en-us/fabric#/components/overflowset 中的示例具有永远不会触发的 onClick 事件。

import * as React from 'react';
import { BaseComponent, css } from 'office-ui-fabric-react/lib/Utilities';
import { CommandBarButton } from 'office-ui-fabric-react/lib/Button';
import { SearchBox } from 'office-ui-fabric-react/lib/SearchBox';
import { IOverflowSetItemProps, OverflowSet } from 'office-ui-fabric-react/lib/OverflowSet';

import * as stylesImport from './OverflowSet.Example.scss';
const styles: any = stylesImport;

export class OverflowSetCustomExample extends BaseComponent<any, any> {
  public render(): JSX.Element {
    return (
      <OverflowSet
        items={[
          {
            key: 'upload',
            name: 'Upload',
            icon: 'Upload',
            onClick: () => {
              console.log("this never happens");
            }
          }
        ]}
        overflowItems={[]}
        onRenderOverflowButton={this._onRenderOverflowButton}
        onRenderItem={this._onRenderItem}
      />
    );
  }

  private _onRenderItem(item: IOverflowSetItemProps): JSX.Element {
    if (item.onRender) {
      return item.onRender(item);
    }
    return <CommandBarButton iconProps={{ iconName: item.icon }} menuProps={item.subMenuProps} text={item.name} />;
  }

  private _onRenderOverflowButton(overflowItems: any[] | undefined): JSX.Element {
    return (
      <CommandBarButton className={css(styles.overflowButton)} menuIconProps={{ iconName: 'More' }} menuProps={{ items: overflowItems! }} />
    );
  }
}

点击事件永远不会触发。如何使用按钮?

由于您提供了 onRenderItem 回调,因此您接管了组件的默认渲染方法。因此,您必须自己直接应用 onClick 属性:

  private _onRenderItem(item: IOverflowSetItemProps): JSX.Element {
    if (item.onRender) {
      return item.onRender(item);
    }
    //
    // this now returns the button with the onClick handler applied
    //
    return <CommandBarButton onClick={item.onClick} iconProps={{ iconName: item.icon }} menuProps={item.subMenuProps} text={item.name} />;
  }