Skip to content

Commit

Permalink
Fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 29, 2023
1 parent 68cc432 commit c43ebb6
Showing 1 changed file with 94 additions and 37 deletions.
131 changes: 94 additions & 37 deletions packages/edit-site/src/components/dataviews/item-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
* WordPress dependencies
*/
import {
DropdownMenu,
MenuGroup,
MenuItem,
Button,
Modal,
__experimentalHStack as HStack,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { moreVertical } from '@wordpress/icons';

export function PrimaryActionTrigger( { action, onClick } ) {
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';

const {
DropdownMenuV2Ariakit: DropdownMenu,
DropdownMenuGroupV2Ariakit: DropdownMenuGroup,
DropdownMenuItemV2Ariakit: DropdownMenuItem,
DropdownMenuItemLabelV2Ariakit: DropdownMenuItemLabel,
} = unlock( componentsPrivateApis );

function ButtonTrigger( { action, onClick } ) {
return (
<Button
label={ action.label }
Expand All @@ -25,11 +35,11 @@ export function PrimaryActionTrigger( { action, onClick } ) {
);
}

function SecondaryActionTrigger( { action, onClick } ) {
function DropdownMenuItemTrigger( { action, onClick } ) {
return (
<MenuItem onClick={ onClick } isDestructive={ action.isDestructive }>
{ action.label }
</MenuItem>
<DropdownMenuItem onClick={ onClick }>
<DropdownMenuItemLabel>{ action.label }</DropdownMenuItemLabel>
</DropdownMenuItem>
);
}

Expand Down Expand Up @@ -68,7 +78,33 @@ export function ActionWithModal( { action, item, items, ActionTrigger } ) {
);
}

export default function ItemActions( { item, actions } ) {
function ActionsDropdownMenuGroup( { actions, item } ) {
return (
<DropdownMenuGroup>
{ actions.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
item={ item }
ActionTrigger={ DropdownMenuItemTrigger }
/>
);
}
return (
<DropdownMenuItemTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( item ) }
/>
);
} ) }
</DropdownMenuGroup>
);
}

export default function ItemActions( { item, actions, isCompact } ) {
const { primaryActions, secondaryActions } = useMemo( () => {
return actions.reduce(
( accumulator, action ) => {
Expand All @@ -90,6 +126,15 @@ export default function ItemActions( { item, actions } ) {
if ( ! primaryActions.length && ! secondaryActions.length ) {
return null;
}
if ( isCompact ) {
return (
<CompactItemActions
item={ item }
primaryActions={ primaryActions }
secondaryActions={ secondaryActions }
/>
);
}
return (
<HStack
spacing={ 1 }
Expand All @@ -107,15 +152,14 @@ export default function ItemActions( { item, actions } ) {
key={ action.id }
action={ action }
item={ item }
ActionTrigger={ PrimaryActionTrigger }
ActionTrigger={ ButtonTrigger }
/>
);
}
return (
<PrimaryActionTrigger
<ButtonTrigger
key={ action.id }
action={ action }
item={ item }
onClick={
action.isBulk
? () => action.callback( [ item ] )
Expand All @@ -125,37 +169,50 @@ export default function ItemActions( { item, actions } ) {
);
} ) }
{ !! secondaryActions.length && (
<DropdownMenu icon={ moreVertical } label={ __( 'Actions' ) }>
{ () => (
<MenuGroup>
{ secondaryActions.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
item={ item }
ActionTrigger={
SecondaryActionTrigger
}
<DropdownMenu
trigger={
<Button
size="compact"
icon={ moreVertical }
label={ __( 'Actions' ) }
/>
);
}
return (
<SecondaryActionTrigger
key={ action.id }
action={ action }
placement="bottom-end"
>
<ActionsDropdownMenuGroup
actions={ secondaryActions }
item={ item }
onClick={ () =>
action.callback( item )
}
/>
);
} ) }
</MenuGroup>
) }
</DropdownMenu>
) }
</HStack>
);
}

function CompactItemActions( { item, primaryActions, secondaryActions } ) {
return (
<DropdownMenu
trigger={
<Button
size="compact"
icon={ moreVertical }
label={ __( 'Actions' ) }
/>
}
placement="bottom-end"
>
{ !! primaryActions.length && (
<ActionsDropdownMenuGroup
actions={ primaryActions }
item={ item }
/>
) }
{ !! secondaryActions.length && (
<ActionsDropdownMenuGroup
actions={ secondaryActions }
item={ item }
/>
) }
</DropdownMenu>
);
}

0 comments on commit c43ebb6

Please sign in to comment.