Skip to content

Commit

Permalink
Add pinning and unpinning actions
Browse files Browse the repository at this point in the history
  • Loading branch information
vcanales committed Sep 25, 2024
1 parent 2b7019f commit 57c995e
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import DataViewsViewConfig from '../dataviews-view-config';
import { normalizeFields } from '../../normalize-fields';
import type { Action, Field, View, SupportedLayouts } from '../../types';
import type { SelectionOrUpdater } from '../../private-types';
import { __ } from '@wordpress/i18n';

type ItemWithId = { id: string };

Expand Down Expand Up @@ -135,13 +136,48 @@ export default function DataViews< Item >( {
} );
}, [ data, getItemId, combinedPinnedItems ] );

const createDefaultActions = useCallback(
( callback: ( items: Item[], context: any ) => void ) => {
return [
{
id: 'pin',
label: __( 'Pin' ),
callback: ( items: Item[], context: any ) => {
items.forEach( ( item ) =>
onPinItem( getItemId( item ) )
);
callback( items, context );
},
},
{
id: 'unpin',
label: __( 'Unpin' ),
callback: ( items: Item[], context: any ) => {
items.forEach( ( item ) =>
onUnpinItem( getItemId( item ) )
);
callback( items, context );
},
},
];
},
[ getItemId, onPinItem, onUnpinItem ]
);

const actionsWithDefaultActions = useMemo( () => {
const defaultActions = createDefaultActions(
( items, context ) => context.onActionPerformed?.( items )
);
return [ ...defaultActions, ...actions ];
}, [ actions, createDefaultActions ] );

return (
<DataViewsContext.Provider
value={ {
view,
onChangeView,
fields: _fields,
actions,
actions: actionsWithDefaultActions,
data: dataWithPinnedItems,
isLoading,
paginationInfo,
Expand Down

0 comments on commit 57c995e

Please sign in to comment.