Skip to content

Added custom filter to PeoplePicker selection #1673

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

Merged
merged 2 commits into from
Jan 8, 2024
Merged
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
18 changes: 11 additions & 7 deletions src/controls/peoplepicker/IPeoplePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IPeoplePickerProps {
context: BaseComponentContext;
/**
* Text of the Control
*/
*/
titleText?: string;
/**
* Web Absolute Url of source site. When this is provided, a search request is done to the local site.
Expand All @@ -32,7 +32,7 @@ export interface IPeoplePickerProps {
/**
* Id of SharePoint Group (Number) or Office365 Group (String)
*/
groupId?: number | string | (string|number)[];
groupId?: number | string | (string | number)[];
/**
* Maximum number of suggestions to show in the full suggestion list. (default: 5)
*/
Expand Down Expand Up @@ -79,7 +79,7 @@ export interface IPeoplePickerProps {
/**
* Prop to validate contents on blur
*/
validateOnFocusOut?: boolean;
validateOnFocusOut?: boolean;
/**
* Method to check value of People Picker text
*/
Expand All @@ -93,8 +93,8 @@ export interface IPeoplePickerProps {
*/
tooltipDirectional?: DirectionalHint;
/**
* Class Name for the whole People picker control
*/
* Class Name for the whole People picker control
*/
peoplePickerWPclassName?: string;
/**
* Class Name for the People picker control
Expand Down Expand Up @@ -129,10 +129,14 @@ export interface IPeoplePickerProps {
* Placeholder to be displayed in an empty term picker
*/
placeholder?: string;
/**
/**
* styles to apply on control
*/
styles?: Partial<IBasePickerStyles>;
styles?: Partial<IBasePickerStyles>;
/**
* Define a filter to be applied to the search results, such as a filter to only show users from a specific domain
*/
resultFilter?: (result: IPersonaProps[]) => IPersonaProps[];
}

export interface IPeoplePickerState {
Expand Down
8 changes: 7 additions & 1 deletion src/controls/peoplepicker/PeoplePickerComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ export class PeoplePicker extends React.Component<IPeoplePickerProps, IPeoplePic
const results = await this.peopleSearchService.searchPeople(searchText, this.suggestionsLimit, this.props.principalTypes, this.props.webAbsoluteUrl, this.groupId, this.props.ensureUser, this.props.allowUnvalidated);
// Remove duplicates
const { selectedPersons, mostRecentlyUsedPersons } = this.state;
const filteredPersons = this.removeDuplicates(results, selectedPersons);
let filteredPersons = this.removeDuplicates(results, selectedPersons);

// If a resultFilter is provided apply the filter to the results
if (this.props.resultFilter !== undefined && filteredPersons.length > 0) {
filteredPersons = this.props.resultFilter(filteredPersons);
}

// Add the users to the most recently used ones
let recentlyUsed = [...filteredPersons, ...mostRecentlyUsedPersons];
recentlyUsed = uniqBy(recentlyUsed, "text");
Expand Down
12 changes: 10 additions & 2 deletions src/webparts/controlsTest/components/ControlsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ import { TestControl } from "./TestControl";
import { UploadFiles } from "../../../controls/uploadFiles";
import { IFileInfo } from "@pnp/sp/files";
import { FieldPicker } from "../../../FieldPicker";
import { Toggle } from "@fluentui/react";
import { IPersonaProps, Toggle } from "@fluentui/react";
import { ListItemComments } from "../../../ListItemComments";
import { ViewPicker } from "../../../controls/viewPicker";

Expand Down Expand Up @@ -1321,9 +1321,17 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
personSelectionLimit={5}
ensureUser={true}
principalTypes={[PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.SecurityGroup, PrincipalType.DistributionList]}

onChange={this._getPeoplePickerItems} />

<PeoplePicker context={this.props.context}
titleText="People Picker with filter for '.com'"
personSelectionLimit={5}
ensureUser={true}
principalTypes={[PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.SecurityGroup, PrincipalType.DistributionList]}
resultFilter={(result: IPersonaProps[]) => {
return result.filter(p => p["loginName"].indexOf(".com") !== -1);
}}
onChange={this._getPeoplePickerItems} />

<PeoplePicker context={this.props.context}
titleText="People Picker (Group not found)"
Expand Down