如何在Office ui架构的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! }} />
);
}
}
click事件永远不会触发。如何使用按钮?
答案 0 :(得分:1)
由于您提供了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} />;
}