@@ -7,6 +7,7 @@ import { SPServiceFactory } from '../../services/SPServiceFactory';
7
7
import * as telemetry from '../../common/telemetry' ;
8
8
9
9
import styles from './ListPicker.module.scss' ;
10
+ import { cloneDeep } from '@microsoft/sp-lodash-subset' ;
10
11
11
12
/**
12
13
* Empty list value, to be checked for single list selection
@@ -17,8 +18,7 @@ const EMPTY_LIST_KEY = 'NO_LIST_SELECTED';
17
18
* Renders the controls for the ListPicker component
18
19
*/
19
20
export class ListPicker extends React . Component < IListPickerProps , IListPickerState > {
20
- private _options : IDropdownOption [ ] = [ ] ;
21
- private _selectedList : string | string [ ] ;
21
+ private _selectedList : string | string [ ] = null ;
22
22
23
23
/**
24
24
* Constructor method
@@ -29,11 +29,9 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
29
29
telemetry . track ( 'ReactListPicker' ) ;
30
30
31
31
this . state = {
32
- options : this . _options ,
32
+ options : [ ] ,
33
33
loading : false
34
34
} ;
35
-
36
- this . onChanged = this . onChanged . bind ( this ) ;
37
35
}
38
36
39
37
/**
@@ -52,18 +50,21 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
52
50
if (
53
51
prevProps . baseTemplate !== this . props . baseTemplate ||
54
52
prevProps . includeHidden !== this . props . includeHidden ||
55
- prevProps . orderBy !== this . props . orderBy ||
56
- prevProps . selectedList !== this . props . selectedList
53
+ prevProps . orderBy !== this . props . orderBy
57
54
) {
58
55
this . loadLists ( ) ;
59
56
}
57
+
58
+ if ( prevProps . selectedList !== this . props . selectedList ) {
59
+ this . setSelectedLists ( ) ;
60
+ }
60
61
}
61
62
62
63
/**
63
64
* Loads the list from SharePoint current web site
64
65
*/
65
66
private loadLists ( ) {
66
- const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter, selectedList } = this . props ;
67
+ const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter } = this . props ;
67
68
68
69
// Show the loading indicator and disable the dropdown
69
70
this . setState ( { loading : true } ) ;
@@ -75,60 +76,66 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
75
76
orderBy : orderBy ,
76
77
filter : filter
77
78
} ) . then ( ( results ) => {
79
+ let options : IDropdownOption [ ] = [ ] ;
80
+
78
81
// Start mapping the lists to the dropdown option
79
- results . value . map ( list => {
80
- this . _options . push ( {
81
- key : list . Id ,
82
- text : list . Title
83
- } ) ;
84
- } ) ;
82
+ options = results . value . map ( list => ( {
83
+ key : list . Id ,
84
+ text : list . Title
85
+ } ) ) ;
85
86
86
87
if ( multiSelect !== true ) {
87
88
// Add option to unselct list
88
- this . _options . unshift ( {
89
+ options . unshift ( {
89
90
key : EMPTY_LIST_KEY ,
90
91
text : ''
91
92
} ) ;
92
93
}
93
94
94
- this . _selectedList = this . props . selectedList ;
95
+ this . setSelectedLists ( ) ;
95
96
96
97
// Hide the loading indicator and set the dropdown options and enable the dropdown
97
98
this . setState ( {
98
99
loading : false ,
99
- options : this . _options ,
100
- selectedList : this . _selectedList
100
+ options : options
101
101
} ) ;
102
102
} ) ;
103
103
}
104
104
105
+ /**
106
+ * Set the currently selected list
107
+ */
108
+ private setSelectedLists ( ) {
109
+ this . _selectedList = cloneDeep ( this . props . selectedList ) ;
110
+ this . setState ( {
111
+ selectedList : this . _selectedList
112
+ } ) ;
113
+ }
114
+
105
115
/**
106
116
* Raises when a list has been selected
107
117
* @param option the new selection
108
118
* @param index the index of the selection
109
119
*/
110
- private onChanged ( option : IDropdownOption , index ?: number ) : void {
120
+ private onChanged = ( option : IDropdownOption , index ?: number ) : void => {
111
121
const { multiSelect, onSelectionChanged } = this . props ;
112
122
113
123
if ( multiSelect === true ) {
114
- if ( ! this . _selectedList ) {
115
- this . _selectedList = [ ] as string [ ] ;
116
- }
117
-
118
- const selectedLists : string [ ] = this . _selectedList as string [ ] ;
119
124
// Check if option was selected
125
+ let selectedLists = this . _selectedList ? cloneDeep ( this . _selectedList ) as string [ ] : [ "test" ] ;
120
126
if ( option . selected ) {
121
127
selectedLists . push ( option . key as string ) ;
122
128
} else {
123
129
// Filter out the unselected list
124
- this . _selectedList = selectedLists . filter ( list => list !== option . key ) ;
130
+ selectedLists = selectedLists . filter ( list => list !== option . key ) ;
125
131
}
132
+ this . _selectedList = selectedLists ;
126
133
} else {
127
134
this . _selectedList = option . key as string ;
128
135
}
129
136
130
137
if ( onSelectionChanged ) {
131
- onSelectionChanged ( this . _selectedList ) ;
138
+ onSelectionChanged ( cloneDeep ( this . _selectedList ) ) ;
132
139
}
133
140
}
134
141
0 commit comments