Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
isNested toolbar group first pass
  • Loading branch information
margolisj committed Sep 12, 2023
commit 3c7f8270089634f59d14429a07f395267ee56a80
17 changes: 11 additions & 6 deletions packages/components/src/toolbar/toolbar-group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import ToolbarButton from '../toolbar-button';
import ToolbarGroupContainer from './toolbar-group-container';
import ToolbarGroupCollapsed from './toolbar-group-collapsed';
import ToolbarContext from '../toolbar-context';
import type { ToolbarGroupProps } from './types';
import type { ToolbarGroupProps, ToolbarGroupControls } from './types';

function isNestedArray< T = any >( arr: T[] | T[][] ): arr is T[][] {
return Array.isArray( arr ) && Array.isArray( arr[ 0 ] );
}

/**
* Renders a collapsible group of controls
Expand Down Expand Up @@ -73,9 +77,11 @@ function ToolbarGroup( {
);

// Normalize controls to nested array of objects (sets of controls)
let controlSets = controls;
if ( ! Array.isArray( controlSets[ 0 ] ) ) {
controlSets = [ controlSets ];
let controlSets: ToolbarGroupControls[][];
if ( isNestedArray( controls ) ) {
controlSets = controls;
} else {
controlSets = [ controls ];
}

if ( isCollapsed ) {
Expand All @@ -93,8 +99,7 @@ function ToolbarGroup( {
return (
<ToolbarGroupContainer className={ finalClassName } { ...props }>
{ controlSets?.flatMap( ( controlSet, indexOfSet ) =>
// @ts-expect-error Type issue
controlSet.map( ( control: any, indexOfControl: number ) => (
controlSet.map( ( control, indexOfControl: number ) => (
<ToolbarButton
key={ [ indexOfSet, indexOfControl ].join() }
containerClassName={
Expand Down