@@ -7,11 +7,12 @@ import { ComboBox, IComboBoxOption } from "office-ui-fabric-react/lib/ComboBox";
7
7
import { ListItemRepository } from '../../common/dal/ListItemRepository' ;
8
8
import { Spinner , SpinnerSize } from 'office-ui-fabric-react' ;
9
9
import styles from './ComboBoxListItemPicker.module.scss' ;
10
+ import { cloneDeep , isEqual } 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
+ private _options : any [ ] = null ;
15
16
16
17
constructor ( props : IComboBoxListItemPickerProps ) {
17
18
super ( props ) ;
@@ -23,13 +24,13 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
23
24
noresultsFoundText : ! this . props . noResultsFoundText ? strings . genericNoResultsFoundText : this . props . noResultsFoundText ,
24
25
showError : false ,
25
26
errorMessage : "" ,
26
- suggestionsHeaderText : ! this . props . suggestionsHeaderText ? strings . ListItemPickerSelectValue : this . props . suggestionsHeaderText
27
+ suggestionsHeaderText : ! this . props . suggestionsHeaderText ? strings . ListItemPickerSelectValue : this . props . suggestionsHeaderText ,
28
+ selectedItems : this . props . defaultSelectedItems || [ ] ,
27
29
} ;
28
30
29
31
// Get SPService Factory
30
32
this . _listItemRepo = new ListItemRepository ( this . props . webUrl , this . props . spHttpClient ) ;
31
33
32
- this . selectedItems = [ ] ;
33
34
}
34
35
35
36
public componentDidMount ( ) : void {
@@ -44,36 +45,32 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
44
45
columnInternalName,
45
46
webUrl,
46
47
itemLimit,
47
- defaultSelectedItems,
48
48
onInitialized
49
49
} = props ;
50
50
let query = filter || "" ;
51
51
//query += filter;
52
52
let keyColumnName = keyColumnInternalName || "Id" ;
53
- let listItems = await this . _listItemRepo . getListItemsByFilterClause ( query ,
54
- listId ,
55
- columnInternalName ,
56
- keyColumnInternalName ,
57
- webUrl ,
58
- itemLimit || 100 ) ;
59
-
60
- let options = listItems . map ( option => {
61
- return {
62
- key : option [ keyColumnName ] ,
63
- text : option [ columnInternalName || "Id" ]
64
- } ;
65
- } ) ;
66
- if ( defaultSelectedItems ) {
67
- //if passed only ids
68
- if ( ! isNaN ( defaultSelectedItems [ 0 ] ) ) {
69
- this . selectedItems = options . filter ( opt => defaultSelectedItems . indexOf ( opt . key ) >= 0 ) ;
70
- }
71
- else {
72
- this . selectedItems = options . filter ( opt => defaultSelectedItems . map ( selected => selected [ keyColumnName ] ) . indexOf ( opt . key ) >= 0 ) ;
73
- }
53
+ if ( ! this . _options || listId !== this . props . listId ) {
54
+ const listItems = await this . _listItemRepo . getListItemsByFilterClause ( query ,
55
+ listId ,
56
+ columnInternalName ,
57
+ keyColumnInternalName ,
58
+ webUrl ,
59
+ itemLimit || 100 ) ;
60
+
61
+ this . _options = listItems . map ( option => {
62
+ return {
63
+ key : option [ keyColumnName ] ,
64
+ text : option [ columnInternalName || "Id" ]
65
+ } ;
66
+ } ) ;
74
67
}
68
+
69
+ const selectedItems = this . _getSelectedItems ( props ) ;
70
+
75
71
this . setState ( {
76
- availableOptions : options
72
+ availableOptions : this . _options ,
73
+ selectedItems : selectedItems ,
77
74
} ) ;
78
75
if ( onInitialized && isInitialLoad !== false ) {
79
76
onInitialized ( ) ;
@@ -82,9 +79,41 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
82
79
83
80
public async componentWillReceiveProps ( nextProps : IComboBoxListItemPickerProps ) : Promise < void > {
84
81
if ( nextProps . listId !== this . props . listId ) {
82
+ this . setState ( {
83
+ selectedItems : [ ] ,
84
+ } ) ;
85
85
await this . loadOptions ( nextProps , false ) ;
86
- this . selectedItems = [ ] ;
87
86
}
87
+ if ( ! isEqual ( nextProps . defaultSelectedItems , this . props . defaultSelectedItems ) ) {
88
+ const selectedItems = this . _getSelectedItems ( nextProps ) ;
89
+ this . setState ( {
90
+ selectedItems : selectedItems ,
91
+ } ) ;
92
+ }
93
+ }
94
+
95
+ private _getSelectedItems ( props : IComboBoxListItemPickerProps ) : any [ ] {
96
+ let selectedItems : any [ ] = [ ] ;
97
+ let keyColumnName = props . keyColumnInternalName || "Id" ;
98
+ if ( props . defaultSelectedItems ) {
99
+ //if passed only ids
100
+ if ( ! isNaN ( props . defaultSelectedItems [ 0 ] ) ) {
101
+ selectedItems = this . _options . filter ( opt => props . defaultSelectedItems . indexOf ( opt . key ) >= 0 ) ;
102
+ }
103
+ else {
104
+ selectedItems = this . _options . filter ( opt => props . defaultSelectedItems . map ( selected => selected [ keyColumnName ] ) . indexOf ( opt . key ) >= 0 ) ;
105
+ }
106
+ }
107
+ if ( selectedItems && selectedItems . length > 0 ) {
108
+ selectedItems = selectedItems . map ( item => {
109
+ return {
110
+ [ this . props . keyColumnInternalName || "Id" ] : item . key ,
111
+ [ this . props . columnInternalName ] : item . text
112
+ } ;
113
+ } ) ;
114
+ }
115
+
116
+ return selectedItems ;
88
117
}
89
118
90
119
/*public componentDidUpdate(prevProps: IComboBoxListItemPickerProps, prevState: IComboBoxListItemPickerState): void {
@@ -98,6 +127,7 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
98
127
*/
99
128
public render ( ) : React . ReactElement < IComboBoxListItemPickerProps > {
100
129
const { className, disabled } = this . props ;
130
+ const selectedKeys = this . state . selectedItems ? this . state . selectedItems . map ( item => item [ this . props . keyColumnInternalName || "Id" ] ) : [ ] ;
101
131
102
132
return (
103
133
< div className = { styles . comboBoxListItemPickerWrapper } >
@@ -113,7 +143,7 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
113
143
text = { this . props . text }
114
144
onChanged = { this . onChanged }
115
145
multiSelect = { this . props . multiSelect }
116
- defaultSelectedKey = { this . selectedItems . map ( item => item . key ) || [ ] }
146
+ selectedKey = { selectedKeys }
117
147
className = { className }
118
148
disabled = { disabled || ! this . state . availableOptions } />
119
149
{ ! this . state . errorMessage && ! this . state . availableOptions && ( < Spinner className = { styles . loading } size = { SpinnerSize . small } /> ) }
@@ -127,25 +157,30 @@ export class ComboBoxListItemPicker extends React.Component<IComboBoxListItemPic
127
157
* On Selected Item
128
158
*/
129
159
private onChanged = ( option ?: IComboBoxOption , index ?: number , value ?: string , submitPendingValueEvent ?: any ) : void => {
160
+ let selectedItems : any [ ] = cloneDeep ( this . state . selectedItems ) ;
130
161
if ( this . props . multiSelect ) {
131
162
if ( option && option . selected ) {
132
- this . selectedItems . push ( {
163
+ selectedItems . push ( {
133
164
[ this . props . keyColumnInternalName || "Id" ] : option . key ,
134
165
[ this . props . columnInternalName ] : option . text ,
135
166
selected : option . selected
136
167
} ) ;
137
168
} else {
138
- this . selectedItems = this . selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] !== option . key ) ;
169
+ selectedItems = selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] !== option . key ) ;
139
170
}
140
171
} else {
141
- this . selectedItems . push ( {
172
+ selectedItems . push ( {
142
173
[ this . props . keyColumnInternalName || "Id" ] : option . key ,
143
174
[ this . props . columnInternalName ] : option . text
144
175
} ) ;
145
176
146
- this . selectedItems = this . selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] === option . key ) ;
177
+ selectedItems = selectedItems . filter ( o => o [ this . props . keyColumnInternalName || "Id" ] === option . key ) ;
147
178
}
148
179
149
- this . props . onSelectedItem ( this . selectedItems ) ;
180
+ this . setState ( {
181
+ selectedItems : selectedItems ,
182
+ } ) ;
183
+
184
+ this . props . onSelectedItem ( selectedItems ) ;
150
185
}
151
186
}
0 commit comments