From c442f0e1f463c357b27ba2e0b9918e71fa2f033c Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 17 Jan 2018 16:33:23 +1100 Subject: [PATCH] Add a unique id to inserter items This lets the inserter do faster string comparison between inserter items rather than deep object comparison. --- editor/components/inserter/group.js | 18 ++++++++++++------ editor/components/inserter/test/menu.js | 7 +++++++ editor/store/selectors.js | 3 +++ editor/store/test/selectors.js | 2 ++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/editor/components/inserter/group.js b/editor/components/inserter/group.js index ab9abf50137275..359f3d2622d874 100644 --- a/editor/components/inserter/group.js +++ b/editor/components/inserter/group.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isEqual, find } from 'lodash'; +import { isEqual } from 'lodash'; /** * WordPress dependencies @@ -29,9 +29,13 @@ export default class InserterGroup extends Component { componentWillReceiveProps( nextProps ) { if ( ! isEqual( this.props.items, nextProps.items ) ) { this.activeItems = deriveActiveItems( nextProps.items ); + // Try and preserve any still valid selected state. - const current = find( this.activeItems, ( item ) => isEqual( item, this.state.current ) ); - if ( ! current ) { + const currentIsStillActive = this.state.current && this.activeItems.some( item => + item.id === this.state.current.id + ); + + if ( ! currentIsStillActive ) { this.setState( { current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null, } ); @@ -39,17 +43,19 @@ export default class InserterGroup extends Component { } } - renderItem( item, index ) { + renderItem( item ) { const { current } = this.state; const { onSelectItem } = this.props; + const isCurrent = current && current.id === item.id; + return (