Skip to content

Commit

Permalink
DataViews table layout: hide actions menu when there is only one acti…
Browse files Browse the repository at this point in the history
…on and is primary (#67020)

Co-authored-by: oandregal <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
4 people authored Nov 15, 2024
1 parent 02f67b8 commit 40954ff
Showing 1 changed file with 63 additions and 23 deletions.
86 changes: 63 additions & 23 deletions packages/dataviews/src/components/dataviews-item-actions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ interface CompactItemActionsProps< Item > {
actions: Action< Item >[];
}

interface PrimaryActionsProps< Item > {
item: Item;
actions: Action< Item >[];
registry: ReturnType< typeof useRegistry >;
}

function ButtonTrigger< Item >( {
action,
onClick,
Expand Down Expand Up @@ -179,6 +185,13 @@ export function ActionsMenuGroup< Item >( {
);
}

function hasOnlyOneActionAndIsPrimary< Item >(
primaryActions: Action< Item >[],
actions: Action< Item >[]
) {
return primaryActions.length === 1 && actions.length;
}

export default function ItemActions< Item >( {
item,
actions,
Expand All @@ -199,9 +212,21 @@ export default function ItemActions< Item >( {
eligibleActions: _eligibleActions,
};
}, [ actions, item ] );

if ( isCompact ) {
return <CompactItemActions item={ item } actions={ eligibleActions } />;
}

if ( hasOnlyOneActionAndIsPrimary( primaryActions, actions ) ) {
return (
<PrimaryActions
item={ item }
actions={ primaryActions }
registry={ registry }
/>
);
}

return (
<HStack
spacing={ 1 }
Expand All @@ -212,29 +237,11 @@ export default function ItemActions< Item >( {
width: 'auto',
} }
>
{ !! primaryActions.length &&
primaryActions.map( ( action ) => {
if ( 'RenderModal' in action ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ [ item ] }
ActionTrigger={ ButtonTrigger }
/>
);
}
return (
<ButtonTrigger
key={ action.id }
action={ action }
onClick={ () => {
action.callback( [ item ], { registry } );
} }
items={ [ item ] }
/>
);
} ) }
<PrimaryActions
item={ item }
actions={ primaryActions }
registry={ registry }
/>
<CompactItemActions item={ item } actions={ eligibleActions } />
</HStack>
);
Expand Down Expand Up @@ -262,3 +269,36 @@ function CompactItemActions< Item >( {
</Menu>
);
}

function PrimaryActions< Item >( {
item,
actions,
registry,
}: PrimaryActionsProps< Item > ) {
if ( ! Array.isArray( actions ) || actions.length === 0 ) {
return null;
}

return actions.map( ( action ) => {
if ( 'RenderModal' in action ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ [ item ] }
ActionTrigger={ ButtonTrigger }
/>
);
}
return (
<ButtonTrigger
key={ action.id }
action={ action }
onClick={ () => {
action.callback( [ item ], { registry } );
} }
items={ [ item ] }
/>
);
} );
}

1 comment on commit 40954ff

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 40954ff.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11852878555
📝 Reported issues:

Please sign in to comment.