Skip to content

Commit faf2780

Browse files
committed
Added custom filter to PeoplePicker selection
1 parent deeaefe commit faf2780

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
*/
@@ -75,7 +75,7 @@ export interface IPeoplePickerProps {
7575
/**
7676
* Prop to validate contents on blur
7777
*/
78-
validateOnFocusOut?: boolean;
78+
validateOnFocusOut?: boolean;
7979
/**
8080
* Method to check value of People Picker text
8181
*/
@@ -89,8 +89,8 @@ export interface IPeoplePickerProps {
8989
*/
9090
tooltipDirectional?: DirectionalHint;
9191
/**
92-
* Class Name for the whole People picker control
93-
*/
92+
* Class Name for the whole People picker control
93+
*/
9494
peoplePickerWPclassName?: string;
9595
/**
9696
* Class Name for the People picker control
@@ -125,10 +125,14 @@ export interface IPeoplePickerProps {
125125
* Placeholder to be displayed in an empty term picker
126126
*/
127127
placeholder?: string;
128-
/**
128+
/**
129129
* styles to apply on control
130130
*/
131-
styles?: Partial<IBasePickerStyles>;
131+
styles?: Partial<IBasePickerStyles>;
132+
/**
133+
* Define a filter to be applied to the search results, such as a filter to only show users from a specific domain
134+
*/
135+
resultFilter?: (result: IPersonaProps[]) => IPersonaProps[];
132136
}
133137

134138
export interface IPeoplePickerState {

src/controls/peoplepicker/PeoplePickerComponent.tsx

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

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ import { TestControl } from "./TestControl";
197197
import { UploadFiles } from "../../../controls/uploadFiles";
198198
import { IFileInfo } from "@pnp/sp/files";
199199
import { FieldPicker } from "../../../FieldPicker";
200-
import { Toggle } from "office-ui-fabric-react";
200+
import { IPersonaProps, Toggle } from "office-ui-fabric-react";
201201
import { ListItemComments } from "../../../ListItemComments";
202202
import { ViewPicker } from "../../../controls/viewPicker";
203203

@@ -1517,9 +1517,17 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
15171517
personSelectionLimit={5}
15181518
ensureUser={true}
15191519
principalTypes={[PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.SecurityGroup, PrincipalType.DistributionList]}
1520-
15211520
onChange={this._getPeoplePickerItems} />
15221521

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

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

0 commit comments

Comments
 (0)