-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
partial feedback (+7 squashed commits)
Squashed commits: [43c3d85] Fix rebase [cb954b6] Fix rebase [0a3723a] add selection label functionality [3c48c68] Add labels [2ae6b76] Lint fixes [77dc74c] Feedback [d0d1456] Add: Bulk actions API and trash action.
- Loading branch information
1 parent
308fc42
commit 8e0ab84
Showing
8 changed files
with
336 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
ToolbarButton, | ||
Toolbar, | ||
ToolbarGroup, | ||
Popover, | ||
} from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __, _n, sprintf } from '@wordpress/i18n'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ActionWithModal } from './item-actions'; | ||
|
||
function PrimaryActionTrigger( { action, onClick } ) { | ||
return ( | ||
<ToolbarButton | ||
label={ action.label } | ||
icon={ action.icon } | ||
isDestructive={ action.isDestructive } | ||
size="compact" | ||
onClick={ onClick } | ||
/> | ||
); | ||
} | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
export default function BulkActions( { | ||
data, | ||
selection, | ||
actions = EMPTY_ARRAY, | ||
setSelection, | ||
} ) { | ||
const items = useMemo( | ||
() => | ||
data?.filter( ( item ) => selection?.includes( item.id ) ) ?? | ||
EMPTY_ARRAY, | ||
[ data, selection ] | ||
); | ||
const primaryActions = useMemo( | ||
() => | ||
actions.filter( ( action ) => { | ||
return ( | ||
action.isBulk && | ||
action.isPrimary && | ||
items.every( ( item ) => action.isEligible( item ) ) | ||
); | ||
} ), | ||
[ actions, items ] | ||
); | ||
|
||
if ( | ||
( selection && selection.length === 0 ) || | ||
primaryActions.length === 0 | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Popover | ||
placement="top-middle" | ||
className="dataviews-bulk-actions-popover" | ||
> | ||
<Toolbar label="Bulk actions"> | ||
<div className="dataviews-bulk-actions-toolbar-wrapper"> | ||
<ToolbarGroup> | ||
<ToolbarButton onClick={ () => {} } disabled={ true }> | ||
{ | ||
// translators: %s: Total number of selected items. | ||
sprintf( | ||
// translators: %s: Total number of selected items. | ||
_n( | ||
'%s item selected', | ||
'%s items selected', | ||
selection.length | ||
), | ||
selection.length | ||
) | ||
} | ||
</ToolbarButton> | ||
<ToolbarButton | ||
onClick={ () => { | ||
setSelection( EMPTY_ARRAY ); | ||
} } | ||
> | ||
{ __( 'Deselect' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
<ToolbarGroup> | ||
{ primaryActions.map( ( action ) => { | ||
if ( !! action.RenderModal ) { | ||
return ( | ||
<ActionWithModal | ||
key={ action.id } | ||
action={ action } | ||
items={ items } | ||
ActionTrigger={ PrimaryActionTrigger } | ||
/> | ||
); | ||
} | ||
return ( | ||
<PrimaryActionTrigger | ||
key={ action.id } | ||
action={ action } | ||
onClick={ () => action.callback( items ) } | ||
/> | ||
); | ||
} ) } | ||
</ToolbarGroup> | ||
</div> | ||
</Toolbar> | ||
</Popover> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.