Skip to content

Commit

Permalink
Refactor dataviews view config menu (#67637)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Dec 12, 2024
1 parent a2993f7 commit 83aa04c
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions packages/dataviews/src/components/dataviews-view-config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,57 @@ function ViewTypeMenu( {
}
const activeView = VIEW_LAYOUTS.find( ( v ) => view.type === v.type );
return (
<Menu
trigger={
<Button
size="compact"
icon={ activeView?.icon }
label={ __( 'Layout' ) }
/>
}
>
{ availableLayouts.map( ( layout ) => {
const config = VIEW_LAYOUTS.find( ( v ) => v.type === layout );
if ( ! config ) {
return null;
<Menu>
<Menu.TriggerButton
render={
<Button
size="compact"
icon={ activeView?.icon }
label={ __( 'Layout' ) }
/>
}
return (
<Menu.RadioItem
key={ layout }
value={ layout }
name="view-actions-available-view"
checked={ layout === view.type }
hideOnClick
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => {
switch ( e.target.value ) {
case 'list':
case 'grid':
case 'table':
const viewWithoutLayout = { ...view };
if ( 'layout' in viewWithoutLayout ) {
delete viewWithoutLayout.layout;
}
// @ts-expect-error
return onChangeView( {
...viewWithoutLayout,
type: e.target.value,
...defaultLayouts[ e.target.value ],
} );
}
warning( 'Invalid dataview' );
} }
>
<Menu.ItemLabel>{ config.label }</Menu.ItemLabel>
</Menu.RadioItem>
);
} ) }
/>
<Menu.Popover>
{ availableLayouts.map( ( layout ) => {
const config = VIEW_LAYOUTS.find(
( v ) => v.type === layout
);
if ( ! config ) {
return null;
}
return (
<Menu.RadioItem
key={ layout }
value={ layout }
name="view-actions-available-view"
checked={ layout === view.type }
hideOnClick
onChange={ (
e: ChangeEvent< HTMLInputElement >
) => {
switch ( e.target.value ) {
case 'list':
case 'grid':
case 'table':
const viewWithoutLayout = { ...view };
if ( 'layout' in viewWithoutLayout ) {
delete viewWithoutLayout.layout;
}
// @ts-expect-error
return onChangeView( {
...viewWithoutLayout,
type: e.target.value,
...defaultLayouts[ e.target.value ],
} );
}
warning( 'Invalid dataview' );
} }
>
<Menu.ItemLabel>{ config.label }</Menu.ItemLabel>
</Menu.RadioItem>
);
} ) }
</Menu.Popover>
</Menu>
);
}
Expand Down

0 comments on commit 83aa04c

Please sign in to comment.