@wordpress/dataviews 0.5.3 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/build/bulk-actions.js +41 -3
  2. package/build/bulk-actions.js.map +1 -1
  3. package/build/dataviews.js +22 -12
  4. package/build/dataviews.js.map +1 -1
  5. package/build/filters.js +8 -1
  6. package/build/filters.js.map +1 -1
  7. package/build/pagination.js +2 -1
  8. package/build/pagination.js.map +1 -1
  9. package/build/single-selection-checkbox.js +7 -2
  10. package/build/single-selection-checkbox.js.map +1 -1
  11. package/build/view-grid.js +6 -3
  12. package/build/view-grid.js.map +1 -1
  13. package/build/view-table.js +76 -41
  14. package/build/view-table.js.map +1 -1
  15. package/build-module/bulk-actions.js +40 -4
  16. package/build-module/bulk-actions.js.map +1 -1
  17. package/build-module/dataviews.js +22 -12
  18. package/build-module/dataviews.js.map +1 -1
  19. package/build-module/filters.js +8 -1
  20. package/build-module/filters.js.map +1 -1
  21. package/build-module/pagination.js +2 -1
  22. package/build-module/pagination.js.map +1 -1
  23. package/build-module/single-selection-checkbox.js +7 -2
  24. package/build-module/single-selection-checkbox.js.map +1 -1
  25. package/build-module/view-grid.js +6 -3
  26. package/build-module/view-grid.js.map +1 -1
  27. package/build-module/view-table.js +77 -42
  28. package/build-module/view-table.js.map +1 -1
  29. package/build-style/style-rtl.css +24 -14
  30. package/build-style/style.css +24 -14
  31. package/package.json +4 -4
  32. package/src/bulk-actions.js +54 -4
  33. package/src/dataviews.js +43 -27
  34. package/src/filters.js +6 -1
  35. package/src/pagination.js +6 -1
  36. package/src/single-selection-checkbox.js +7 -1
  37. package/src/style.scss +21 -12
  38. package/src/view-grid.js +6 -2
  39. package/src/view-table.js +109 -75
package/src/view-table.js CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  useState,
24
24
  Children,
25
25
  Fragment,
26
+ useMemo,
26
27
  } from '@wordpress/element';
27
28
 
28
29
  /**
@@ -33,6 +34,10 @@ import { unlock } from './lock-unlock';
33
34
  import ItemActions from './item-actions';
34
35
  import { sanitizeOperators } from './utils';
35
36
  import { ENUMERATION_TYPE, SORTING_DIRECTIONS } from './constants';
37
+ import {
38
+ useSomeItemHasAPossibleBulkAction,
39
+ useHasAPossibleBulkAction,
40
+ } from './bulk-actions';
36
41
 
37
42
  const {
38
43
  DropdownMenuV2: DropdownMenu,
@@ -186,8 +191,20 @@ const HeaderMenu = forwardRef( function HeaderMenu(
186
191
  );
187
192
  } );
188
193
 
189
- function BulkSelectionCheckbox( { selection, onSelectionChange, data } ) {
190
- const areAllSelected = selection.length === data.length;
194
+ function BulkSelectionCheckbox( {
195
+ selection,
196
+ onSelectionChange,
197
+ data,
198
+ actions,
199
+ } ) {
200
+ const selectableItems = useMemo( () => {
201
+ return data.filter( ( item ) => {
202
+ return actions.some(
203
+ ( action ) => action.supportsBulk && action.isEligible( item )
204
+ );
205
+ } );
206
+ }, [ data, actions ] );
207
+ const areAllSelected = selection.length === selectableItems.length;
191
208
  return (
192
209
  <CheckboxControl
193
210
  className="dataviews-view-table-selection-checkbox"
@@ -198,7 +215,7 @@ function BulkSelectionCheckbox( { selection, onSelectionChange, data } ) {
198
215
  if ( areAllSelected ) {
199
216
  onSelectionChange( [] );
200
217
  } else {
201
- onSelectionChange( data );
218
+ onSelectionChange( selectableItems );
202
219
  }
203
220
  } }
204
221
  label={ areAllSelected ? __( 'Deselect all' ) : __( 'Select all' ) }
@@ -206,6 +223,81 @@ function BulkSelectionCheckbox( { selection, onSelectionChange, data } ) {
206
223
  );
207
224
  }
208
225
 
226
+ function TableRow( {
227
+ hasBulkActions,
228
+ item,
229
+ actions,
230
+ id,
231
+ visibleFields,
232
+ primaryField,
233
+ selection,
234
+ getItemId,
235
+ onSelectionChange,
236
+ data,
237
+ } ) {
238
+ const hasPossibleBulkAction = useHasAPossibleBulkAction( actions, item );
239
+ return (
240
+ <tr
241
+ className={ classnames( 'dataviews-view-table__row', {
242
+ 'is-selected':
243
+ hasPossibleBulkAction && selection.includes( id ),
244
+ } ) }
245
+ >
246
+ { hasBulkActions && (
247
+ <td
248
+ className="dataviews-view-table__checkbox-column"
249
+ style={ {
250
+ width: 20,
251
+ minWidth: 20,
252
+ } }
253
+ >
254
+ <div className="dataviews-view-table__cell-content-wrapper">
255
+ <SingleSelectionCheckbox
256
+ id={ id }
257
+ item={ item }
258
+ selection={ selection }
259
+ onSelectionChange={ onSelectionChange }
260
+ getItemId={ getItemId }
261
+ data={ data }
262
+ primaryField={ primaryField }
263
+ disabled={ ! hasPossibleBulkAction }
264
+ />
265
+ </div>
266
+ </td>
267
+ ) }
268
+ { visibleFields.map( ( field ) => (
269
+ <td
270
+ key={ field.id }
271
+ style={ {
272
+ width: field.width || undefined,
273
+ minWidth: field.minWidth || undefined,
274
+ maxWidth: field.maxWidth || undefined,
275
+ } }
276
+ >
277
+ <div
278
+ className={ classnames(
279
+ 'dataviews-view-table__cell-content-wrapper',
280
+ {
281
+ 'dataviews-view-table__primary-field':
282
+ primaryField?.id === field.id,
283
+ }
284
+ ) }
285
+ >
286
+ { field.render( {
287
+ item,
288
+ } ) }
289
+ </div>
290
+ </td>
291
+ ) ) }
292
+ { !! actions?.length && (
293
+ <td className="dataviews-view-table__actions-column">
294
+ <ItemActions item={ item } actions={ actions } />
295
+ </td>
296
+ ) }
297
+ </tr>
298
+ );
299
+ }
300
+
209
301
  function ViewTable( {
210
302
  view,
211
303
  onChangeView,
@@ -219,10 +311,10 @@ function ViewTable( {
219
311
  onSelectionChange,
220
312
  setOpenedFilter,
221
313
  } ) {
222
- const hasBulkActions = actions?.some( ( action ) => action.supportsBulk );
223
314
  const headerMenuRefs = useRef( new Map() );
224
315
  const headerMenuToFocusRef = useRef();
225
316
  const [ nextHeaderMenuToFocus, setNextHeaderMenuToFocus ] = useState();
317
+ const hasBulkActions = useSomeItemHasAPossibleBulkAction( actions, data );
226
318
 
227
319
  useEffect( () => {
228
320
  if ( headerMenuToFocusRef.current ) {
@@ -285,6 +377,7 @@ function ViewTable( {
285
377
  selection={ selection }
286
378
  onSelectionChange={ onSelectionChange }
287
379
  data={ data }
380
+ actions={ actions }
288
381
  />
289
382
  </th>
290
383
  ) }
@@ -347,78 +440,19 @@ function ViewTable( {
347
440
  <tbody>
348
441
  { hasData &&
349
442
  usedData.map( ( item, index ) => (
350
- <tr
443
+ <TableRow
351
444
  key={ getItemId( item ) }
352
- className={ classnames(
353
- 'dataviews-view-table__row',
354
- {
355
- 'is-selected': selection.includes(
356
- getItemId( item ) || index
357
- ),
358
- }
359
- ) }
360
- >
361
- { hasBulkActions && (
362
- <td
363
- className="dataviews-view-table__checkbox-column"
364
- style={ {
365
- width: 20,
366
- minWidth: 20,
367
- } }
368
- >
369
- <div className="dataviews-view-table__cell-content-wrapper">
370
- <SingleSelectionCheckbox
371
- id={
372
- getItemId( item ) || index
373
- }
374
- item={ item }
375
- selection={ selection }
376
- onSelectionChange={
377
- onSelectionChange
378
- }
379
- getItemId={ getItemId }
380
- data={ data }
381
- primaryField={ primaryField }
382
- />
383
- </div>
384
- </td>
385
- ) }
386
- { visibleFields.map( ( field ) => (
387
- <td
388
- key={ field.id }
389
- style={ {
390
- width: field.width || undefined,
391
- minWidth:
392
- field.minWidth || undefined,
393
- maxWidth:
394
- field.maxWidth || undefined,
395
- } }
396
- >
397
- <div
398
- className={ classnames(
399
- 'dataviews-view-table__cell-content-wrapper',
400
- {
401
- 'dataviews-view-table__primary-field':
402
- primaryField?.id ===
403
- field.id,
404
- }
405
- ) }
406
- >
407
- { field.render( {
408
- item,
409
- } ) }
410
- </div>
411
- </td>
412
- ) ) }
413
- { !! actions?.length && (
414
- <td className="dataviews-view-table__actions-column">
415
- <ItemActions
416
- item={ item }
417
- actions={ actions }
418
- />
419
- </td>
420
- ) }
421
- </tr>
445
+ item={ item }
446
+ hasBulkActions={ hasBulkActions }
447
+ actions={ actions }
448
+ id={ getItemId( item ) || index }
449
+ visibleFields={ visibleFields }
450
+ primaryField={ primaryField }
451
+ selection={ selection }
452
+ getItemId={ getItemId }
453
+ onSelectionChange={ onSelectionChange }
454
+ data={ data }
455
+ />
422
456
  ) ) }
423
457
  </tbody>
424
458
  </table>