|
| 1 | +# ListView: Add a contextual menu |
| 2 | + |
| 3 | +## The ContextualMenu component |
| 4 | + |
| 5 | +In order to create a contextual menu for your list view, you first need to create a new component which will use a combination of an [IconButton](https://developer.microsoft.com/en-us/fabric#/components/button#Variants) and [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) controls from the Office UI Fabric React. |
| 6 | + |
| 7 | +Here is some sample code: |
| 8 | + |
| 9 | +```TypeScript |
| 10 | +import * as React from 'react'; |
| 11 | +import { Layer, IconButton, IButtonProps } from 'office-ui-fabric-react'; |
| 12 | +import { ContextualMenuItemType } from 'office-ui-fabric-react/lib/ContextualMenu'; |
| 13 | +// The following are project specific components |
| 14 | +import { IECBProps } from './IECBProps'; |
| 15 | +import styles from './ECB.module.scss'; |
| 16 | +import { IListitem } from '../../model/IListitem'; |
| 17 | + |
| 18 | +export class ECB extends React.Component<IECBProps, {}> { |
| 19 | + |
| 20 | + public constructor(props: IECBProps) { |
| 21 | + super(props); |
| 22 | + |
| 23 | + this.state = { |
| 24 | + panelOpen: false |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + public render() { |
| 29 | + return ( |
| 30 | + <div className={styles.ecb}> |
| 31 | + <IconButton id='ContextualMenuButton1' |
| 32 | + className={styles.ecbbutton} |
| 33 | + text='' |
| 34 | + width='30' |
| 35 | + split={false} |
| 36 | + iconProps={ { iconName: 'MoreVertical' } } |
| 37 | + menuIconProps={ { iconName: '' } } |
| 38 | + menuProps={{ |
| 39 | + shouldFocusOnMount: true, |
| 40 | + items: [ |
| 41 | + { |
| 42 | + key: 'action1', |
| 43 | + name: 'Action 1', |
| 44 | + onClick: this.handleClick.bind(this, this.props.item.Firstname + ' Action 1') |
| 45 | + }, |
| 46 | + { |
| 47 | + key: 'divider_1', |
| 48 | + itemType: ContextualMenuItemType.Divider |
| 49 | + }, |
| 50 | + { |
| 51 | + key: 'action2', |
| 52 | + name: 'Action 2', |
| 53 | + onClick: this.handleClick.bind(this, this.props.item.Firstname + ' Action 2') |
| 54 | + }, |
| 55 | + { |
| 56 | + key: 'action3', |
| 57 | + name: 'Action 3', |
| 58 | + onClick: this.handleClick.bind(this, this.props.item.Lastname + ' Action 3') |
| 59 | + }, |
| 60 | + { |
| 61 | + key: 'disabled', |
| 62 | + name: 'Disabled action', |
| 63 | + disabled: true, |
| 64 | + onClick: () => console.error('Disabled action should not be clickable.') |
| 65 | + } |
| 66 | + ] |
| 67 | + }} /> |
| 68 | + </div> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + private handleClick(source:string, event) { |
| 73 | + alert(`${source} clicked`); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## The ListView column |
| 79 | + |
| 80 | +Once the ECB component is created, you can add the contextual menu to the `ListView` control. In order to do this, you have to insert another `Viewfield` in code at the position of our choice. For instance after the `Lastname`: |
| 81 | + |
| 82 | +```TypeScript |
| 83 | +{ |
| 84 | + name: "", |
| 85 | + sorting: false, |
| 86 | + maxWidth: 40, |
| 87 | + render: (rowitem: IListitem) => { |
| 88 | + const element:React.ReactElement<IECBProps> = React.createElement( |
| 89 | + ECB, |
| 90 | + { |
| 91 | + item: rowitem |
| 92 | + } |
| 93 | + ); |
| 94 | + return element; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Inside the render method of the `IViewField`, the ECB component gets created and the current item will be used as a reference for the clicked row. |
| 100 | + |
| 101 | +## The result |
| 102 | +The result will look like the following: |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +Once you click on an action, you will see the alert: |
| 107 | + |
| 108 | + |
0 commit comments