@@ -7,11 +7,11 @@ import { ComboBox, IComboBoxOption } from "office-ui-fabric-react/lib/ComboBox";
7
7
import { ListItemRepository } from '../../common/dal/ListItemRepository' ;
8
8
import styles from './ComboBoxListItemPicker.module.scss' ;
9
9
import { Spinner , SpinnerSize } from 'office-ui-fabric-react' ;
10
+ import { cloneDeep } from 'lodash' ;
10
11
11
12
12
13
export class ComboBoxListItemPicker extends React . Component < IComboBoxListItemPickerProps , IComboBoxListItemPickerState > {
13
14
private _listItemRepo : ListItemRepository ;
14
- public selectedItems : any [ ] ;
15
15
16
16
constructor ( props : IComboBoxListItemPickerProps ) {
17
17
super ( props ) ;
@@ -23,13 +23,13 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
23
23
noresultsFoundText : ! this . props . noResultsFoundText ? strings . genericNoResultsFoundText : this . props . noResultsFoundText ,
24
24
showError : false ,
25
25
errorMessage : "" ,
26
- suggestionsHeaderText : ! this . props . suggestionsHeaderText ? strings . ListItemPickerSelectValue : this . props . suggestionsHeaderText
26
+ suggestionsHeaderText : ! this . props . suggestionsHeaderText ? strings . ListItemPickerSelectValue : this . props . suggestionsHeaderText ,
27
+ selectedItems : this . props . defaultSelectedItems || [ ] ,
27
28
} ;
28
29
29
30
// Get SPService Factory
30
31
this . _listItemRepo = new ListItemRepository ( this . props . webUrl , this . props . spHttpClient ) ;
31
32
32
- this . selectedItems = [ ] ;
33
33
}
34
34
35
35
public componentDidMount ( ) : void {
@@ -47,6 +47,7 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
47
47
defaultSelectedItems,
48
48
onInitialized
49
49
} = props ;
50
+ let selectedItems : any [ ] = [ ] ;
50
51
let query = filter || "" ;
51
52
//query += filter;
52
53
let keyColumnName = keyColumnInternalName || "Id" ;
@@ -66,14 +67,23 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
66
67
if ( defaultSelectedItems ) {
67
68
//if passed only ids
68
69
if ( ! isNaN ( defaultSelectedItems [ 0 ] ) ) {
69
- this . selectedItems = options . filter ( opt => defaultSelectedItems . indexOf ( opt . key ) >= 0 ) ;
70
+ selectedItems = options . filter ( opt => defaultSelectedItems . indexOf ( opt . key ) >= 0 ) ;
70
71
}
71
72
else {
72
- this . selectedItems = options . filter ( opt => defaultSelectedItems . map ( selected => selected [ keyColumnName ] ) . indexOf ( opt . key ) >= 0 ) ;
73
+ selectedItems = options . filter ( opt => defaultSelectedItems . map ( selected => selected [ keyColumnName ] ) . indexOf ( opt . key ) >= 0 ) ;
73
74
}
74
75
}
76
+ if ( selectedItems ?. length > 0 ) {
77
+ selectedItems = selectedItems . map ( item => {
78
+ return {
79
+ [ this . props . keyColumnInternalName || "Id" ] : item . key ,
80
+ [ this . props . columnInternalName ] : item . text
81
+ }
82
+ } )
83
+ }
75
84
this . setState ( {
76
- availableOptions : options
85
+ availableOptions : options ,
86
+ selectedItems : selectedItems ,
77
87
} ) ;
78
88
if ( onInitialized && isInitialLoad !== false ) {
79
89
onInitialized ( ) ;
@@ -83,7 +93,9 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
83
93
public async componentWillReceiveProps ( nextProps : IComboBoxListItemPickerProps ) : Promise < void > {
84
94
if ( nextProps . listId !== this . props . listId ) {
85
95
await this . loadOptions ( nextProps , false ) ;
86
- this . selectedItems = [ ] ;
96
+ this . setState ( {
97
+ selectedItems : [ ] ,
98
+ } ) ;
87
99
}
88
100
}
89
101
@@ -98,6 +110,7 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
98
110
*/
99
111
public render ( ) : React . ReactElement < IComboBoxListItemPickerProps > {
100
112
const { className, disabled } = this . props ;
113
+ const selectedKeys = this . state . selectedItems ?. map ( item => item [ this . props . keyColumnInternalName || "Id" ] ) || [ ] ;
101
114
102
115
return (
103
116
< div className = { styles . comboBoxListItemPickerWrapper } >
@@ -112,7 +125,7 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
112
125
text = { this . props . text }
113
126
onChange = { ( e , value ) => this . onChanged ( value ) }
114
127
multiSelect = { this . props . multiSelect }
115
- defaultSelectedKey = { this . selectedItems . map ( item => item . key ) || [ ] }
128
+ selectedKey = { selectedKeys }
116
129
className = { className }
117
130
disabled = { disabled } />
118
131
{ ! this . state . errorMessage && ! this . state . availableOptions && ( < Spinner className = { styles . loading } size = { SpinnerSize . small } /> ) }
@@ -126,25 +139,30 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
126
139
* On Selected Item
127
140
*/
128
141
private onChanged = ( option ?: IComboBoxOption , index ?: number , value ?: string , submitPendingValueEvent ?: any ) : void => {
142
+ let selectedItems : any [ ] = cloneDeep ( this . state . selectedItems ) ;
129
143
if ( this . props . multiSelect ) {
130
144
if ( option && option . selected ) {
131
- this . selectedItems . push ( {
145
+ selectedItems . push ( {
132
146
[ this . props . keyColumnInternalName || "Id" ] : option . key ,
133
147
[ this . props . columnInternalName ] : option . text ,
134
148
selected : option . selected
135
149
} ) ;
136
150
} else {
137
- this . selectedItems = this . selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] !== option . key ) ;
151
+ selectedItems = selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] !== option . key ) ;
138
152
}
139
153
} else {
140
- this . selectedItems . push ( {
154
+ selectedItems . push ( {
141
155
[ this . props . keyColumnInternalName || "Id" ] : option . key ,
142
156
[ this . props . columnInternalName ] : option . text
143
157
} ) ;
144
158
145
- this . selectedItems = this . selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] === option . key ) ;
159
+ selectedItems = selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] === option . key ) ;
146
160
}
147
161
148
- this . props . onSelectedItem ( this . selectedItems ) ;
162
+ this . setState ( {
163
+ selectedItems : selectedItems ,
164
+ } ) ;
165
+
166
+ this . props . onSelectedItem ( selectedItems ) ;
149
167
}
150
168
}
0 commit comments