Sharepoint - SPFx(On-Prem) - Property for single choice of Office-IU-Fabric - PeoplePicker

In the office fabric UI, we have a property itemLimit that you can use to restrict the selection to a single person.

So, you need to modify it as below:

public render(): React.ReactElement<ISPPeoplePickerProps> {

    return (
      <div className={ css('ms-TextField', {'is-required': this.props.required }) }>
        <Label>{ this.props.label }</Label>
        <NormalPeoplePicker
            onChange={ this._onChangePeoplePicker }
            onResolveSuggestions={ this._onFilterChangedPeoplePicker }
            getTextFromItem={ (persona: IPersonaProps) => persona.primaryText }                
            pickerSuggestionsProps={ suggestionProps }
            itemLimit={ 1 }
            className={ 'ms-PeoplePicker' }
            key={ 'normal' } />
      </div>
    );
  }

This property was added in the version 4.37.0 which was released on 24 Aug 2017. So, ensure that you are using a version that is 4.37.0 or higher.

In your package.json file, ensure that the reference is somewhat as below:

"dependencies": {    
    //other entries
    "office-ui-fabric-react": "^4.37.0",    
  },

Reference - Office UI Fabric React


You can use onResolveSuggestions.

public render() {
const suggestionProps: IBasePickerSuggestionsProps = {
    suggestionsHeaderText: 'Suggested People',
    noResultsFoundText: 'No results found',
    loadingText: 'Loading'
};
return <CompactPeoplePicker
                onResolveSuggestions={this.onFilterChanged.bind(this)}
                getTextFromItem={(persona: IPersonaProps) => persona.primaryText}
                pickerSuggestionsProps={suggestionProps}
                className={`class_name ${this.props.className}`}
                onChange={(items) => this.onChangeItems(items)}
            />
 }

onFilterChanged(filterText: string, currentPersonas: IPersonaProps[], limitResults?: number) {
var self = this;
if (filterText && currentPersonas.length < 1) {
    return new Promise((resolve, reject) => {
            self.getUsersByFilter(filterText + "*").then((usersResult: any) => {
                var d = usersResult.d.query;
                var us;
                if (d.PrimaryQueryResult && d.PrimaryQueryResult.RelevantResults) {
                    us = d.PrimaryQueryResult.RelevantResults.Table.Rows.results.map((el, id) => {
                        var elem = this.parseSearchResults(el.Cells.results, ["PreferredName", "PictureURL", "Title", "SipAddress", "WorkEmail", "JobTitle", "WorkPhone", "MobilePhone", "Path", "BaseOfficeLocation", "OfficeNumber"]);
                        return elem;
                    });
                }
                var usersArray = us.map((el) => {
                    return {                                
                        imageUrl: `/_layouts/15/userphoto.aspx?size=S&username=${encodeURIComponent(el["WorkEmail"])}`,
                        path: el["Path"],
                        primaryText: el["PreferredName"],
                        secondaryText: el["WorkEmail"],
                        email: el["WorkEmail"],
                        officeLocation: el["BaseOfficeLocation"],
                        officeNumber: el["OfficeNumber"]
                    } as IPersonaProps
                });
                resolve(usersArray);
            }).catch((ex) => {
                console.log("reject");
                reject([])
            })
    })
} else {
    return [];
}
}

Ref: PeoplePicker Office UI Fabric

Tags:

Spfx