@@ -47,7 +47,8 @@ export default class SortableList extends Component {
47
47
/**
48
48
* Stores promises of rows’ layouts.
49
49
*/
50
- _rowsLayouts = [ ] ;
50
+ _rowsLayouts = { } ;
51
+ _resolveRowLayout = { } ;
51
52
52
53
_contentOffset = { x : 0 , y : 0 } ;
53
54
@@ -64,23 +65,43 @@ export default class SortableList extends Component {
64
65
scrollEnabled : this . props . scrollEnabled
65
66
} ;
66
67
68
+ componentWillMount ( ) {
69
+ this . state . order . forEach ( ( key ) => {
70
+ this . _rowsLayouts [ key ] = new Promise ( ( resolve ) => {
71
+ this . _resolveRowLayout [ key ] = resolve ;
72
+ } ) ;
73
+ } ) ;
74
+
75
+ if ( this . props . renderFooter && ! this . props . horizontal ) {
76
+ this . _footerLayout = new Promise ( ( resolve ) => {
77
+ this . _resolveFooterLayout = resolve ;
78
+ } ) ;
79
+ }
80
+ }
81
+
67
82
componentDidMount ( ) {
68
83
this . _onUpdateLayouts ( ) ;
69
84
}
70
85
71
86
componentWillReceiveProps ( nextProps ) {
72
87
const { data, order} = this . state ;
73
- const { data : nextData , order : nextOrder } = nextProps ;
88
+ let { data : nextData , order : nextOrder } = nextProps ;
74
89
75
90
if ( data && nextData && ! shallowEqual ( data , nextData ) ) {
91
+ nextOrder = nextOrder || Object . keys ( nextData )
76
92
uniqueRowKey . id ++ ;
77
- this . _rowsLayouts = [ ] ;
93
+ this . _rowsLayouts = { } ;
94
+ nextOrder . forEach ( ( key ) => {
95
+ this . _rowsLayouts [ key ] = new Promise ( ( resolve ) => {
96
+ this . _resolveRowLayout [ key ] = resolve ;
97
+ } ) ;
98
+ } ) ;
78
99
this . setState ( {
79
100
animated : false ,
80
101
data : nextData ,
81
102
containerLayout : null ,
82
103
rowsLayouts : null ,
83
- order : nextOrder || Object . keys ( nextData )
104
+ order : nextOrder
84
105
} ) ;
85
106
86
107
} else if ( order && nextOrder && ! shallowEqual ( order , nextOrder ) ) {
@@ -206,7 +227,6 @@ export default class SortableList extends Component {
206
227
return order . map ( ( key , index ) => {
207
228
const style = { [ ZINDEX ] : 0 } ;
208
229
const location = { x : 0 , y : 0 } ;
209
- let resolveLayout ;
210
230
211
231
if ( rowsLayouts ) {
212
232
if ( horizontal ) {
@@ -218,8 +238,6 @@ export default class SortableList extends Component {
218
238
location . y = nextY ;
219
239
nextY += rowsLayouts [ key ] . height ;
220
240
}
221
- } else {
222
- this . _rowsLayouts . push ( new Promise ( ( resolve ) => ( resolveLayout = resolve ) ) ) ;
223
241
}
224
242
225
243
const active = activeRowKey === key ;
@@ -239,7 +257,7 @@ export default class SortableList extends Component {
239
257
disabled = { ! sortingEnabled }
240
258
style = { style }
241
259
location = { location }
242
- onLayout = { ! rowsLayouts ? this . _onLayoutRow . bind ( this , resolveLayout , key ) : null }
260
+ onLayout = { ! rowsLayouts ? this . _onLayoutRow . bind ( this , key ) : null }
243
261
onActivate = { this . _onActivateRow . bind ( this , key , index ) }
244
262
onPress = { this . _onPressRow . bind ( this , key ) }
245
263
onRelease = { this . _onReleaseRow . bind ( this , key ) }
@@ -262,21 +280,16 @@ export default class SortableList extends Component {
262
280
}
263
281
264
282
const { footerLayout} = this . state ;
265
- let resolveLayout ;
266
-
267
- if ( ! footerLayout ) {
268
- this . _footerLayout = new Promise ( ( resolve ) => ( resolveLayout = resolve ) ) ;
269
- }
270
283
271
284
return (
272
- < View onLayout = { ! footerLayout ? this . _onLayoutFooter . bind ( this , resolveLayout ) : null } >
285
+ < View onLayout = { ! footerLayout ? this . _onLayoutFooter : null } >
273
286
{ this . props . renderFooter ( ) }
274
287
</ View >
275
288
) ;
276
289
}
277
290
278
291
_onUpdateLayouts ( ) {
279
- Promise . all ( [ this . _footerLayout , ...this . _rowsLayouts ] )
292
+ Promise . all ( [ this . _footerLayout , ...Object . values ( this . _rowsLayouts ) ] )
280
293
. then ( ( [ footerLayout , ...rowsLayouts ] ) => {
281
294
// Can get correct container’s layout only after rows’s layouts.
282
295
this . _container . measure ( ( x , y , width , height , pageX , pageY ) => {
@@ -515,13 +528,13 @@ export default class SortableList extends Component {
515
528
this . _autoScrollInterval = null ;
516
529
}
517
530
518
- _onLayoutRow ( resolveLayout , rowKey , { nativeEvent : { layout} } ) {
519
- resolveLayout ( { rowKey, layout} ) ;
531
+ _onLayoutRow ( rowKey , { nativeEvent : { layout} } ) {
532
+ this . _resolveRowLayout [ rowKey ] ( { rowKey, layout} ) ;
520
533
}
521
534
522
- _onLayoutFooter ( resolveLayout , { nativeEvent : { layout} } ) {
523
- resolveLayout ( layout ) ;
524
- }
535
+ _onLayoutFooter = ( { nativeEvent : { layout} } ) => {
536
+ this . _resolveFooterLayout ( layout ) ;
537
+ } ;
525
538
526
539
_onActivateRow = ( rowKey , index , e , gestureState , location ) => {
527
540
this . _activeRowLocation = location ;
0 commit comments