From 2010aa26b26a3a8687eb40048741040bf4c43994 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 14 Feb 2019 19:55:52 +0000 Subject: [PATCH 1/2] Fix: Inserter impossible to collapse panels while searching. --- .../src/components/inserter/menu.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/inserter/menu.js b/packages/block-editor/src/components/inserter/menu.js index 92253b32cfc1d..fb81cea7a9af6 100644 --- a/packages/block-editor/src/components/inserter/menu.js +++ b/packages/block-editor/src/components/inserter/menu.js @@ -197,11 +197,16 @@ export class InserterMenu extends Component { if ( filterValue !== this.state.filterValue ) { if ( ! filterValue ) { openPanels = [ 'suggested' ]; - } else if ( reusableItems.length ) { - openPanels = [ 'reusable' ]; - } else if ( filteredItems.length ) { - const firstCategory = find( getCategories(), ( { slug } ) => itemsPerCategory[ slug ] && itemsPerCategory[ slug ].length ); - openPanels = [ firstCategory.slug ]; + } else { + openPanels = []; + if ( reusableItems.length ) { + openPanels.push( 'reusable' ); + } + if ( filteredItems.length ) { + openPanels = openPanels.concat( + Object.keys( itemsPerCategory ) + ); + } } } @@ -238,7 +243,6 @@ export class InserterMenu extends Component { const { instanceId, onSelect, rootClientId } = this.props; const { childItems, filterValue, hoveredItem, suggestedItems, reusableItems, itemsPerCategory, openPanels } = this.state; const isPanelOpen = ( panel ) => openPanels.indexOf( panel ) !== -1; - const isSearching = !! filterValue; // Disable reason (no-autofocus): The inserter menu is a modal display, not one which // is always visible, and one which already incurs this behavior of autoFocus via @@ -300,7 +304,7 @@ export class InserterMenu extends Component { key={ category.slug } title={ category.title } icon={ category.icon } - opened={ isSearching || isPanelOpen( category.slug ) } + opened={ isPanelOpen( category.slug ) } onToggle={ this.onTogglePanel( category.slug ) } ref={ this.bindPanel( category.slug ) } > From f8e2c44db35179c60160a35581615b6ab725e4f1 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 11 Mar 2019 08:35:53 +0000 Subject: [PATCH 2/2] Extract filterOpenPanels funcion --- .../src/components/inserter/menu.js | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/block-editor/src/components/inserter/menu.js b/packages/block-editor/src/components/inserter/menu.js index fb81cea7a9af6..1ed720113e766 100644 --- a/packages/block-editor/src/components/inserter/menu.js +++ b/packages/block-editor/src/components/inserter/menu.js @@ -170,6 +170,25 @@ export class InserterMenu extends Component { }; } + filterOpenPanels( filterValue, itemsPerCategory, filteredItems, reusableItems ) { + if ( filterValue === this.state.filterValue ) { + return this.state.openPanels; + } + if ( ! filterValue ) { + return [ 'suggested' ]; + } + let openPanels = []; + if ( reusableItems.length > 0 ) { + openPanels.push( 'reusable' ); + } + if ( filteredItems.length > 0 ) { + openPanels = openPanels.concat( + Object.keys( itemsPerCategory ) + ); + } + return openPanels; + } + filter( filterValue = '' ) { const { debouncedSpeak, items, rootChildBlocks } = this.props; const filteredItems = searchItems( items, filterValue ); @@ -193,23 +212,6 @@ export class InserterMenu extends Component { ( itemList ) => groupBy( itemList, 'category' ) )( filteredItems ); - let openPanels = this.state.openPanels; - if ( filterValue !== this.state.filterValue ) { - if ( ! filterValue ) { - openPanels = [ 'suggested' ]; - } else { - openPanels = []; - if ( reusableItems.length ) { - openPanels.push( 'reusable' ); - } - if ( filteredItems.length ) { - openPanels = openPanels.concat( - Object.keys( itemsPerCategory ) - ); - } - } - } - this.setState( { hoveredItem: null, childItems, @@ -217,7 +219,12 @@ export class InserterMenu extends Component { suggestedItems, reusableItems, itemsPerCategory, - openPanels, + openPanels: this.filterOpenPanels( + filterValue, + itemsPerCategory, + filteredItems, + reusableItems + ), } ); const resultCount = Object.keys( itemsPerCategory ).reduce( ( accumulator, currentCategorySlug ) => { @@ -241,7 +248,14 @@ export class InserterMenu extends Component { render() { const { instanceId, onSelect, rootClientId } = this.props; - const { childItems, filterValue, hoveredItem, suggestedItems, reusableItems, itemsPerCategory, openPanels } = this.state; + const { + childItems, + hoveredItem, + itemsPerCategory, + openPanels, + reusableItems, + suggestedItems, + } = this.state; const isPanelOpen = ( panel ) => openPanels.indexOf( panel ) !== -1; // Disable reason (no-autofocus): The inserter menu is a modal display, not one which