1
1
import React , { useEffect } from "react" ;
2
+ import { List } from "react-virtualized" ;
2
3
import PT from "prop-types" ;
3
4
4
5
import Button from "../Button" ;
@@ -159,6 +160,42 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
159
160
setFilter ( "" ) ;
160
161
} ;
161
162
163
+ const filteredOtherGroups = otherGroups . filter ( ( g ) =>
164
+ g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
165
+ ) ;
166
+ const filteredMyGroups = myGroups . filter ( ( g ) =>
167
+ g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
168
+ ) ;
169
+
170
+ const myGroupsListEstimatedWidth = filteredMyGroups . length > 10 ? 540 : 560 ;
171
+ const otherGroupsListEstimatedWidth =
172
+ filteredOtherGroups . length > 10 ? 540 : 560 ;
173
+ const listWidth =
174
+ myGroupsListEstimatedWidth > otherGroupsListEstimatedWidth
175
+ ? myGroupsListEstimatedWidth
176
+ : otherGroupsListEstimatedWidth ;
177
+
178
+ /**
179
+ * Row renderer for react-virtualized#List.
180
+ * Renders each item as a row.
181
+ * @param {String } key unique key for the item
182
+ * @param {Number } index index of the item (row)
183
+ * @param {Object } style
184
+ * @return {SectionRow } row element
185
+ */
186
+ function rowRenderer ( { key, index, style, items } ) {
187
+ return (
188
+ < div key = { key } style = { style } >
189
+ < Group
190
+ checked = { items [ index ] . isSelected === true }
191
+ group = { items [ index ] }
192
+ key = { items [ index ] . id }
193
+ onSwitch = { ( ) => switchSelected ( items [ index ] ) }
194
+ />
195
+ </ div >
196
+ ) ;
197
+ }
198
+
162
199
return (
163
200
< Modal
164
201
onCancel = { onCancel }
@@ -201,19 +238,22 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
201
238
My groups{ loadingGroups && " (Loading...)" }
202
239
</ h3 >
203
240
< div >
204
- { ! loadingGroups &&
205
- myGroups
206
- . filter ( ( g ) =>
207
- g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
208
- )
209
- . map ( ( g ) => (
210
- < Group
211
- checked = { g . isSelected === true }
212
- group = { g }
213
- key = { g . id }
214
- onSwitch = { ( ) => switchSelected ( g ) }
215
- />
216
- ) ) }
241
+ { ! loadingGroups && (
242
+ < List
243
+ className = { style . groupsList }
244
+ width = { listWidth }
245
+ height = {
246
+ filteredMyGroups . length > 10
247
+ ? 450
248
+ : filteredMyGroups . length * 45
249
+ }
250
+ rowCount = { filteredMyGroups . length }
251
+ rowHeight = { 45 }
252
+ rowRenderer = { ( params ) =>
253
+ rowRenderer ( { ...params , items : filteredMyGroups } )
254
+ }
255
+ />
256
+ ) }
217
257
</ div >
218
258
{ myGroups . filter ( ( g ) =>
219
259
g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
@@ -225,19 +265,22 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
225
265
Other Groups{ loadingGroups && " (Loading...)" }
226
266
</ h3 >
227
267
< div >
228
- { ! loadingGroups &&
229
- otherGroups
230
- . filter ( ( g ) =>
231
- g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
232
- )
233
- . map ( ( g ) => (
234
- < Group
235
- checked = { g . isSelected === true }
236
- group = { g }
237
- key = { g . id }
238
- onSwitch = { ( ) => switchSelected ( g ) }
239
- />
240
- ) ) }
268
+ { ! loadingGroups && (
269
+ < List
270
+ className = { style . groupsList }
271
+ width = { listWidth }
272
+ height = {
273
+ filteredOtherGroups . length > 10
274
+ ? 450
275
+ : filteredOtherGroups . length * 45
276
+ }
277
+ rowCount = { filteredOtherGroups . length }
278
+ rowHeight = { 45 }
279
+ rowRenderer = { ( params ) =>
280
+ rowRenderer ( { ...params , items : filteredOtherGroups } )
281
+ }
282
+ />
283
+ ) }
241
284
</ div >
242
285
{ otherGroups . filter ( ( g ) =>
243
286
g . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
0 commit comments