Skip to content

Commit

Permalink
Simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 12, 2024
1 parent 1258c8b commit 396bf6b
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 254 deletions.
7 changes: 3 additions & 4 deletions packages/dataviews/src/bulk-actions-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ function ActionButton< Item extends AnyItem >( {
action={ action }
onClick={ () => {
setActionInProgress( action.id );
const maybeThunk = action.callback( selectedItems );
if ( typeof maybeThunk === 'function' ) {
maybeThunk( { registry } );
}
action.callback( selectedItems, {
registry,
} );
} }
items={ selectedEligibleItems }
isBusy={ actionInProgress === action.id }
Expand Down
5 changes: 1 addition & 4 deletions packages/dataviews/src/bulk-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ function BulkActionItem< Item extends AnyItem >( {
if ( shouldShowModal ) {
setActionWithModal( action );
} else {
const maybeThunk = action.callback( eligibleItems );
if ( typeof maybeThunk === 'function' ) {
maybeThunk( { registry } );
}
action.callback( eligibleItems, { registry } );
}
} }
suffix={
Expand Down
17 changes: 3 additions & 14 deletions packages/dataviews/src/item-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ export function ActionModal< Item extends AnyItem >( {
action.id
) }` }
>
<action.RenderModal
items={ items }
closeModal={ closeModal }
onActionStart={ action.onActionStart }
onActionPerformed={ action.onActionPerformed }
/>
<action.RenderModal items={ items } closeModal={ closeModal } />
</Modal>
);
}
Expand Down Expand Up @@ -179,10 +174,7 @@ export function ActionsDropdownMenuGroup< Item extends AnyItem >( {
key={ action.id }
action={ action }
onClick={ () => {
const maybeThunk = action.callback( [ item ] );
if ( typeof maybeThunk === 'function' ) {
maybeThunk( { registry } );
}
action.callback( [ item ], { registry } );
} }
items={ [ item ] }
/>
Expand Down Expand Up @@ -242,10 +234,7 @@ export default function ItemActions< Item extends AnyItem >( {
key={ action.id }
action={ action }
onClick={ () => {
const maybeThunk = action.callback( [ item ] );
if ( typeof maybeThunk === 'function' ) {
maybeThunk( { registry } );
}
action.callback( [ item ], { registry } );
} }
items={ [ item ] }
/>
Expand Down
20 changes: 6 additions & 14 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,28 +327,16 @@ interface ActionBase< Item extends AnyItem > {

export interface ActionModal< Item extends AnyItem >
extends ActionBase< Item > {
/**
* The callback to execute when the action has finished.
*/
onActionPerformed: ( ( items: Item[] ) => void ) | undefined;

/**
* The callback to execute when the action is triggered.
*/
onActionStart: ( ( items: Item[] ) => void ) | undefined;

/**
* Modal to render when the action is triggered.
*/
RenderModal: ( {
items,
closeModal,
onActionStart,
onActionPerformed,
}: {
items: Item[];
closeModal?: () => void;
onActionStart?: ( items: Item[] ) => void;
onActionPerformed?: ( items: Item[] ) => void;
} ) => ReactElement;

Expand All @@ -369,8 +357,12 @@ export interface ActionButton< Item extends AnyItem >
* The callback to execute when the action is triggered.
*/
callback: (
items: Item[]
) => void | ( ( { registry }: { registry: any } ) => void );
items: Item[],
context: {
registry: any;
onActionPerformed?: ( items: Item[] ) => void;
}
) => void;
}

export type Action< Item extends AnyItem > =
Expand Down
16 changes: 4 additions & 12 deletions packages/dataviews/src/view-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,10 @@ function ListItem< Item extends AnyItem >( {
}
size="compact"
onClick={ () => {
const maybeThunk =
primaryAction.callback(
[ item ]
);
if (
typeof maybeThunk ===
'function'
) {
maybeThunk( {
registry,
} );
}
primaryAction.callback(
[ item ],
{ registry }
);
} }
/>
}
Expand Down
Loading

0 comments on commit 396bf6b

Please sign in to comment.