@@ -7,6 +7,7 @@ import { SPServiceFactory } from '../../services/SPServiceFactory';
77import * as telemetry from '../../common/telemetry' ;
88
99import styles from './ListPicker.module.scss' ;
10+ import { cloneDeep } from '@microsoft/sp-lodash-subset' ;
1011
1112/**
1213* Empty list value, to be checked for single list selection
@@ -17,8 +18,7 @@ const EMPTY_LIST_KEY = 'NO_LIST_SELECTED';
1718* Renders the controls for the ListPicker component
1819*/
1920export class ListPicker extends React . Component < IListPickerProps , IListPickerState > {
20- private _options : IDropdownOption [ ] = [ ] ;
21- private _selectedList : string | string [ ] ;
21+ private _selectedList : string | string [ ] = null ;
2222
2323 /**
2424 * Constructor method
@@ -29,11 +29,9 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
2929 telemetry . track ( 'ReactListPicker' ) ;
3030
3131 this . state = {
32- options : this . _options ,
32+ options : [ ] ,
3333 loading : false
3434 } ;
35-
36- this . onChanged = this . onChanged . bind ( this ) ;
3735 }
3836
3937 /**
@@ -52,18 +50,21 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
5250 if (
5351 prevProps . baseTemplate !== this . props . baseTemplate ||
5452 prevProps . includeHidden !== this . props . includeHidden ||
55- prevProps . orderBy !== this . props . orderBy ||
56- prevProps . selectedList !== this . props . selectedList
53+ prevProps . orderBy !== this . props . orderBy
5754 ) {
5855 this . loadLists ( ) ;
5956 }
57+
58+ if ( prevProps . selectedList !== this . props . selectedList ) {
59+ this . setSelectedLists ( ) ;
60+ }
6061 }
6162
6263 /**
6364 * Loads the list from SharePoint current web site
6465 */
6566 private loadLists ( ) {
66- const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter, selectedList } = this . props ;
67+ const { context, baseTemplate, includeHidden, orderBy, multiSelect, filter } = this . props ;
6768
6869 // Show the loading indicator and disable the dropdown
6970 this . setState ( { loading : true } ) ;
@@ -75,60 +76,66 @@ export class ListPicker extends React.Component<IListPickerProps, IListPickerSta
7576 orderBy : orderBy ,
7677 filter : filter
7778 } ) . then ( ( results ) => {
79+ let options : IDropdownOption [ ] = [ ] ;
80+
7881 // 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+ } ) ) ;
8586
8687 if ( multiSelect !== true ) {
8788 // Add option to unselct list
88- this . _options . unshift ( {
89+ options . unshift ( {
8990 key : EMPTY_LIST_KEY ,
9091 text : ''
9192 } ) ;
9293 }
9394
94- this . _selectedList = this . props . selectedList ;
95+ this . setSelectedLists ( ) ;
9596
9697 // Hide the loading indicator and set the dropdown options and enable the dropdown
9798 this . setState ( {
9899 loading : false ,
99- options : this . _options ,
100- selectedList : this . _selectedList
100+ options : options
101101 } ) ;
102102 } ) ;
103103 }
104104
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+
105115 /**
106116 * Raises when a list has been selected
107117 * @param option the new selection
108118 * @param index the index of the selection
109119 */
110- private onChanged ( option : IDropdownOption , index ?: number ) : void {
120+ private onChanged = ( option : IDropdownOption , index ?: number ) : void => {
111121 const { multiSelect, onSelectionChanged } = this . props ;
112122
113123 if ( multiSelect === true ) {
114- if ( ! this . _selectedList ) {
115- this . _selectedList = [ ] as string [ ] ;
116- }
117-
118- const selectedLists : string [ ] = this . _selectedList as string [ ] ;
119124 // Check if option was selected
125+ let selectedLists = this . _selectedList ? cloneDeep ( this . _selectedList ) as string [ ] : [ "test" ] ;
120126 if ( option . selected ) {
121127 selectedLists . push ( option . key as string ) ;
122128 } else {
123129 // Filter out the unselected list
124- this . _selectedList = selectedLists . filter ( list => list !== option . key ) ;
130+ selectedLists = selectedLists . filter ( list => list !== option . key ) ;
125131 }
132+ this . _selectedList = selectedLists ;
126133 } else {
127134 this . _selectedList = option . key as string ;
128135 }
129136
130137 if ( onSelectionChanged ) {
131- onSelectionChanged ( this . _selectedList ) ;
138+ onSelectionChanged ( cloneDeep ( this . _selectedList ) ) ;
132139 }
133140 }
134141
0 commit comments