Skip to content

Commit

Permalink
Extract filterOpenPanels funcion
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 11, 2019
1 parent 2010aa2 commit f8e2c44
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -193,31 +212,19 @@ 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,
filterValue,
suggestedItems,
reusableItems,
itemsPerCategory,
openPanels,
openPanels: this.filterOpenPanels(
filterValue,
itemsPerCategory,
filteredItems,
reusableItems
),
} );

const resultCount = Object.keys( itemsPerCategory ).reduce( ( accumulator, currentCategorySlug ) => {
Expand All @@ -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
Expand Down

0 comments on commit f8e2c44

Please sign in to comment.