Skip to content

Commit

Permalink
DataViews: Create a single component for rendering the actions list (W…
Browse files Browse the repository at this point in the history
…ordPress#67558)

Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: oandregal <[email protected]>
  • Loading branch information
4 people authored Dec 5, 2024
1 parent 094bd3a commit 3ea3ba2
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions packages/dataviews/src/components/dataviews-item-actions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ interface PrimaryActionsProps< Item > {
actions: Action< Item >[];
registry: ReturnType< typeof useRegistry >;
}
interface ActionsListProps< Item > {
item: Item;
actions: Action< Item >[];
registry: ReturnType< typeof useRegistry >;
ActionTrigger: ( props: ActionTriggerProps< Item > ) => ReactElement;
}

function ButtonTrigger< Item >( {
action,
Expand Down Expand Up @@ -160,28 +166,12 @@ export function ActionsMenuGroup< Item >( {
const registry = useRegistry();
return (
<Menu.Group>
{ actions.map( ( action ) => {
if ( 'RenderModal' in action ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ [ item ] }
ActionTrigger={ MenuItemTrigger }
/>
);
}
return (
<MenuItemTrigger
key={ action.id }
action={ action }
onClick={ () => {
action.callback( [ item ], { registry } );
} }
items={ [ item ] }
/>
);
} ) }
<ActionsList
actions={ actions }
item={ item }
registry={ registry }
ActionTrigger={ MenuItemTrigger }
/>
</Menu.Group>
);
}
Expand Down Expand Up @@ -286,20 +276,35 @@ function PrimaryActions< Item >( {
if ( ! Array.isArray( actions ) || actions.length === 0 ) {
return null;
}
return (
<ActionsList
actions={ actions }
item={ item }
registry={ registry }
ActionTrigger={ ButtonTrigger }
/>
);
}

function ActionsList< Item >( {
item,
actions,
registry,
ActionTrigger,
}: ActionsListProps< Item > ) {
return actions.map( ( action ) => {
if ( 'RenderModal' in action ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ [ item ] }
ActionTrigger={ ButtonTrigger }
ActionTrigger={ ActionTrigger }
/>
);
}
return (
<ButtonTrigger
<ActionTrigger
key={ action.id }
action={ action }
onClick={ () => {
Expand Down

0 comments on commit 3ea3ba2

Please sign in to comment.