Skip to content

Commit

Permalink
Fix updating dataSource from blocks props
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug committed Nov 14, 2018
1 parent 0c614ea commit d7ea973
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import React from 'react';
import { isEqual } from 'lodash';
import { xorBy, isEqual } from 'lodash';

import { Platform, Switch, Text, View, FlatList, KeyboardAvoidingView } from 'react-native';
import RecyclerViewList, { DataSource } from 'react-native-recyclerview-list';
Expand Down Expand Up @@ -72,6 +72,17 @@ export default class BlockManager extends React.Component<PropsType, StateType>
this.props.focusBlockAction( clientId );
}

static focusDataSourceItem( dataSource: DataSource, clientId: string ) {
for ( let i = 0; i < dataSource.size(); ++i ) {
const block = dataSource.get( i );
if ( block.clientId === clientId ) {
block.focused = true;
} else {
block.focused = false;
}
}
}

getDataSourceIndexFromClientId( clientId: string ) {
for ( let i = 0; i < this.state.dataSource.size(); ++i ) {
const block = this.state.dataSource.get( i );
Expand Down Expand Up @@ -124,20 +135,28 @@ export default class BlockManager extends React.Component<PropsType, StateType>
}

static getDerivedStateFromProps( props: PropsType, state: StateType ) {
if ( props.fullparse === true ) {
return {
...state,
dataSource: new DataSource( props.blocks, ( item: BlockType ) => item.clientId ),
};
}

const blocks = props.blocks.map( ( block ) => {
const newBlock = { ...block };
newBlock.focused = props.isBlockSelected( block.clientId );
return newBlock;
} );

// if the blocks in the list (not the order or the block attribute)
// have changed without our knowledge, recreate the dataSource
if ( xorBy( state.blocks, blocks, 'clientId' ).length !== 0 ) {
return {
dataSource: new DataSource( blocks, ( item: BlockType ) => item.clientId ),
blocks,
refresh: ! state.refresh,
};
}

// if some properties of the blocks have changed, assume it's `focused` and manually update in dataSource
if ( ! isEqual( state.blocks, blocks ) ) {
const blockFocused = blocks.find( ( block ) => block.focused );
if ( blockFocused ) {
BlockManager.focusDataSourceItem( state.dataSource, blockFocused.clientId );
}
return {
...state,
blocks,
Expand Down

0 comments on commit d7ea973

Please sign in to comment.