Skip to content

Commit 6461e31

Browse files
committed
Merge branch 'mmsharepoint-master' into dev
2 parents e62fd8a + 2b66cf0 commit 6461e31

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
13.2 KB
Loading
16.2 KB
Loading
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
![ContextualMenu_shown](../assets/ListView.ContextualMenu.png)
105+
106+
Once you click on an action, you will see the alert:
107+
108+
![ContextualMenu_clicked](../assets/ListView.ContextualMenu_clicked.png)

docs/documentation/docs/controls/ListView.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const groupByFields: IGrouping[] = [
5454
];
5555
```
5656

57+
!!! note "Extend ListView with a ContextualMenu"
58+
To extend the `ListView` control with a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) refer to [ListView.ContextualMenu](./ListView.ContextualMenu).
59+
5760
## Implementation
5861

5962
The ListView control can be configured with the following properties:

docs/documentation/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pages:
44
- Controls:
55
- FileTypeIcon: 'controls/FileTypeIcon.md'
66
- ListView: 'controls/ListView.md'
7+
- "ListView: add a contextual menu": 'controls/ListView.ContextualMenu.md'
78
- Placeholder: 'controls/Placeholder.md'
89
- SiteBreadcrumb: 'controls/SiteBreadcrumb.md'
910
- WebPartTitle: 'controls/WebPartTitle.md'

0 commit comments

Comments
 (0)