@@ -5,11 +5,11 @@ import { TagPicker } from "office-ui-fabric-react/lib/components/pickers/TagPick
5
5
import { Label } from "office-ui-fabric-react/lib/Label" ;
6
6
import { IListItemPickerProps , IListItemPickerState } from "." ;
7
7
import * as telemetry from '../../common/telemetry' ;
8
+ import isEqual from 'lodash/isEqual' ;
8
9
9
10
10
11
export class ListItemPicker extends React . Component < IListItemPickerProps , IListItemPickerState > {
11
12
private _spservice : SPservice ;
12
- private selectedItems : any [ ] ;
13
13
14
14
constructor ( props : IListItemPickerProps ) {
15
15
super ( props ) ;
@@ -21,37 +21,50 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
21
21
noresultsFoundText : ! this . props . noResultsFoundText ? strings . genericNoResultsFoundText : this . props . noResultsFoundText ,
22
22
showError : false ,
23
23
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 || [ ]
25
26
} ;
26
27
27
28
// Get SPService Factory
28
29
this . _spservice = new SPservice ( this . props . context ) ;
29
-
30
- this . selectedItems = [ ] ;
31
30
}
32
31
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 ;
36
39
}
40
+
41
+ this . setState ( {
42
+ selectedItems : newSelectedItems
43
+ } ) ;
37
44
}
38
45
39
46
/**
40
47
* Render the field
41
48
*/
42
49
public render ( ) : React . ReactElement < IListItemPickerProps > {
43
50
const { className, disabled, itemLimit, placeholder } = this . props ;
51
+ const {
52
+ suggestionsHeaderText,
53
+ noresultsFoundText,
54
+ errorMessage,
55
+ selectedItems
56
+ } = this . state ;
44
57
45
58
return (
46
59
< div >
47
60
< TagPicker onResolveSuggestions = { this . onFilterChanged }
48
61
// getTextFromItem={(item: any) => { return item.name; }}
49
62
getTextFromItem = { this . getTextFromItem }
50
63
pickerSuggestionsProps = { {
51
- suggestionsHeaderText : this . state . suggestionsHeaderText ,
52
- noResultsFoundText : this . state . noresultsFoundText
64
+ suggestionsHeaderText : suggestionsHeaderText ,
65
+ noResultsFoundText : noresultsFoundText
53
66
} }
54
- defaultSelectedItems = { this . props . defaultSelectedItems || [ ] }
67
+ selectedItems = { selectedItems }
55
68
onChange = { this . onItemChanged }
56
69
className = { className }
57
70
itemLimit = { itemLimit }
@@ -60,8 +73,8 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
60
73
placeholder : placeholder
61
74
} } />
62
75
63
- { ! ! this . state . errorMessage &&
64
- ( < Label style = { { color :'#FF0000' } } > { this . state . errorMessage } </ Label > ) }
76
+ { ! ! errorMessage &&
77
+ ( < Label style = { { color :'#FF0000' } } > { errorMessage } </ Label > ) }
65
78
</ div >
66
79
) ;
67
80
}
@@ -77,7 +90,9 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
77
90
* On Selected Item
78
91
*/
79
92
private onItemChanged = ( selectedItems : { key : string ; name : string } [ ] ) : void => {
80
- this . selectedItems = selectedItems ;
93
+ this . setState ( {
94
+ selectedItems : selectedItems
95
+ } ) ;
81
96
this . props . onSelectedItem ( selectedItems ) ;
82
97
}
83
98
@@ -87,7 +102,9 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
87
102
private onFilterChanged = async ( filterText : string , tagList : { key : string ; name : string } [ ] ) => {
88
103
let resolvedSugestions : { key : string ; name : string } [ ] = await this . loadListItems ( filterText ) ;
89
104
90
- const selectedItems = [ ...this . selectedItems , ...( this . props . defaultSelectedItems || [ ] ) ] ;
105
+ const {
106
+ selectedItems
107
+ } = this . state ;
91
108
92
109
// Filter out the already retrieved items, so that they cannot be selected again
93
110
if ( selectedItems && selectedItems . length > 0 ) {
0 commit comments