Skip to content

Commit b38d90a

Browse files
committed
Merge branch 'master' of https://github.com/mmsharepoint/sp-dev-fx-controls-react into mmsharepoint-master
2 parents e62fd8a + 744ed9a commit b38d90a

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
13.2 KB
Loading
16.2 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Add a contextual menu
2+
## The ListView column
3+
To add a contextual menu to our ListView we will insert another Viewfield in the webpart code at the position of our choice, for instance after the “Lastname”:
4+
```TypeScript
5+
{
6+
name: "",
7+
sorting: false,
8+
maxWidth: 40,
9+
render: (rowitem: IListitem) => {
10+
const element:React.ReactElement<IECBProps> =React.createElement(
11+
ECB, {
12+
item:rowitem
13+
}
14+
);
15+
return element;
16+
}
17+
}
18+
```
19+
We use the render method of IViewField. Inside we create our ECB component and as a property we handover the rowitem which is clicked.
20+
We could also handover functions that shall be executed inside the context menu component but be able to be bound to the parent component, that is the webpart/component which contains the ListView.
21+
## The ContextualMenu component
22+
We now need to create the ECB component which represents our context menu. Therefore we will use a combination of an [IconButton](https://developer.microsoft.com/en-us/fabric#/components/button#Variants) and a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) from the Office UI Fabric components.
23+
The code for this additional component looks like this:
24+
```TypeScript
25+
import * as React from 'react';
26+
import { Layer, IconButton, IButtonProps } from 'office-ui-fabric-react';
27+
import { ContextualMenuItemType } from 'office-ui-fabric-react/lib/ContextualMenu';
28+
// The following are project specific components
29+
import { IECBProps } from './IECBProps';
30+
import styles from './ECB.module.scss';
31+
import { IListitem } from '../../model/IListitem';
32+
33+
export class ECB extends React.Component<IECBProps, {}> {
34+
35+
public constructor(props: IECBProps) {
36+
super(props);
37+
38+
this.state = {
39+
panelOpen: false
40+
};
41+
}
42+
43+
public render() {
44+
return <div className={styles.ecb}>
45+
<IconButton
46+
47+
id='ContextualMenuButton1'
48+
className={styles.ecbbutton}
49+
text=''
50+
width='30'
51+
split={false}
52+
iconProps={ { iconName: 'MoreVertical' } }
53+
menuIconProps={ { iconName: '' } }
54+
menuProps={ {
55+
shouldFocusOnMount: true,
56+
items: [
57+
{
58+
key: 'action1',
59+
name: 'Action 1',
60+
onClick: this.handleClick.bind(this, this.props.item.Firstname + ' Action 1')
61+
},
62+
[ {
63+
key: 'divider_1',
64+
itemType: ContextualMenuItemType.Divider
65+
},
66+
{
67+
key: 'action2',
68+
name: 'Action 2',
69+
onClick: this.handleClick.bind(this, this.props.item.Firstname + ' Action 2')
70+
},
71+
{
72+
key: 'action3',
73+
name: 'Action 3',
74+
onClick: this.handleClick.bind(this, this.props.item.Lastname + ' Action 3')
75+
},
76+
{
77+
key: 'disabled',
78+
name: 'Disabled action',
79+
disabled: true,
80+
onClick: () => console.error('Disabled action should not be clickable.')
81+
}
82+
]
83+
} }
84+
/>
85+
</div>
86+
;
87+
}
88+
89+
private handleClick(source:string, event) {
90+
alert(source + ' clicked');
91+
}
92+
}
93+
```
94+
One of the things to mention is that in the (totally simplified for demo reasons here) onClick function we hand over one attribute of the clicked item that we have in the components' properties so we can process it in the function.
95+
Another trick is to give an empty iconName for the menuIconProps. This is because once you attach a menuProps Attribute to any Kind of Office UI fabric button a ChevronDown selector on the right side of the button will occur by default.
96+
With the menuIconProps Attribute you can adjust this, that is when specifying an empty Name you can remove it. This is what we want because we only want to have our “MoreVertical” icon which are the three dots in a vertical order.
97+
To place this a bit more centric, we have small CSS manipulation as well:
98+
```SCSS
99+
.ecb {
100+
position: absolute;
101+
top: -3px;
102+
.ecbbutton div {
103+
padding-left: 12px;
104+
}
105+
}
106+
```
107+
## The result
108+
The result will look like the following:
109+
110+
![ContextualMenu_shown](../assets/ListView.ContextualMenu.png)
111+
And in action it shows which function and item was clicked:
112+
113+
![ContextualMenu_clicked](../assets/ListView.ContextualMenu_clicked.png)

docs/documentation/docs/controls/ListView.md

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

57+
## Extend with a ContextualMenu
58+
59+
To extend the ListView control with a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) refer to [ListView.ContextualMenu](./ListView.ContextualMenu.md)
60+
5761
## Implementation
5862

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

0 commit comments

Comments
 (0)