import * as React from 'react'; import { PrimaryButton } from 'office-ui-fabric-react/lib/Button'; import { IPersonaProps, IPersona } from 'office-ui-fabric-react/lib/Persona'; import { people } from '@uifabric/example-data'; import { SelectedPeopleList, 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[]; } export class SelectedPeopleListWithEditExample extends React.Component<{}, IPeopleSelectedItemsListExampleState> { // Used to resolve suggestions on the editableItem private model = new ExampleSuggestionsModel(people); private suggestionsStore = new SuggestionsStore(); /** * 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) => ( ), }), }); constructor(props: {}) { super(props); this.state = { currentSelectedItems: [people[40]], }; } public render(): JSX.Element { return (
Right click any persona to edit it
{this._renderExtendedPicker()}
); } private _renderExtendedPicker(): JSX.Element { return (
); } 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] }); }); }; } type IBaseExampleType = { text?: string; name?: string; }; class ExampleSuggestionsModel { private suggestionsData: T[]; public constructor(data: T[]) { this.suggestionsData = [...data]; } public resolveSuggestions = (filterText: string, currentItems?: T[]): Promise => { 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 { return new Promise(resolve => setTimeout(() => resolve(results), 150)); } }