Skip to content

Commit a2e97d5

Browse files
Merge pull request #1673 from GuidoZam/peoplepicker-resultFilter
Added custom filter to PeoplePicker selection
2 parents 0314111 + 286854e commit a2e97d5

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/controls/peoplepicker/IPeoplePicker.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface IPeoplePickerProps {
1515
context: BaseComponentContext;
1616
/**
1717
* Text of the Control
18-
*/
18+
*/
1919
titleText?: string;
2020
/**
2121
* Web Absolute Url of source site. When this is provided, a search request is done to the local site.
@@ -32,7 +32,7 @@ export interface IPeoplePickerProps {
3232
/**
3333
* Id of SharePoint Group (Number) or Office365 Group (String)
3434
*/
35-
groupId?: number | string | (string|number)[];
35+
groupId?: number | string | (string | number)[];
3636
/**
3737
* Maximum number of suggestions to show in the full suggestion list. (default: 5)
3838
*/
@@ -79,7 +79,7 @@ export interface IPeoplePickerProps {
7979
/**
8080
* Prop to validate contents on blur
8181
*/
82-
validateOnFocusOut?: boolean;
82+
validateOnFocusOut?: boolean;
8383
/**
8484
* Method to check value of People Picker text
8585
*/
@@ -93,8 +93,8 @@ export interface IPeoplePickerProps {
9393
*/
9494
tooltipDirectional?: DirectionalHint;
9595
/**
96-
* Class Name for the whole People picker control
97-
*/
96+
* Class Name for the whole People picker control
97+
*/
9898
peoplePickerWPclassName?: string;
9999
/**
100100
* Class Name for the People picker control
@@ -129,10 +129,14 @@ export interface IPeoplePickerProps {
129129
* Placeholder to be displayed in an empty term picker
130130
*/
131131
placeholder?: string;
132-
/**
132+
/**
133133
* styles to apply on control
134134
*/
135-
styles?: Partial<IBasePickerStyles>;
135+
styles?: Partial<IBasePickerStyles>;
136+
/**
137+
* Define a filter to be applied to the search results, such as a filter to only show users from a specific domain
138+
*/
139+
resultFilter?: (result: IPersonaProps[]) => IPersonaProps[];
136140
}
137141

138142
export interface IPeoplePickerState {

src/controls/peoplepicker/PeoplePickerComponent.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ export class PeoplePicker extends React.Component<IPeoplePickerProps, IPeoplePic
142142
const results = await this.peopleSearchService.searchPeople(searchText, this.suggestionsLimit, this.props.principalTypes, this.props.webAbsoluteUrl, this.groupId, this.props.ensureUser, this.props.allowUnvalidated);
143143
// Remove duplicates
144144
const { selectedPersons, mostRecentlyUsedPersons } = this.state;
145-
const filteredPersons = this.removeDuplicates(results, selectedPersons);
145+
let filteredPersons = this.removeDuplicates(results, selectedPersons);
146+
147+
// If a resultFilter is provided apply the filter to the results
148+
if (this.props.resultFilter !== undefined && filteredPersons.length > 0) {
149+
filteredPersons = this.props.resultFilter(filteredPersons);
150+
}
151+
146152
// Add the users to the most recently used ones
147153
let recentlyUsed = [...filteredPersons, ...mostRecentlyUsedPersons];
148154
recentlyUsed = uniqBy(recentlyUsed, "text");

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ import { TestControl } from "./TestControl";
195195
import { UploadFiles } from "../../../controls/uploadFiles";
196196
import { IFileInfo } from "@pnp/sp/files";
197197
import { FieldPicker } from "../../../FieldPicker";
198-
import { Toggle } from "@fluentui/react";
198+
import { IPersonaProps, Toggle } from "@fluentui/react";
199199
import { ListItemComments } from "../../../ListItemComments";
200200
import { ViewPicker } from "../../../controls/viewPicker";
201201

@@ -1321,9 +1321,17 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
13211321
personSelectionLimit={5}
13221322
ensureUser={true}
13231323
principalTypes={[PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.SecurityGroup, PrincipalType.DistributionList]}
1324-
13251324
onChange={this._getPeoplePickerItems} />
13261325

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

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

0 commit comments

Comments
 (0)