Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataViews: Create a single component for rendering the actions list #67558

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
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
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
Loading