Skip to content

Commit

Permalink
Added functionality to add selected users to peoplepicker control on …
Browse files Browse the repository at this point in the history
…initial load
  • Loading branch information
AsishP committed Jul 29, 2018
1 parent e0bba1e commit d5c4f45
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/controls/peoplepicker/IPeoplePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export interface IPeoplePickerProps {
* Class Name for the Error Section
*/
errorMessageclassName?: string;
/**
* Default Selected User Emails
*/
defaultSelectedUsers? : string[];
}

export interface IPeoplePickerState {
Expand Down
64 changes: 61 additions & 3 deletions src/controls/peoplepicker/PeoplePickerComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'office-ui-fabric-react/lib/Utilities';
import { IUsers } from './IUsers';
import { Label } from 'office-ui-fabric-react/lib/Label';
import { select } from '../../../node_modules/glamor';

const suggestionProps: IBasePickerSuggestionsProps = {
suggestionsHeaderText: 'Suggested People',
Expand Down Expand Up @@ -130,6 +131,19 @@ export class PeoplePicker extends React.Component<IPeoplePickerProps, IPeoplePic
}
}

//Set Default selected persons
var defaultUsers : any = [];
var defaultPeopleList: IPersonaWithMenu[] = [];
if(this.props.defaultSelectedUsers != null)
{
defaultUsers = this.getDefaultUsers(userValuesArray, this.props.defaultSelectedUsers);
defaultUsers.forEach((persona: IPersonaProps) => {
let selectedPeople: IPersonaWithMenu = {};
assign(selectedPeople, persona);
defaultPeopleList.push(selectedPeople);
});
}

let personaList: IPersonaWithMenu[] = [];
for (const persona of userValuesArray) {
let personaWithMenu: IPersonaWithMenu = {};
Expand All @@ -140,13 +154,17 @@ export class PeoplePicker extends React.Component<IPeoplePickerProps, IPeoplePic
// Update the current state
this.setState({
allPersons : userValuesArray,
selectedPersons : defaultPeopleList.length != 0 ? defaultPeopleList : [],
peoplePersonaMenu : personaList,
mostRecentlyUsedPersons : personaList.slice(0,5),
mostRecentlyUsedPersons : personaList.slice(0,5)
});

this.setState({
showmessageerror: this.props.isRequired && this.state.selectedPersons.length === 0
});
}
} catch (e) {
console.error("Error occured while fetching the users.", e);
console.error("Error occured while fetching the users and setting selected users.", e);
}
}

Expand All @@ -155,7 +173,7 @@ export class PeoplePicker extends React.Component<IPeoplePickerProps, IPeoplePic
*/
private _onPersonItemsChange = (items: any[]) => {
const { selectedItems } = this.props;

this.setState({
selectedPersons: items,
showmessageerror: items.length > 0 ? false : true
Expand Down Expand Up @@ -266,6 +284,45 @@ private _filterPersons(filterText: string): IPersonaProps[] {
return personas.filter(item => item.primaryText === persona.primaryText).length > 0;
}

/**
* Gets the default users based on the provided email address.
* Adds emails that are not found with a random generated User Id
*
* @param userValuesArray
* @param selectedUsers
*/
private getDefaultUsers(userValuesArray : any[], selectedUsers : string[]) : any {
var defaultuserValuesArray: any[] = [];
for(let i=0; i < selectedUsers.length; i++)
{
var obj = {valToCompare : selectedUsers[i] }
var length = defaultuserValuesArray.length;
defaultuserValuesArray = defaultuserValuesArray.length != 0 ? defaultuserValuesArray.concat(userValuesArray.filter(this.filterUsers, obj)) : userValuesArray.filter(this.filterUsers, obj);
if(length === defaultuserValuesArray.length)
{
var defaultUnknownUser = [{
id: 1000 + i, //just a random number
imageUrl: "",
imageInitials: "",
primaryText: selectedUsers[i] , //Name
secondaryText: selectedUsers[i], //Role
tertiaryText: "", //status
optionalText: "" //stgring
}]
defaultuserValuesArray = defaultuserValuesArray.length != 0 ? defaultuserValuesArray.concat(defaultUnknownUser) : defaultUnknownUser;
}
}
return defaultuserValuesArray;
}

/**
* Filters Users based on email
*/
private filterUsers = function(value : any, index : number, ar : any[]){
if(value.secondaryText.toLowerCase().indexOf(this.valToCompare.toLowerCase()) !== -1)
return value;
}

/**
* Default React component render method
*/
Expand All @@ -285,6 +342,7 @@ private _filterPersons(filterText: string): IPersonaProps[] {
inputProps={{
'aria-label': 'People Picker'
}}
selectedItems={this.state.selectedPersons}
itemLimit={this.props.personSelectionLimit || 1}
disabled={this.props.disabled}
onChange={this._onPersonItemsChange} />
Expand Down
1 change: 1 addition & 0 deletions src/webparts/controlsTest/components/ControlsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
// groupName={"Team Site Owners"}
showtooltip={true}
isRequired={true}
defaultSelectedUsers = {["tenantUser@domain.onmicrosoft.com", "test@user.com"]}
selectedItems={this._getPeoplePickerItems} />

<PeoplePicker
Expand Down

0 comments on commit d5c4f45

Please sign in to comment.