Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not review] Enabling selection for input changed for UnifiedPicker and updating example for that case #15412

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const UnifiedPicker = <T extends {}>(props: IUnifiedPickerProps<T>): JSX.

const rootRef = React.createRef<HTMLDivElement>();
const input = React.useRef<Autofill>(null);
const { setQueryString } = useQueryString('');
const { setQueryString, clearQueryString } = useQueryString('');
const [selection, setSelection] = React.useState(new Selection({ onSelectionChanged: () => _onSelectionChanged() }));
const [focusedItemIndices, setFocusedItemIndices] = React.useState(selection.getSelectedIndices() || []);
const { suggestions, selectedSuggestionIndex, isSuggestionsVisible } = props.floatingSuggestionProps;
Expand Down Expand Up @@ -270,12 +270,25 @@ export const UnifiedPicker = <T extends {}>(props: IUnifiedPickerProps<T>): JSX.
props.inputProps.onClick(ev as React.MouseEvent<HTMLInputElement>);
}
};
const _onInputChange = (value: string, composing?: boolean) => {
const _onInputChange = (value: string, composing?: boolean, resultItemsList?: T[]) => {
Copy link
Contributor

@elisabethcvs elisabethcvs Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this (resultItemList) ever have a value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would, this is what is getting updated

if (!composing) {
// update query string
setQueryString(value);
!isSuggestionsShown ? showPicker(true) : null;
onInputChange ? onInputChange(value) : null;
if (!resultItemsList) {
resultItemsList = [];
}
if (onInputChange) {
onInputChange(value, composing, resultItemsList);
clearQueryString();
if (resultItemsList && resultItemsList.length > 0) {
addItems(resultItemsList);
showPicker(false);
if (input.current) {
input.current._updateValue('', composing);
}
}
}
}
};
const _onPaste = (ev: React.ClipboardEvent<Autofill | HTMLInputElement>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface IUnifiedPickerProps<T> {
/**
* On input value change
*/
onInputChange?: (filter: string) => void;
onInputChange?: (filter: string, composing?: boolean, resultItemsList?: T[]) => void;

/**
* Drag drop events callback interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const UnifiedPeoplePickerExample = (): JSX.Element => {
..._suggestions,
]);

const ref = React.useRef<any>();

const [peopleSelectedItems, setPeopleSelectedItems] = React.useState<IPersonaProps[]>([]);

const _onSuggestionSelected = (
Expand Down Expand Up @@ -102,26 +104,6 @@ const UnifiedPeoplePickerExample = (): JSX.Element => {
return copyText;
};

const _onPaste = (pastedValue: string, selectedItemsList: IPersonaProps[]): void => {
// Find the suggestion corresponding to the specific text name
// and update the selectedItemsList to re-render everything.
const newList: IPersonaProps[] = [];
if (pastedValue !== null) {
pastedValue.split(',').forEach(textValue => {
if (textValue) {
people.forEach(suggestionItem => {
if (suggestionItem.text === textValue) {
selectedItemsList.push(suggestionItem);
newList.push(suggestionItem);
}
});
}
});
}

setPeopleSelectedItems(prevPeopleSelectedItems => [...prevPeopleSelectedItems, ...newList]);
};

const _serializeItemsForDrag = (items: IPersonaProps[]): string => {
return _getItemsCopyText(items);
};
Expand Down Expand Up @@ -179,14 +161,54 @@ const UnifiedPeoplePickerExample = (): JSX.Element => {
setPeopleSelectedItems(updatedItems);
};

const _onInputChange = (filterText: string): void => {
const SEPARATOR_REGEX = new RegExp(';|,|\\n|\u000A', 'g');
const splitPasteInputIntoRecipientStrings = (inputText: string) =>
inputText
.split(SEPARATOR_REGEX)
.map((token: string) => token.trim())
.filter((token: string) => !!token);

const _onInputChange = (filterText: string, composing?: boolean, resultItemsList?: IPersonaProps[]): void => {
const allPeople = people;

const lastCharIndex = filterText.length - 1;
const lastChar = filterText[lastCharIndex];
const suggestions = allPeople.filter((item: IPersonaProps) => _startsWith(item.text || '', filterText));
const suggestionList = suggestions.map(item => {
return { item: item, isSelected: false, key: item.key } as IFloatingSuggestionItem<IPersonaProps>;
});
// We want to show top 5 results
setPeopleSuggestions(suggestionList.splice(0, 5));

const updatedItems: IPersonaProps[] = [];
const currentItems: IPersonaProps[] = [...peopleSelectedItems];

for (let i = 0; i < currentItems.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this, you can have the declaration be this right?
const updatedItems: IPersonaProps[] = [...peopleSelectedItems];

const item = currentItems[i];
updatedItems.push(item);
}

if (lastChar == ';' || lastChar == ',') {
const pastedText = splitPasteInputIntoRecipientStrings(filterText);
pastedText.forEach(function(itemText) {
let result;
const extractedText = itemText.substring(0, lastCharIndex);
// We need to do an exact match

allPeople.forEach((item: IPersonaProps) => {
if (item.text.toLowerCase() == extractedText.toLowerCase()) {
result = item;
}
});

if (result && resultItemsList) {
resultItemsList.push(result);
updatedItems.push(result);
}
setPeopleSelectedItems(updatedItems);
});
} else {
// We want to show top 5 results
setPeopleSuggestions(suggestionList.splice(0, 5));
}
};

function _startsWith(text: string, filterText: string): boolean {
Expand Down Expand Up @@ -222,13 +244,12 @@ const UnifiedPeoplePickerExample = (): JSX.Element => {
return (
<>
<UnifiedPeoplePicker
componentRef={ref}
selectedItemsListProps={selectedPeopleListProps}
floatingSuggestionProps={floatingPeoplePickerProps}
inputProps={inputProps}
// eslint-disable-next-line react/jsx-no-bind
onInputChange={_onInputChange}
// eslint-disable-next-line react/jsx-no-bind
onPaste={_onPaste}
customClipboardType="recipients"
/>
</>
Expand Down