11/**
22 * External dependencies
33 */
4- import { isEqual , find } from 'lodash' ;
4+ import { isEqual } from 'lodash' ;
55
66/**
77 * WordPress dependencies
@@ -10,8 +10,8 @@ import { Component } from '@wordpress/element';
1010import { NavigableMenu } from '@wordpress/components' ;
1111import { BlockIcon } from '@wordpress/blocks' ;
1212
13- function deriveActiveBlocks ( blocks ) {
14- return blocks . filter ( ( block ) => ! block . disabled ) ;
13+ function deriveActiveItems ( items ) {
14+ return items . filter ( ( item ) => ! item . isDisabled ) ;
1515}
1616
1717export default class InserterGroup extends Component {
@@ -20,61 +20,62 @@ export default class InserterGroup extends Component {
2020
2121 this . onNavigate = this . onNavigate . bind ( this ) ;
2222
23- this . activeBlocks = deriveActiveBlocks ( this . props . blockTypes ) ;
23+ this . activeItems = deriveActiveItems ( this . props . items ) ;
2424 this . state = {
25- current : this . activeBlocks . length > 0 ? this . activeBlocks [ 0 ] . name : null ,
25+ current : this . activeItems . length > 0 ? this . activeItems [ 0 ] : null ,
2626 } ;
2727 }
2828
2929 componentWillReceiveProps ( nextProps ) {
30- if ( ! isEqual ( this . props . blockTypes , nextProps . blockTypes ) ) {
31- this . activeBlocks = deriveActiveBlocks ( nextProps . blockTypes ) ;
30+ if ( ! isEqual ( this . props . items , nextProps . items ) ) {
31+ this . activeItems = deriveActiveItems ( nextProps . items ) ;
32+
3233 // Try and preserve any still valid selected state.
33- const current = find ( this . activeBlocks , { name : this . state . current } ) ;
34- if ( ! current ) {
34+ const currentIsStillActive = this . state . current && this . activeItems . some ( item =>
35+ item . id === this . state . current . id
36+ ) ;
37+
38+ if ( ! currentIsStillActive ) {
3539 this . setState ( {
36- current : this . activeBlocks . length > 0 ? this . activeBlocks [ 0 ] . name : null ,
40+ current : this . activeItems . length > 0 ? this . activeItems [ 0 ] : null ,
3741 } ) ;
3842 }
3943 }
4044 }
4145
42- renderItem ( block ) {
46+ renderItem ( item ) {
4347 const { current } = this . state ;
44- const { selectBlock, bindReferenceNode } = this . props ;
45- const { disabled } = block ;
48+ const { onSelectItem } = this . props ;
49+
50+ const isCurrent = current && current . id === item . id ;
4651
4752 return (
4853 < button
4954 role = "menuitem"
50- key = { block . name === 'core/block' && block . initialAttributes ?
51- block . name + block . initialAttributes . ref :
52- block . name
53- }
55+ key = { item . id }
5456 className = "editor-inserter__block"
55- onClick = { selectBlock ( block ) }
56- ref = { bindReferenceNode ( block . name ) }
57- tabIndex = { current === block . name || disabled ? null : '-1' }
58- disabled = { disabled }
57+ onClick = { ( ) => onSelectItem ( item ) }
58+ tabIndex = { isCurrent || item . isDisabled ? null : '-1' }
59+ disabled = { item . isDisabled }
5960 >
60- < BlockIcon icon = { block . icon } />
61- { block . title }
61+ < BlockIcon icon = { item . icon } />
62+ { item . title }
6263 </ button >
6364 ) ;
6465 }
6566
6667 onNavigate ( index ) {
67- const { activeBlocks } = this ;
68- const dest = activeBlocks [ index ] ;
68+ const { activeItems } = this ;
69+ const dest = activeItems [ index ] ;
6970 if ( dest ) {
7071 this . setState ( {
71- current : dest . name ,
72+ current : dest ,
7273 } ) ;
7374 }
7475 }
7576
7677 render ( ) {
77- const { labelledBy, blockTypes } = this . props ;
78+ const { labelledBy, items } = this . props ;
7879
7980 return (
8081 < NavigableMenu
@@ -83,7 +84,7 @@ export default class InserterGroup extends Component {
8384 aria-labelledby = { labelledBy }
8485 cycle = { false }
8586 onNavigate = { this . onNavigate } >
86- { blockTypes . map ( this . renderItem , this ) }
87+ { items . map ( this . renderItem , this ) }
8788 </ NavigableMenu >
8889 ) ;
8990 }
0 commit comments