-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathSelectedPeopleList.WithContextMenu.Example.tsx
115 lines (103 loc) · 3.07 KB
/
SelectedPeopleList.WithContextMenu.Example.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import * as React from 'react';
import { PrimaryButton } from '@fluentui/react/lib/compat/Button';
import { IPersonaProps, IPersona } from '@fluentui/react/lib/Persona';
import { people } from '@fluentui/example-data';
import {
SelectedPeopleList,
SelectedPersona,
ItemWithContextMenu,
TriggerOnContextMenu,
} from '@fluentui/react-experiments/lib/SelectedItemsList';
export interface IPeopleSelectedItemsListExampleState {
currentSelectedItems: IPersonaProps[];
}
export class SelectedPeopleListWithContextMenuExample extends React.Component<
{},
IPeopleSelectedItemsListExampleState
> {
/**
* Build a custom selected item capable of being edited with a dropdown and
* capable of editing
*/
private SelectedItem = ItemWithContextMenu<IPersona>({
menuItems: item => [
{
key: 'remove',
text: 'Remove',
onClick: () => {
this._onItemsRemoved([item]);
},
},
{
key: 'copy',
text: 'Copy',
onClick: () => {
this._copyToClipboard(this._getCopyItemsText([item]));
},
},
],
itemComponent: TriggerOnContextMenu(SelectedPersona),
});
constructor(props: {}) {
super(props);
this.state = {
currentSelectedItems: [people[40]],
};
}
public render(): JSX.Element {
return (
<div className={'ms-BasePicker-text'}>
Right click any persona to open the context menu
<br />
<PrimaryButton text="Add another item" onClick={this._onAddItemButtonClicked} />
{this._renderExtendedPicker()}
</div>
);
}
private _renderExtendedPicker(): JSX.Element {
return (
<div>
<SelectedPeopleList
key={'normal'}
removeButtonAriaLabel={'Remove'}
selectedItems={[...this.state.currentSelectedItems]}
onRenderItem={this.SelectedItem}
onItemsRemoved={this._onItemsRemoved}
/>
</div>
);
}
private _onAddItemButtonClicked = (): void => {
const randomPerson = people[Math.floor(Math.random() * (people.length - 1))];
this.setState({ currentSelectedItems: [...this.state.currentSelectedItems, randomPerson] });
};
private _onItemsRemoved = (items: IPersona[]): void => {
const currentSelectedItemsCopy = [...this.state.currentSelectedItems];
items.forEach(item => {
const indexToRemove = currentSelectedItemsCopy.indexOf(item);
currentSelectedItemsCopy.splice(indexToRemove, 1);
this.setState({ currentSelectedItems: [...currentSelectedItemsCopy] });
});
};
private _copyToClipboard = (copyString: string): void => {
navigator.clipboard.writeText(copyString).then(
() => {
/* clipboard successfully set */
},
() => {
/* clipboard write failed */
throw new Error();
},
);
};
private _getCopyItemsText(items: IPersonaProps[]): string {
let copyText = '';
items.forEach((item: IPersonaProps, index: number) => {
copyText += item.text;
if (index < items.length - 1) {
copyText += ', ';
}
});
return copyText;
}
}