Skip to content

Commit 4b0bec5

Browse files
committed
support defaultSelectedUser change
1 parent 056c6d9 commit 4b0bec5

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/controls/listItemPicker/IListItemPickerState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export interface IListItemPickerState {
33
showError: boolean;
44
errorMessage: string;
55
suggestionsHeaderText:string;
6+
selectedItems?: any[];
67
}

src/controls/listItemPicker/ListItemPicker.tsx

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { TagPicker } from "office-ui-fabric-react/lib/components/pickers/TagPick
55
import { Label } from "office-ui-fabric-react/lib/Label";
66
import { IListItemPickerProps, IListItemPickerState } from ".";
77
import * as telemetry from '../../common/telemetry';
8+
import isEqual from 'lodash/isEqual';
89

910

1011
export class ListItemPicker extends React.Component<IListItemPickerProps, IListItemPickerState> {
1112
private _spservice: SPservice;
12-
private selectedItems: any[];
1313

1414
constructor(props: IListItemPickerProps) {
1515
super(props);
@@ -21,37 +21,50 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
2121
noresultsFoundText: !this.props.noResultsFoundText ? strings.genericNoResultsFoundText : this.props.noResultsFoundText,
2222
showError: false,
2323
errorMessage: "",
24-
suggestionsHeaderText: !this.props.suggestionsHeaderText ? strings.ListItemPickerSelectValue : this.props.suggestionsHeaderText
24+
suggestionsHeaderText: !this.props.suggestionsHeaderText ? strings.ListItemPickerSelectValue : this.props.suggestionsHeaderText,
25+
selectedItems: props.defaultSelectedItems || []
2526
};
2627

2728
// Get SPService Factory
2829
this._spservice = new SPservice(this.props.context);
29-
30-
this.selectedItems = [];
3130
}
3231

33-
public componentDidUpdate(prevProps: IListItemPickerProps, prevState: IListItemPickerState): void {
34-
if (this.props.listId !== prevProps.listId) {
35-
this.selectedItems = [];
32+
public componentWillReceiveProps(nextProps: IListItemPickerProps) {
33+
let newSelectedItems: any[] | undefined;
34+
if (this.props.listId !== nextProps.listId) {
35+
newSelectedItems = [];
36+
}
37+
if (!isEqual(this.props.defaultSelectedItems, nextProps.defaultSelectedItems)) {
38+
newSelectedItems = nextProps.defaultSelectedItems;
3639
}
40+
41+
this.setState({
42+
selectedItems: newSelectedItems
43+
});
3744
}
3845

3946
/**
4047
* Render the field
4148
*/
4249
public render(): React.ReactElement<IListItemPickerProps> {
4350
const { className, disabled, itemLimit, placeholder } = this.props;
51+
const {
52+
suggestionsHeaderText,
53+
noresultsFoundText,
54+
errorMessage,
55+
selectedItems
56+
} = this.state;
4457

4558
return (
4659
<div>
4760
<TagPicker onResolveSuggestions={this.onFilterChanged}
4861
// getTextFromItem={(item: any) => { return item.name; }}
4962
getTextFromItem={this.getTextFromItem}
5063
pickerSuggestionsProps={{
51-
suggestionsHeaderText: this.state.suggestionsHeaderText,
52-
noResultsFoundText: this.state.noresultsFoundText
64+
suggestionsHeaderText: suggestionsHeaderText,
65+
noResultsFoundText: noresultsFoundText
5366
}}
54-
defaultSelectedItems={this.props.defaultSelectedItems || []}
67+
selectedItems={selectedItems}
5568
onChange={this.onItemChanged}
5669
className={className}
5770
itemLimit={itemLimit}
@@ -60,8 +73,8 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
6073
placeholder: placeholder
6174
}} />
6275

63-
{!!this.state.errorMessage &&
64-
(<Label style={{color:'#FF0000'}}> {this.state.errorMessage} </Label>)}
76+
{!!errorMessage &&
77+
(<Label style={{color:'#FF0000'}}> {errorMessage} </Label>)}
6578
</div>
6679
);
6780
}
@@ -77,7 +90,9 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
7790
* On Selected Item
7891
*/
7992
private onItemChanged = (selectedItems: { key: string; name: string }[]): void => {
80-
this.selectedItems = selectedItems;
93+
this.setState({
94+
selectedItems: selectedItems
95+
});
8196
this.props.onSelectedItem(selectedItems);
8297
}
8398

@@ -87,7 +102,9 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
87102
private onFilterChanged = async (filterText: string, tagList: { key: string; name: string }[]) => {
88103
let resolvedSugestions: { key: string; name: string }[] = await this.loadListItems(filterText);
89104

90-
const selectedItems = [...this.selectedItems, ...(this.props.defaultSelectedItems || [])];
105+
const {
106+
selectedItems
107+
} = this.state;
91108

92109
// Filter out the already retrieved items, so that they cannot be selected again
93110
if (selectedItems && selectedItems.length > 0) {

0 commit comments

Comments
 (0)