@wordpress/dataviews 1.1.0 → 2.0.0

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 (176) hide show
  1. package/CHANGELOG.md +27 -5
  2. package/README.md +33 -30
  3. package/build/add-filter.js +30 -22
  4. package/build/add-filter.js.map +1 -1
  5. package/build/bulk-actions-toolbar.js +187 -0
  6. package/build/bulk-actions-toolbar.js.map +1 -0
  7. package/build/bulk-actions.js +75 -62
  8. package/build/bulk-actions.js.map +1 -1
  9. package/build/constants.js +17 -10
  10. package/build/constants.js.map +1 -1
  11. package/build/dataviews.js +64 -50
  12. package/build/dataviews.js.map +1 -1
  13. package/build/filter-and-sort-data-view.js +2 -2
  14. package/build/filter-and-sort-data-view.js.map +1 -1
  15. package/build/filter-summary.js +106 -96
  16. package/build/filter-summary.js.map +1 -1
  17. package/build/filters.js +18 -17
  18. package/build/filters.js.map +1 -1
  19. package/build/index.js.map +1 -1
  20. package/build/item-actions.js +101 -69
  21. package/build/item-actions.js.map +1 -1
  22. package/build/layouts.js.map +1 -1
  23. package/build/lock-unlock.js.map +1 -1
  24. package/build/normalize-fields.js.map +1 -1
  25. package/build/pagination.js +66 -57
  26. package/build/pagination.js.map +1 -1
  27. package/build/reset-filters.js +9 -4
  28. package/build/reset-filters.js.map +1 -1
  29. package/build/search-widget.js +108 -89
  30. package/build/search-widget.js.map +1 -1
  31. package/build/search.js +13 -6
  32. package/build/search.js.map +1 -1
  33. package/build/single-selection-checkbox.js +6 -2
  34. package/build/single-selection-checkbox.js.map +1 -1
  35. package/build/types.js.map +1 -1
  36. package/build/utils.js +3 -15
  37. package/build/utils.js.map +1 -1
  38. package/build/view-actions.js +168 -120
  39. package/build/view-actions.js.map +1 -1
  40. package/build/view-grid.js +119 -106
  41. package/build/view-grid.js.map +1 -1
  42. package/build/view-list.js +217 -83
  43. package/build/view-list.js.map +1 -1
  44. package/build/view-table.js +227 -199
  45. package/build/view-table.js.map +1 -1
  46. package/build-module/add-filter.js +30 -22
  47. package/build-module/add-filter.js.map +1 -1
  48. package/build-module/bulk-actions-toolbar.js +182 -0
  49. package/build-module/bulk-actions-toolbar.js.map +1 -0
  50. package/build-module/bulk-actions.js +77 -62
  51. package/build-module/bulk-actions.js.map +1 -1
  52. package/build-module/constants.js +16 -9
  53. package/build-module/constants.js.map +1 -1
  54. package/build-module/dataviews.js +65 -50
  55. package/build-module/dataviews.js.map +1 -1
  56. package/build-module/filter-and-sort-data-view.js +2 -2
  57. package/build-module/filter-and-sort-data-view.js.map +1 -1
  58. package/build-module/filter-summary.js +107 -97
  59. package/build-module/filter-summary.js.map +1 -1
  60. package/build-module/filters.js +18 -17
  61. package/build-module/filters.js.map +1 -1
  62. package/build-module/index.js.map +1 -1
  63. package/build-module/item-actions.js +102 -71
  64. package/build-module/item-actions.js.map +1 -1
  65. package/build-module/layouts.js.map +1 -1
  66. package/build-module/lock-unlock.js.map +1 -1
  67. package/build-module/normalize-fields.js.map +1 -1
  68. package/build-module/pagination.js +67 -57
  69. package/build-module/pagination.js.map +1 -1
  70. package/build-module/reset-filters.js +9 -4
  71. package/build-module/reset-filters.js.map +1 -1
  72. package/build-module/search-widget.js +109 -89
  73. package/build-module/search-widget.js.map +1 -1
  74. package/build-module/search.js +13 -6
  75. package/build-module/search.js.map +1 -1
  76. package/build-module/single-selection-checkbox.js +6 -2
  77. package/build-module/single-selection-checkbox.js.map +1 -1
  78. package/build-module/types.js.map +1 -1
  79. package/build-module/utils.js +2 -13
  80. package/build-module/utils.js.map +1 -1
  81. package/build-module/view-actions.js +170 -121
  82. package/build-module/view-actions.js.map +1 -1
  83. package/build-module/view-grid.js +121 -106
  84. package/build-module/view-grid.js.map +1 -1
  85. package/build-module/view-list.js +219 -85
  86. package/build-module/view-list.js.map +1 -1
  87. package/build-module/view-table.js +230 -201
  88. package/build-module/view-table.js.map +1 -1
  89. package/build-style/style-rtl.css +168 -44
  90. package/build-style/style.css +168 -44
  91. package/build-types/add-filter.d.ts +11 -0
  92. package/build-types/add-filter.d.ts.map +1 -0
  93. package/build-types/bulk-actions-toolbar.d.ts +12 -0
  94. package/build-types/bulk-actions-toolbar.d.ts.map +1 -0
  95. package/build-types/bulk-actions.d.ts +14 -0
  96. package/build-types/bulk-actions.d.ts.map +1 -0
  97. package/build-types/constants.d.ts +19 -32
  98. package/build-types/constants.d.ts.map +1 -1
  99. package/build-types/dataviews.d.ts +22 -0
  100. package/build-types/dataviews.d.ts.map +1 -0
  101. package/build-types/filter-and-sort-data-view.d.ts +3 -3
  102. package/build-types/filter-and-sort-data-view.d.ts.map +1 -1
  103. package/build-types/filter-summary.d.ts +14 -0
  104. package/build-types/filter-summary.d.ts.map +1 -0
  105. package/build-types/filters.d.ts +13 -0
  106. package/build-types/filters.d.ts.map +1 -0
  107. package/build-types/index.d.ts +4 -0
  108. package/build-types/index.d.ts.map +1 -0
  109. package/build-types/item-actions.d.ts +35 -0
  110. package/build-types/item-actions.d.ts.map +1 -0
  111. package/build-types/layouts.d.ts +24 -0
  112. package/build-types/layouts.d.ts.map +1 -0
  113. package/build-types/lock-unlock.d.ts +2 -0
  114. package/build-types/lock-unlock.d.ts.map +1 -0
  115. package/build-types/normalize-fields.d.ts +2 -2
  116. package/build-types/normalize-fields.d.ts.map +1 -1
  117. package/build-types/pagination.d.ts +16 -0
  118. package/build-types/pagination.d.ts.map +1 -0
  119. package/build-types/reset-filters.d.ts +13 -0
  120. package/build-types/reset-filters.d.ts.map +1 -0
  121. package/build-types/search-widget.d.ts +10 -0
  122. package/build-types/search-widget.d.ts.map +1 -0
  123. package/build-types/search.d.ts +13 -0
  124. package/build-types/search.d.ts.map +1 -0
  125. package/build-types/single-selection-checkbox.d.ts +17 -0
  126. package/build-types/single-selection-checkbox.d.ts.map +1 -0
  127. package/build-types/stories/fixtures.d.ts +114 -0
  128. package/build-types/stories/fixtures.d.ts.map +1 -0
  129. package/build-types/stories/index.story.d.ts +15 -0
  130. package/build-types/stories/index.story.d.ts.map +1 -0
  131. package/build-types/types.d.ts +221 -21
  132. package/build-types/types.d.ts.map +1 -1
  133. package/build-types/utils.d.ts +3 -0
  134. package/build-types/utils.d.ts.map +1 -0
  135. package/build-types/view-actions.d.ts +12 -0
  136. package/build-types/view-actions.d.ts.map +1 -0
  137. package/build-types/view-grid.d.ts +4 -0
  138. package/build-types/view-grid.d.ts.map +1 -0
  139. package/build-types/view-list.d.ts +4 -0
  140. package/build-types/view-list.d.ts.map +1 -0
  141. package/build-types/view-table.d.ts +5 -0
  142. package/build-types/view-table.d.ts.map +1 -0
  143. package/package.json +12 -13
  144. package/src/{add-filter.js → add-filter.tsx} +17 -1
  145. package/src/bulk-actions-toolbar.tsx +272 -0
  146. package/src/{bulk-actions.js → bulk-actions.tsx} +77 -17
  147. package/src/constants.ts +12 -5
  148. package/src/{dataviews.js → dataviews.tsx} +54 -14
  149. package/src/filter-and-sort-data-view.ts +13 -8
  150. package/src/{filter-summary.js → filter-summary.tsx} +38 -9
  151. package/src/{filters.js → filters.tsx} +18 -6
  152. package/src/{item-actions.js → item-actions.tsx} +119 -30
  153. package/src/normalize-fields.ts +4 -2
  154. package/src/{pagination.js → pagination.tsx} +29 -8
  155. package/src/{reset-filters.js → reset-filters.tsx} +17 -2
  156. package/src/{search-widget.js → search-widget.tsx} +27 -7
  157. package/src/{search.js → search.tsx} +22 -5
  158. package/src/{single-selection-checkbox.js → single-selection-checkbox.tsx} +17 -2
  159. package/src/style.scss +166 -43
  160. package/src/types.ts +286 -21
  161. package/src/{utils.js → utils.ts} +5 -13
  162. package/src/{view-actions.js → view-actions.tsx} +105 -49
  163. package/src/{view-grid.js → view-grid.tsx} +31 -18
  164. package/src/view-list.tsx +410 -0
  165. package/src/{view-table.js → view-table.tsx} +99 -40
  166. package/tsconfig.json +3 -4
  167. package/tsconfig.tsbuildinfo +1 -1
  168. package/build/dropdown-menu-helper.js +0 -71
  169. package/build/dropdown-menu-helper.js.map +0 -1
  170. package/build-module/dropdown-menu-helper.js +0 -64
  171. package/build-module/dropdown-menu-helper.js.map +0 -1
  172. package/src/dropdown-menu-helper.js +0 -61
  173. package/src/view-list.js +0 -207
  174. /package/src/{index.js → index.ts} +0 -0
  175. /package/src/{layouts.js → layouts.ts} +0 -0
  176. /package/src/{lock-unlock.js → lock-unlock.ts} +0 -0
package/src/types.ts CHANGED
@@ -1,23 +1,48 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import type { ReactNode } from 'react';
4
+ import type { ReactElement, ReactNode } from 'react';
5
5
 
6
- type Item = Record< string, any >;
6
+ export type SortDirection = 'asc' | 'desc';
7
7
 
8
- interface Option {
9
- value: any;
8
+ /**
9
+ * Generic option type.
10
+ */
11
+ export interface Option< Value extends any = any > {
12
+ value: Value;
10
13
  label: string;
14
+ description?: string;
11
15
  }
12
16
 
13
- interface filterByConfig {
17
+ interface FilterByConfig {
18
+ /**
19
+ * The list of operators supported by the field.
20
+ */
14
21
  operators?: Operator[];
22
+
23
+ /**
24
+ * Whether it is a primary filter.
25
+ *
26
+ * A primary filter is always visible and is not listed in the "Add filter" component,
27
+ * except for the list layout where it behaves like a secondary filter.
28
+ */
15
29
  isPrimary?: boolean;
16
30
  }
17
31
 
18
- type Operator = 'is' | 'isNot' | 'isAny' | 'isNone' | 'isAll' | 'isNotAll';
32
+ export type Operator =
33
+ | 'is'
34
+ | 'isNot'
35
+ | 'isAny'
36
+ | 'isNone'
37
+ | 'isAll'
38
+ | 'isNotAll';
39
+
40
+ export type AnyItem = Record< string, any >;
19
41
 
20
- export interface Field {
42
+ /**
43
+ * A dataview field for a specific property of a data type.
44
+ */
45
+ export interface Field< Item extends AnyItem > {
21
46
  /**
22
47
  * The unique identifier of the field.
23
48
  */
@@ -32,58 +57,67 @@ export interface Field {
32
57
  * Callback used to retrieve the value of the field from the item.
33
58
  * Defaults to `item[ field.id ]`.
34
59
  */
35
- getValue?: ( { item }: { item: Item } ) => any;
60
+ getValue?: ( args: { item: Item } ) => any;
36
61
 
37
62
  /**
38
63
  * Callback used to render the field. Defaults to `field.getValue`.
39
64
  */
40
- render?: ( { item }: { item: Item } ) => ReactNode;
65
+ render?: ( args: { item: Item } ) => ReactNode;
41
66
 
42
67
  /**
43
68
  * The width of the field column.
44
69
  */
45
- width: string | number | undefined;
70
+ width?: string | number;
46
71
 
47
72
  /**
48
73
  * The minimum width of the field column.
49
74
  */
50
- maxWidth: string | number | undefined;
75
+ maxWidth?: string | number;
51
76
 
52
77
  /**
53
78
  * The maximum width of the field column.
54
79
  */
55
- minWidth: string | number | undefined;
80
+ minWidth?: string | number;
56
81
 
57
82
  /**
58
83
  * Whether the field is sortable.
59
84
  */
60
- enableSorting: boolean | undefined;
85
+ enableSorting?: boolean;
61
86
 
62
87
  /**
63
88
  * Whether the field is searchable.
64
89
  */
65
- enableGlobalSearch: boolean | undefined;
90
+ enableGlobalSearch?: boolean;
66
91
 
67
92
  /**
68
93
  * Whether the field is filterable.
69
94
  */
70
- enableHiding: boolean | undefined;
95
+ enableHiding?: boolean;
71
96
 
72
97
  /**
73
98
  * The list of options to pick from when using the field as a filter.
74
99
  */
75
- elements: Option[] | undefined;
100
+ elements?: Option[];
76
101
 
77
102
  /**
78
103
  * Filter config for the field.
79
104
  */
80
- filterBy: filterByConfig | undefined;
105
+ filterBy?: FilterByConfig | undefined;
81
106
  }
82
107
 
83
- export type NormalizedField = Required< Field >;
108
+ export type NormalizedField< Item extends AnyItem > = Field< Item > &
109
+ Required< Pick< Field< Item >, 'header' | 'getValue' | 'render' > >;
110
+
111
+ /**
112
+ * A collection of dataview fields for a data type.
113
+ */
114
+ export type Fields< Item extends AnyItem > = Field< Item >[];
84
115
 
85
- export type Data = Item[];
116
+ export type Data< Item extends AnyItem > = Item[];
86
117
 
118
+ /**
119
+ * The filters applied to the dataset.
120
+ */
87
121
  export interface Filter {
88
122
  /**
89
123
  * The field to filter by.
@@ -101,7 +135,44 @@ export interface Filter {
101
135
  value: any;
102
136
  }
103
137
 
104
- export interface View {
138
+ export interface NormalizedFilter {
139
+ /**
140
+ * The field to filter by.
141
+ */
142
+ field: string;
143
+
144
+ /**
145
+ * The field name.
146
+ */
147
+ name: string;
148
+
149
+ /**
150
+ * The list of options to pick from when using the field as a filter.
151
+ */
152
+ elements: Option[];
153
+
154
+ /**
155
+ * Is a single selection filter.
156
+ */
157
+ singleSelection: boolean;
158
+
159
+ /**
160
+ * The list of operators supported by the field.
161
+ */
162
+ operators: Operator[];
163
+
164
+ /**
165
+ * Whether the filter is visible.
166
+ */
167
+ isVisible: boolean;
168
+
169
+ /**
170
+ * Whether it is a primary filter.
171
+ */
172
+ isPrimary: boolean;
173
+ }
174
+
175
+ interface ViewBase {
105
176
  /**
106
177
  * The layout of the view.
107
178
  */
@@ -129,7 +200,7 @@ export interface View {
129
200
  /**
130
201
  * The direction to sort by.
131
202
  */
132
- direction: string;
203
+ direction: SortDirection;
133
204
  };
134
205
 
135
206
  /**
@@ -141,4 +212,198 @@ export interface View {
141
212
  * The number of items per page
142
213
  */
143
214
  perPage?: number;
215
+
216
+ /**
217
+ * The hidden fields.
218
+ */
219
+ hiddenFields: string[];
220
+ }
221
+
222
+ export interface ViewTable extends ViewBase {
223
+ type: 'table';
224
+
225
+ layout: {
226
+ /**
227
+ * The field to use as the primary field.
228
+ */
229
+ primaryField?: string;
230
+
231
+ /**
232
+ * The field to use as the media field.
233
+ */
234
+ mediaField?: string;
235
+ };
144
236
  }
237
+
238
+ export interface ViewList extends ViewBase {
239
+ type: 'list';
240
+
241
+ layout: {
242
+ /**
243
+ * The field to use as the primary field.
244
+ */
245
+ primaryField?: string;
246
+
247
+ /**
248
+ * The field to use as the media field.
249
+ */
250
+ mediaField?: string;
251
+ };
252
+ }
253
+
254
+ export interface ViewGrid extends ViewBase {
255
+ type: 'grid';
256
+
257
+ layout: {
258
+ /**
259
+ * The field to use as the primary field.
260
+ */
261
+ primaryField?: string;
262
+
263
+ /**
264
+ * The field to use as the media field.
265
+ */
266
+ mediaField?: string;
267
+
268
+ /**
269
+ * The fields to use as columns.
270
+ */
271
+ columnFields?: string[];
272
+
273
+ /**
274
+ * The fields to use as badge fields.
275
+ */
276
+ badgeFields?: string[];
277
+ };
278
+ }
279
+
280
+ export type View = ViewList | ViewGrid | ViewTable;
281
+
282
+ interface ActionBase< Item extends AnyItem > {
283
+ /**
284
+ * The unique identifier of the action.
285
+ */
286
+ id: string;
287
+
288
+ /**
289
+ * The label of the action.
290
+ * In case we want to adjust the label based on the selected items,
291
+ * a function can be provided.
292
+ */
293
+ label: string | ( ( items: Item[] ) => string );
294
+
295
+ /**
296
+ * The icon of the action. (Either a string or an SVG element)
297
+ * This should be IconType from the components package
298
+ * but that import is breaking typescript build for the moment.
299
+ */
300
+ icon?: any;
301
+
302
+ /**
303
+ * Whether the action is disabled.
304
+ */
305
+ disabled?: boolean;
306
+
307
+ /**
308
+ * Whether the action is destructive.
309
+ */
310
+ isDestructive?: boolean;
311
+
312
+ /**
313
+ * Whether the action is a primary action.
314
+ */
315
+ isPrimary?: boolean;
316
+
317
+ /**
318
+ * Whether the item passed as an argument supports the current action.
319
+ */
320
+ isEligible?: ( item: Item ) => boolean;
321
+
322
+ /**
323
+ * Whether the action can be used as a bulk action.
324
+ */
325
+ supportsBulk?: boolean;
326
+ }
327
+
328
+ export interface ActionModal< Item extends AnyItem >
329
+ extends ActionBase< Item > {
330
+ /**
331
+ * The callback to execute when the action has finished.
332
+ */
333
+ onActionPerformed: ( ( items: Item[] ) => void ) | undefined;
334
+
335
+ /**
336
+ * The callback to execute when the action is triggered.
337
+ */
338
+ onActionStart: ( ( items: Item[] ) => void ) | undefined;
339
+
340
+ /**
341
+ * Modal to render when the action is triggered.
342
+ */
343
+ RenderModal: ( {
344
+ items,
345
+ closeModal,
346
+ onActionStart,
347
+ onActionPerformed,
348
+ }: {
349
+ items: Item[];
350
+ closeModal?: () => void;
351
+ onActionStart?: ( items: Item[] ) => void;
352
+ onActionPerformed?: ( items: Item[] ) => void;
353
+ } ) => ReactElement;
354
+
355
+ /**
356
+ * Whether to hide the modal header.
357
+ */
358
+ hideModalHeader?: boolean;
359
+
360
+ /**
361
+ * The header of the modal.
362
+ */
363
+ modalHeader?: string;
364
+ }
365
+
366
+ export interface ActionButton< Item extends AnyItem >
367
+ extends ActionBase< AnyItem > {
368
+ /**
369
+ * The callback to execute when the action is triggered.
370
+ */
371
+ callback: ( items: Item[] ) => void;
372
+ }
373
+
374
+ export type Action< Item extends AnyItem > =
375
+ | ActionModal< Item >
376
+ | ActionButton< Item >;
377
+
378
+ export interface ViewBaseProps< Item extends AnyItem > {
379
+ actions: Action< Item >[];
380
+ data: Item[];
381
+ fields: NormalizedField< Item >[];
382
+ getItemId: ( item: Item ) => string;
383
+ isLoading?: boolean;
384
+ onChangeView( view: View ): void;
385
+ onSelectionChange: ( items: Item[] ) => void;
386
+ selection: string[];
387
+ setOpenedFilter: ( fieldId: string ) => void;
388
+ view: View;
389
+ }
390
+
391
+ export interface ViewTableProps< Item extends AnyItem >
392
+ extends ViewBaseProps< Item > {
393
+ view: ViewTable;
394
+ }
395
+
396
+ export interface ViewListProps< Item extends AnyItem >
397
+ extends ViewBaseProps< Item > {
398
+ view: ViewList;
399
+ }
400
+
401
+ export interface ViewGridProps< Item extends AnyItem >
402
+ extends ViewBaseProps< Item > {
403
+ view: ViewGrid;
404
+ }
405
+
406
+ export type ViewProps< Item extends AnyItem > =
407
+ | ViewTableProps< Item >
408
+ | ViewGridProps< Item >
409
+ | ViewListProps< Item >;
@@ -8,8 +8,11 @@ import {
8
8
  OPERATOR_IS_ANY,
9
9
  OPERATOR_IS_NONE,
10
10
  } from './constants';
11
+ import type { AnyItem, NormalizedField } from './types';
11
12
 
12
- export const sanitizeOperators = ( field ) => {
13
+ export function sanitizeOperators< Item extends AnyItem >(
14
+ field: NormalizedField< Item >
15
+ ) {
13
16
  let operators = field.filterBy?.operators;
14
17
 
15
18
  // Assign default values.
@@ -17,17 +20,6 @@ export const sanitizeOperators = ( field ) => {
17
20
  operators = [ OPERATOR_IS_ANY, OPERATOR_IS_NONE ];
18
21
  }
19
22
 
20
- // Transform legacy in, notIn operators to is, isNot.
21
- // To be removed in the future.
22
- if ( operators.includes( 'in' ) ) {
23
- operators = operators.filter( ( operator ) => operator !== 'is' );
24
- operators.push( 'is' );
25
- }
26
- if ( operators.includes( 'notIn' ) ) {
27
- operators = operators.filter( ( operator ) => operator !== 'notIn' );
28
- operators.push( 'isNot' );
29
- }
30
-
31
23
  // Make sure only valid operators are used.
32
24
  operators = operators.filter( ( operator ) =>
33
25
  ALL_OPERATORS.includes( operator )
@@ -45,4 +37,4 @@ export const sanitizeOperators = ( field ) => {
45
37
  }
46
38
 
47
39
  return operators;
48
- };
40
+ }
@@ -1,3 +1,8 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type { ChangeEvent } from 'react';
5
+
1
6
  /**
2
7
  * WordPress dependencies
3
8
  */
@@ -13,8 +18,9 @@ import { settings } from '@wordpress/icons';
13
18
  * Internal dependencies
14
19
  */
15
20
  import { unlock } from './lock-unlock';
16
- import { SORTING_DIRECTIONS } from './constants';
21
+ import { SORTING_DIRECTIONS, sortLabels } from './constants';
17
22
  import { VIEW_LAYOUTS } from './layouts';
23
+ import type { AnyItem, NormalizedField, View } from './types';
18
24
 
19
25
  const {
20
26
  DropdownMenuV2: DropdownMenu,
@@ -25,7 +31,41 @@ const {
25
31
  DropdownMenuItemLabelV2: DropdownMenuItemLabel,
26
32
  } = unlock( componentsPrivateApis );
27
33
 
28
- function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) {
34
+ interface ViewTypeMenuProps {
35
+ view: View;
36
+ onChangeView: ( view: View ) => void;
37
+ supportedLayouts?: string[];
38
+ }
39
+
40
+ interface PageSizeMenuProps {
41
+ view: View;
42
+ onChangeView: ( view: View ) => void;
43
+ }
44
+
45
+ interface FieldsVisibilityMenuProps< Item extends AnyItem > {
46
+ view: View;
47
+ onChangeView: ( view: View ) => void;
48
+ fields: NormalizedField< Item >[];
49
+ }
50
+
51
+ interface SortMenuProps< Item extends AnyItem > {
52
+ fields: NormalizedField< Item >[];
53
+ view: View;
54
+ onChangeView: ( view: View ) => void;
55
+ }
56
+
57
+ interface ViewActionsProps< Item extends AnyItem > {
58
+ fields: NormalizedField< Item >[];
59
+ view: View;
60
+ onChangeView: ( view: View ) => void;
61
+ supportedLayouts?: string[];
62
+ }
63
+
64
+ function ViewTypeMenu( {
65
+ view,
66
+ onChangeView,
67
+ supportedLayouts,
68
+ }: ViewTypeMenuProps ) {
29
69
  let _availableViews = VIEW_LAYOUTS;
30
70
  if ( supportedLayouts ) {
31
71
  _availableViews = _availableViews.filter( ( _view ) =>
@@ -41,7 +81,7 @@ function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) {
41
81
  trigger={
42
82
  <DropdownMenuItem
43
83
  suffix={
44
- <span aria-hidden="true">{ activeView.label }</span>
84
+ <span aria-hidden="true">{ activeView?.label }</span>
45
85
  }
46
86
  >
47
87
  <DropdownMenuItemLabel>
@@ -58,11 +98,18 @@ function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) {
58
98
  name="view-actions-available-view"
59
99
  checked={ availableView.type === view.type }
60
100
  hideOnClick
61
- onChange={ ( e ) => {
62
- onChangeView( {
63
- ...view,
64
- type: e.target.value,
65
- } );
101
+ onChange={ ( e: ChangeEvent< HTMLInputElement > ) => {
102
+ switch ( e.target.value ) {
103
+ case 'list':
104
+ case 'grid':
105
+ case 'table':
106
+ return onChangeView( {
107
+ ...view,
108
+ type: e.target.value,
109
+ layout: {},
110
+ } );
111
+ }
112
+ throw new Error( 'Invalid dataview' );
66
113
  } }
67
114
  >
68
115
  <DropdownMenuItemLabel>
@@ -76,7 +123,7 @@ function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) {
76
123
  }
77
124
 
78
125
  const PAGE_SIZE_VALUES = [ 10, 20, 50, 100 ];
79
- function PageSizeMenu( { view, onChangeView } ) {
126
+ function PageSizeMenu( { view, onChangeView }: PageSizeMenuProps ) {
80
127
  return (
81
128
  <DropdownMenu
82
129
  trigger={
@@ -114,7 +161,11 @@ function PageSizeMenu( { view, onChangeView } ) {
114
161
  );
115
162
  }
116
163
 
117
- function FieldsVisibilityMenu( { view, onChangeView, fields } ) {
164
+ function FieldsVisibilityMenu< Item extends AnyItem >( {
165
+ view,
166
+ onChangeView,
167
+ fields,
168
+ }: FieldsVisibilityMenuProps< Item > ) {
118
169
  const hidableFields = fields.filter(
119
170
  ( field ) =>
120
171
  field.enableHiding !== false && field.id !== view.layout.mediaField
@@ -164,7 +215,11 @@ function FieldsVisibilityMenu( { view, onChangeView, fields } ) {
164
215
  );
165
216
  }
166
217
 
167
- function SortMenu( { fields, view, onChangeView } ) {
218
+ function SortMenu< Item extends AnyItem >( {
219
+ fields,
220
+ view,
221
+ onChangeView,
222
+ }: SortMenuProps< Item > ) {
168
223
  const sortableFields = fields.filter(
169
224
  ( field ) => field.enableSorting !== false
170
225
  );
@@ -206,43 +261,41 @@ function SortMenu( { fields, view, onChangeView } ) {
206
261
  minWidth: '220px',
207
262
  } }
208
263
  >
209
- { Object.entries( SORTING_DIRECTIONS ).map(
210
- ( [ direction, info ] ) => {
211
- const isChecked =
212
- currentSortedField !== undefined &&
213
- sortedDirection === direction &&
214
- field.id === currentSortedField.id;
264
+ { SORTING_DIRECTIONS.map( ( direction ) => {
265
+ const isChecked =
266
+ currentSortedField !== undefined &&
267
+ sortedDirection === direction &&
268
+ field.id === currentSortedField.id;
215
269
 
216
- const value = `${ field.id }-${ direction }`;
270
+ const value = `${ field.id }-${ direction }`;
217
271
 
218
- return (
219
- <DropdownMenuRadioItem
220
- key={ value }
221
- // All sorting radio items share the same name, so that
222
- // selecting a sorting option automatically deselects the
223
- // previously selected one, even if it is displayed in
224
- // another submenu. The field and direction are passed via
225
- // the `value` prop.
226
- name="view-actions-sorting"
227
- value={ value }
228
- checked={ isChecked }
229
- onChange={ () => {
230
- onChangeView( {
231
- ...view,
232
- sort: {
233
- field: field.id,
234
- direction,
235
- },
236
- } );
237
- } }
238
- >
239
- <DropdownMenuItemLabel>
240
- { info.label }
241
- </DropdownMenuItemLabel>
242
- </DropdownMenuRadioItem>
243
- );
244
- }
245
- ) }
272
+ return (
273
+ <DropdownMenuRadioItem
274
+ key={ value }
275
+ // All sorting radio items share the same name, so that
276
+ // selecting a sorting option automatically deselects the
277
+ // previously selected one, even if it is displayed in
278
+ // another submenu. The field and direction are passed via
279
+ // the `value` prop.
280
+ name="view-actions-sorting"
281
+ value={ value }
282
+ checked={ isChecked }
283
+ onChange={ () => {
284
+ onChangeView( {
285
+ ...view,
286
+ sort: {
287
+ field: field.id,
288
+ direction,
289
+ },
290
+ } );
291
+ } }
292
+ >
293
+ <DropdownMenuItemLabel>
294
+ { sortLabels[ direction ] }
295
+ </DropdownMenuItemLabel>
296
+ </DropdownMenuRadioItem>
297
+ );
298
+ } ) }
246
299
  </DropdownMenu>
247
300
  );
248
301
  } ) }
@@ -250,12 +303,12 @@ function SortMenu( { fields, view, onChangeView } ) {
250
303
  );
251
304
  }
252
305
 
253
- const ViewActions = memo( function ViewActions( {
306
+ function _ViewActions< Item extends AnyItem >( {
254
307
  fields,
255
308
  view,
256
309
  onChangeView,
257
310
  supportedLayouts,
258
- } ) {
311
+ }: ViewActionsProps< Item > ) {
259
312
  return (
260
313
  <DropdownMenu
261
314
  trigger={
@@ -286,6 +339,9 @@ const ViewActions = memo( function ViewActions( {
286
339
  </DropdownMenuGroup>
287
340
  </DropdownMenu>
288
341
  );
289
- } );
342
+ }
343
+
344
+ // A type assertion is used here to keep the type argument.
345
+ const ViewActions = memo( _ViewActions ) as typeof _ViewActions;
290
346
 
291
347
  export default ViewActions;