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 table layout: hide actions menu when there is only one action and is primary #67020

Merged
merged 1 commit into from
Nov 15, 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
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;
Copy link
Contributor

@ntsekouras ntsekouras Nov 15, 2024

Choose a reason for hiding this comment

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

Why not inline the check?

Copy link
Member Author

Choose a reason for hiding this comment

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

That could work as well. No big feelings either way. I suppose I thought it better to have it explain the why.

Copy link
Contributor

@okmttdhr okmttdhr Nov 21, 2024

Choose a reason for hiding this comment

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

@oandregal I believe the condition actions.length should be updated to actions.length === 1. The current implementation only displays the primary action when there is exactly one primary action, even if additional non-primary actions are present.

https://wordpress.github.io/gutenberg/?path=/story/dataviews-dataviews--default
Screenshot 2024-11-21 at 17 01 42

{
id: 'secondary',
label: 'Secondary action',
callback() {},
},
];

Copy link
Member Author

@oandregal oandregal Nov 21, 2024

Choose a reason for hiding this comment

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

Thanks, @okmttdhr! Prepared a fix at #67197 The fix will be part of 19.8, as this PR is, which hasn't been released yet.

}

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 ] }
/>
);
} );
}
Loading