From 63cf98ba23e7333deb3057d5c78d338a74fa8ad2 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 23 Jan 2024 18:31:13 +0000 Subject: [PATCH] Add: Selection and bulk actions to grid view. --- packages/dataviews/src/dataviews.js | 5 +- packages/dataviews/src/style.scss | 4 + packages/dataviews/src/view-grid.js | 136 ++++++++++++++++++---------- 3 files changed, 94 insertions(+), 51 deletions(-) diff --git a/packages/dataviews/src/dataviews.js b/packages/dataviews/src/dataviews.js index 64a70d46c7d127..272676e6491550 100644 --- a/packages/dataviews/src/dataviews.js +++ b/packages/dataviews/src/dataviews.js @@ -14,7 +14,7 @@ import Pagination from './pagination'; import ViewActions from './view-actions'; import Filters from './filters'; import Search from './search'; -import { VIEW_LAYOUTS, LAYOUT_TABLE } from './constants'; +import { VIEW_LAYOUTS, LAYOUT_TABLE, LAYOUT_GRID } from './constants'; import BulkActions from './bulk-actions'; const defaultGetItemId = ( item ) => item.id; @@ -93,7 +93,8 @@ export default function DataViews( { onChangeView={ onChangeView } /> - { view.type === LAYOUT_TABLE && ( + { ( view.type === LAYOUT_TABLE || + view.type === LAYOUT_GRID ) && ( field.id === view.layout.mediaField @@ -44,59 +51,90 @@ export default function ViewGrid( { alignment="top" className="dataviews-view-grid" > - { usedData.map( ( item ) => ( - -
- { mediaField?.render( { item } ) } -
- - - { primaryField?.render( { item } ) } - - - + { usedData.map( ( item ) => { + const id = getItemId( item ); + const isSelected = selection.includes( getItemId( item ) ); + return ( - { visibleFields.map( ( field ) => { - const renderedValue = field.render( { - item, - } ); - if ( ! renderedValue ) { - return null; + spacing={ 0 } + key={ id } + className={ classnames( 'dataviews-view-grid__card', { + 'is-selected': isSelected, + } ) } + onClick={ ( event ) => { + if ( event.ctrlKey || event.metaKey ) { + if ( ! isSelected ) { + onSelectionChange( + data.filter( ( _item ) => { + const itemId = getItemId?.( _item ); + return ( + itemId === id || + selection.includes( itemId ) + ); + } ) + ); + } else { + onSelectionChange( + data.filter( ( _item ) => { + const itemId = getItemId?.( _item ); + return ( + itemId !== id && + selection.includes( itemId ) + ); + } ) + ); + } } - return ( - - +
+ { mediaField?.render( { item } ) } +
+ + + { primaryField?.render( { item } ) } + + + + + { visibleFields.map( ( field ) => { + const renderedValue = field.render( { + item, + } ); + if ( ! renderedValue ) { + return null; + } + return ( + -
- { renderedValue } -
-
-
- ); - } ) } + +
+ { renderedValue } +
+
+
+ ); + } ) } +
- - ) ) } + ); + } ) } ); }