-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathSelectedPeopleList.WithEditInContextMenu.Example.tsx
197 lines (173 loc) · 5.87 KB
/
SelectedPeopleList.WithEditInContextMenu.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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,
TriggerOnContextMenu,
ItemWithContextMenu,
EditableItem,
DefaultEditingItem,
EditingItemInnerFloatingPickerProps,
} from '@fluentui/react-experiments/lib/SelectedItemsList';
import { FloatingPeopleSuggestions } from '@fluentui/react-experiments/lib/FloatingPeopleSuggestions';
import { SuggestionsStore } from '@fluentui/react-experiments/lib/FloatingSuggestions';
export interface IPeopleSelectedItemsListExampleState {
currentSelectedItems: IPersonaProps[];
}
export class SelectedPeopleListWithEditInContextMenuExample extends React.Component<
{},
IPeopleSelectedItemsListExampleState
> {
// Used to resolve suggestions on the editableItem
private model = new ExampleSuggestionsModel<IPersonaProps>(people);
private suggestionsStore = new SuggestionsStore<IPersonaProps>();
/**
* Build a custom selected item capable of being edited with a dropdown and capable of editing
*/
private EditableItemWithContextMenu = EditableItem({
editingItemComponent: DefaultEditingItem({
getEditingItemText: (persona: IPersonaProps) => persona.text || '',
onRenderFloatingPicker: (props: EditingItemInnerFloatingPickerProps<IPersonaProps>) => (
<FloatingPeopleSuggestions
{...props}
suggestionsStore={this.suggestionsStore}
onResolveSuggestions={this.model.resolveSuggestions}
/>
),
}),
itemComponent: ItemWithContextMenu<IPersona>({
menuItems: (item, onTrigger) => [
{
key: 'remove',
text: 'Remove',
onClick: () => {
this._onItemsRemoved([item]);
},
},
{
key: 'copy',
text: 'Copy',
onClick: () => {
this._copyToClipboard(this._getCopyItemsText([item]));
},
},
{
key: 'edit',
text: 'Edit',
onClick: () => onTrigger && onTrigger(),
},
],
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.EditableItemWithContextMenu}
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;
}
}
type IBaseExampleType = {
text?: string;
name?: string;
};
class ExampleSuggestionsModel<T extends IBaseExampleType> {
private suggestionsData: T[];
public constructor(data: T[]) {
this.suggestionsData = [...data];
}
public resolveSuggestions = (filterText: string, currentItems?: T[]): Promise<T[]> => {
let filteredItems: T[] = [];
if (filterText) {
filteredItems = this._filterItemsByText(filterText);
filteredItems = this._removeDuplicates(filteredItems, currentItems || []);
}
return this._convertResultsToPromise(filteredItems);
};
public removeSuggestion(item: T) {
const index = this.suggestionsData.indexOf(item);
console.log('removing', item, 'at', index);
if (index !== -1) {
this.suggestionsData.splice(index, 1);
}
}
private _filterItemsByText(filterText: string): T[] {
return this.suggestionsData.filter((item: T) => {
const itemText = item.text || item.name;
return itemText ? this._doesTextStartWith(itemText, filterText) : false;
});
}
private _doesTextStartWith(text: string, filterText: string): boolean {
return text.toLowerCase().indexOf(filterText.toLowerCase()) === 0;
}
private _removeDuplicates(items: T[], possibleDupes: T[]): T[] {
return items.filter((item: T) => !this._listContainsItem(item, possibleDupes));
}
private _listContainsItem(item: T, Items: T[]): boolean {
if (!Items || !Items.length || Items.length === 0) {
return false;
}
return Items.filter((i: T) => (i.text || i.name) === (item.text || item.name)).length > 0;
}
private _convertResultsToPromise(results: T[]): Promise<T[]> {
return new Promise<T[]>(resolve => setTimeout(() => resolve(results), 150));
}
}