-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
SelectedPeopleList.WithEdit.Example.tsx
144 lines (123 loc) · 4.58 KB
/
SelectedPeopleList.WithEdit.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
import * as React from 'react';
import { PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { Selection } from 'office-ui-fabric-react/lib/Selection';
import { IPersonaProps } from 'office-ui-fabric-react/lib/Persona';
import { people } from '@uifabric/example-data';
import {
SelectedPeopleList,
ISelectedPeopleList,
SelectedPersona,
TriggerOnContextMenu,
EditableItem,
DefaultEditingItem,
EditingItemInnerFloatingPickerProps
} from '@uifabric/experiments/lib/SelectedItemsList';
import { FloatingPeopleSuggestions } from '@uifabric/experiments/lib/FloatingPeopleSuggestions';
import { SuggestionsStore } from '@uifabric/experiments/lib/FloatingSuggestions';
export interface IPeopleSelectedItemsListExampleState {
currentSelectedItems: IPersonaProps[];
controlledComponent: boolean;
}
export class SelectedPeopleListWithEditExample extends React.Component<{}, IPeopleSelectedItemsListExampleState> {
private _selectionList: ISelectedPeopleList;
private selection: Selection = new Selection({ onSelectionChanged: () => this._onSelectionChange() });
// 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 when the item is right clicked
*/
private SelectedItem = EditableItem({
itemComponent: TriggerOnContextMenu(SelectedPersona),
editingItemComponent: DefaultEditingItem({
getEditingItemText: persona => persona.text || '',
onRenderFloatingPicker: (props: EditingItemInnerFloatingPickerProps<IPersonaProps>) => (
<FloatingPeopleSuggestions
{...props}
suggestionsStore={this.suggestionsStore}
onResolveSuggestions={this.model.resolveSuggestions}
/>
)
})
});
public render(): JSX.Element {
return (
<div className={'ms-BasePicker-text'}>
Right click any persona to edit it
<br />
<PrimaryButton text="Add another item" onClick={this._onAddItemButtonClicked} />
{this._renderExtendedPicker()}
</div>
);
}
private _renderExtendedPicker(): JSX.Element {
return (
<div>
<SelectedPeopleList
key={'normal'}
ref={this._setComponentRef}
removeButtonAriaLabel={'Remove'}
defaultSelectedItems={[people[40]]}
selection={this.selection}
onRenderItem={this.SelectedItem}
/>
</div>
);
}
private _setComponentRef = (component: ISelectedPeopleList): void => {
this._selectionList = component;
};
private _onAddItemButtonClicked = (): void => {
const randomPerson = people[Math.floor(Math.random() * (people.length - 1))];
this._selectionList.addItems([randomPerson]);
};
private _onSelectionChange(): void {
this.forceUpdate();
}
}
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));
}
}