@wordpress/dataviews 4.1.0 → 4.3.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 (223) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +77 -29
  3. package/build/components/dataviews/index.js +10 -14
  4. package/build/components/dataviews/index.js.map +1 -1
  5. package/build/components/dataviews-bulk-actions/index.js +145 -141
  6. package/build/components/dataviews-bulk-actions/index.js.map +1 -1
  7. package/build/components/dataviews-filters/add-filter.js +4 -6
  8. package/build/components/dataviews-filters/add-filter.js.map +1 -1
  9. package/build/components/dataviews-filters/index.js +3 -0
  10. package/build/components/dataviews-filters/index.js.map +1 -1
  11. package/build/components/dataviews-filters/search-widget.js +30 -23
  12. package/build/components/dataviews-filters/search-widget.js.map +1 -1
  13. package/build/components/dataviews-footer/index.js +45 -0
  14. package/build/components/dataviews-footer/index.js.map +1 -0
  15. package/build/components/dataviews-item-actions/index.js +5 -8
  16. package/build/components/dataviews-item-actions/index.js.map +1 -1
  17. package/build/components/dataviews-pagination/index.js +27 -19
  18. package/build/components/dataviews-pagination/index.js.map +1 -1
  19. package/build/components/dataviews-view-config/index.js +197 -41
  20. package/build/components/dataviews-view-config/index.js.map +1 -1
  21. package/build/dataform-controls/datetime.js +49 -0
  22. package/build/dataform-controls/datetime.js.map +1 -0
  23. package/build/dataform-controls/index.js +50 -0
  24. package/build/dataform-controls/index.js.map +1 -0
  25. package/build/dataform-controls/integer.js +45 -0
  26. package/build/dataform-controls/integer.js.map +1 -0
  27. package/build/dataform-controls/radio.js +45 -0
  28. package/build/dataform-controls/radio.js.map +1 -0
  29. package/build/dataform-controls/select.js +58 -0
  30. package/build/dataform-controls/select.js.map +1 -0
  31. package/build/dataform-controls/text.js +45 -0
  32. package/build/dataform-controls/text.js.map +1 -0
  33. package/build/dataforms-layouts/panel/index.js +10 -4
  34. package/build/dataforms-layouts/panel/index.js.map +1 -1
  35. package/build/dataforms-layouts/regular/index.js +6 -3
  36. package/build/dataforms-layouts/regular/index.js.map +1 -1
  37. package/build/dataviews-layouts/grid/density-picker.js +23 -52
  38. package/build/dataviews-layouts/grid/density-picker.js.map +1 -1
  39. package/build/dataviews-layouts/grid/index.js +1 -1
  40. package/build/dataviews-layouts/grid/index.js.map +1 -1
  41. package/build/dataviews-layouts/index.js +48 -2
  42. package/build/dataviews-layouts/index.js.map +1 -1
  43. package/build/dataviews-layouts/list/index.js +124 -80
  44. package/build/dataviews-layouts/list/index.js.map +1 -1
  45. package/build/dataviews-layouts/table/column-header-menu.js +52 -57
  46. package/build/dataviews-layouts/table/column-header-menu.js.map +1 -1
  47. package/build/dataviews-layouts/table/index.js +7 -35
  48. package/build/dataviews-layouts/table/index.js.map +1 -1
  49. package/build/field-types/datetime.js +30 -0
  50. package/build/field-types/datetime.js.map +1 -0
  51. package/build/field-types/index.js +4 -0
  52. package/build/field-types/index.js.map +1 -1
  53. package/build/field-types/integer.js +1 -60
  54. package/build/field-types/integer.js.map +1 -1
  55. package/build/field-types/text.js +1 -60
  56. package/build/field-types/text.js.map +1 -1
  57. package/build/normalize-fields.js +10 -9
  58. package/build/normalize-fields.js.map +1 -1
  59. package/build/types.js.map +1 -1
  60. package/build-module/components/dataviews/index.js +10 -14
  61. package/build-module/components/dataviews/index.js.map +1 -1
  62. package/build-module/components/dataviews-bulk-actions/index.js +145 -143
  63. package/build-module/components/dataviews-bulk-actions/index.js.map +1 -1
  64. package/build-module/components/dataviews-filters/add-filter.js +4 -6
  65. package/build-module/components/dataviews-filters/add-filter.js.map +1 -1
  66. package/build-module/components/dataviews-filters/index.js +3 -0
  67. package/build-module/components/dataviews-filters/index.js.map +1 -1
  68. package/build-module/components/dataviews-filters/search-widget.js +30 -23
  69. package/build-module/components/dataviews-filters/search-widget.js.map +1 -1
  70. package/build-module/components/dataviews-footer/index.js +38 -0
  71. package/build-module/components/dataviews-footer/index.js.map +1 -0
  72. package/build-module/components/dataviews-item-actions/index.js +5 -8
  73. package/build-module/components/dataviews-item-actions/index.js.map +1 -1
  74. package/build-module/components/dataviews-pagination/index.js +28 -20
  75. package/build-module/components/dataviews-pagination/index.js.map +1 -1
  76. package/build-module/components/dataviews-view-config/index.js +203 -47
  77. package/build-module/components/dataviews-view-config/index.js.map +1 -1
  78. package/build-module/dataform-controls/datetime.js +43 -0
  79. package/build-module/dataform-controls/datetime.js.map +1 -0
  80. package/build-module/dataform-controls/index.js +42 -0
  81. package/build-module/dataform-controls/index.js.map +1 -0
  82. package/build-module/dataform-controls/integer.js +38 -0
  83. package/build-module/dataform-controls/integer.js.map +1 -0
  84. package/build-module/dataform-controls/radio.js +38 -0
  85. package/build-module/dataform-controls/radio.js.map +1 -0
  86. package/build-module/dataform-controls/select.js +51 -0
  87. package/build-module/dataform-controls/select.js.map +1 -0
  88. package/build-module/dataform-controls/text.js +38 -0
  89. package/build-module/dataform-controls/text.js.map +1 -0
  90. package/build-module/dataforms-layouts/panel/index.js +10 -4
  91. package/build-module/dataforms-layouts/panel/index.js.map +1 -1
  92. package/build-module/dataforms-layouts/regular/index.js +6 -3
  93. package/build-module/dataforms-layouts/regular/index.js.map +1 -1
  94. package/build-module/dataviews-layouts/grid/density-picker.js +25 -56
  95. package/build-module/dataviews-layouts/grid/density-picker.js.map +1 -1
  96. package/build-module/dataviews-layouts/grid/index.js +1 -1
  97. package/build-module/dataviews-layouts/grid/index.js.map +1 -1
  98. package/build-module/dataviews-layouts/index.js +45 -1
  99. package/build-module/dataviews-layouts/index.js.map +1 -1
  100. package/build-module/dataviews-layouts/list/index.js +125 -80
  101. package/build-module/dataviews-layouts/list/index.js.map +1 -1
  102. package/build-module/dataviews-layouts/table/column-header-menu.js +52 -57
  103. package/build-module/dataviews-layouts/table/column-header-menu.js.map +1 -1
  104. package/build-module/dataviews-layouts/table/index.js +9 -37
  105. package/build-module/dataviews-layouts/table/index.js.map +1 -1
  106. package/build-module/field-types/datetime.js +24 -0
  107. package/build-module/field-types/datetime.js.map +1 -0
  108. package/build-module/field-types/index.js +4 -0
  109. package/build-module/field-types/index.js.map +1 -1
  110. package/build-module/field-types/integer.js +2 -60
  111. package/build-module/field-types/integer.js.map +1 -1
  112. package/build-module/field-types/text.js +2 -60
  113. package/build-module/field-types/text.js.map +1 -1
  114. package/build-module/normalize-fields.js +11 -9
  115. package/build-module/normalize-fields.js.map +1 -1
  116. package/build-module/types.js.map +1 -1
  117. package/build-style/style-rtl.css +93 -80
  118. package/build-style/style.css +93 -80
  119. package/build-types/components/dataform/stories/index.story.d.ts.map +1 -1
  120. package/build-types/components/dataviews/index.d.ts.map +1 -1
  121. package/build-types/components/dataviews/stories/fixtures.d.ts +28 -113
  122. package/build-types/components/dataviews/stories/fixtures.d.ts.map +1 -1
  123. package/build-types/components/dataviews/stories/index.story.d.ts +12 -44
  124. package/build-types/components/dataviews/stories/index.story.d.ts.map +1 -1
  125. package/build-types/components/dataviews-bulk-actions/index.d.ts +11 -1
  126. package/build-types/components/dataviews-bulk-actions/index.d.ts.map +1 -1
  127. package/build-types/components/dataviews-filters/add-filter.d.ts.map +1 -1
  128. package/build-types/components/dataviews-filters/index.d.ts +1 -1
  129. package/build-types/components/dataviews-filters/index.d.ts.map +1 -1
  130. package/build-types/components/dataviews-filters/search-widget.d.ts.map +1 -1
  131. package/build-types/components/dataviews-footer/index.d.ts +2 -0
  132. package/build-types/components/dataviews-footer/index.d.ts.map +1 -0
  133. package/build-types/components/dataviews-item-actions/index.d.ts.map +1 -1
  134. package/build-types/components/dataviews-pagination/index.d.ts.map +1 -1
  135. package/build-types/components/dataviews-view-config/index.d.ts +4 -3
  136. package/build-types/components/dataviews-view-config/index.d.ts.map +1 -1
  137. package/build-types/dataform-controls/datetime.d.ts +6 -0
  138. package/build-types/dataform-controls/datetime.d.ts.map +1 -0
  139. package/build-types/dataform-controls/index.d.ts +11 -0
  140. package/build-types/dataform-controls/index.d.ts.map +1 -0
  141. package/build-types/dataform-controls/integer.d.ts +6 -0
  142. package/build-types/dataform-controls/integer.d.ts.map +1 -0
  143. package/build-types/dataform-controls/radio.d.ts +6 -0
  144. package/build-types/dataform-controls/radio.d.ts.map +1 -0
  145. package/build-types/dataform-controls/select.d.ts +6 -0
  146. package/build-types/dataform-controls/select.d.ts.map +1 -0
  147. package/build-types/dataform-controls/text.d.ts +6 -0
  148. package/build-types/dataform-controls/text.d.ts.map +1 -0
  149. package/build-types/dataforms-layouts/panel/index.d.ts.map +1 -1
  150. package/build-types/dataforms-layouts/regular/index.d.ts.map +1 -1
  151. package/build-types/dataviews-layouts/grid/density-picker.d.ts.map +1 -1
  152. package/build-types/dataviews-layouts/index.d.ts +4 -2
  153. package/build-types/dataviews-layouts/index.d.ts.map +1 -1
  154. package/build-types/dataviews-layouts/list/index.d.ts.map +1 -1
  155. package/build-types/dataviews-layouts/table/column-header-menu.d.ts.map +1 -1
  156. package/build-types/dataviews-layouts/table/index.d.ts.map +1 -1
  157. package/build-types/field-types/datetime.d.ts +13 -0
  158. package/build-types/field-types/datetime.d.ts.map +1 -0
  159. package/build-types/field-types/index.d.ts +1 -1
  160. package/build-types/field-types/index.d.ts.map +1 -1
  161. package/build-types/field-types/integer.d.ts +2 -3
  162. package/build-types/field-types/integer.d.ts.map +1 -1
  163. package/build-types/field-types/text.d.ts +2 -3
  164. package/build-types/field-types/text.d.ts.map +1 -1
  165. package/build-types/normalize-fields.d.ts.map +1 -1
  166. package/build-types/types.d.ts +43 -21
  167. package/build-types/types.d.ts.map +1 -1
  168. package/package.json +12 -12
  169. package/src/components/dataform/stories/index.story.tsx +43 -2
  170. package/src/components/dataviews/index.tsx +14 -18
  171. package/src/components/dataviews/stories/fixtures.tsx +690 -0
  172. package/src/components/dataviews/stories/index.story.tsx +164 -0
  173. package/src/components/dataviews/style.scss +2 -12
  174. package/src/components/dataviews-bulk-actions/index.tsx +264 -213
  175. package/src/components/dataviews-bulk-actions/style.scss +9 -4
  176. package/src/components/dataviews-filters/add-filter.tsx +7 -11
  177. package/src/components/dataviews-filters/index.tsx +3 -0
  178. package/src/components/dataviews-filters/search-widget.tsx +46 -25
  179. package/src/components/dataviews-filters/style.scss +13 -3
  180. package/src/components/dataviews-footer/index.tsx +50 -0
  181. package/src/components/dataviews-footer/style.scss +40 -0
  182. package/src/components/dataviews-item-actions/index.tsx +8 -14
  183. package/src/components/dataviews-pagination/index.tsx +40 -21
  184. package/src/components/dataviews-pagination/style.scss +7 -21
  185. package/src/components/dataviews-view-config/index.tsx +297 -69
  186. package/src/components/dataviews-view-config/style.scss +25 -0
  187. package/src/dataform-controls/datetime.tsx +43 -0
  188. package/src/dataform-controls/index.tsx +61 -0
  189. package/src/dataform-controls/integer.tsx +38 -0
  190. package/src/dataform-controls/radio.tsx +42 -0
  191. package/src/dataform-controls/select.tsx +52 -0
  192. package/src/dataform-controls/style.scss +4 -0
  193. package/src/dataform-controls/text.tsx +40 -0
  194. package/src/dataforms-layouts/panel/index.tsx +8 -2
  195. package/src/dataforms-layouts/regular/index.tsx +6 -2
  196. package/src/dataviews-layouts/grid/density-picker.tsx +33 -67
  197. package/src/dataviews-layouts/grid/index.tsx +1 -1
  198. package/src/dataviews-layouts/grid/style.scss +1 -5
  199. package/src/dataviews-layouts/index.ts +63 -2
  200. package/src/dataviews-layouts/list/index.tsx +199 -123
  201. package/src/dataviews-layouts/list/style.scss +10 -4
  202. package/src/dataviews-layouts/table/column-header-menu.tsx +86 -90
  203. package/src/dataviews-layouts/table/index.tsx +8 -65
  204. package/src/dataviews-layouts/table/style.scss +0 -5
  205. package/src/field-types/datetime.tsx +28 -0
  206. package/src/field-types/index.tsx +5 -0
  207. package/src/field-types/integer.tsx +2 -71
  208. package/src/field-types/text.tsx +2 -70
  209. package/src/normalize-fields.ts +10 -10
  210. package/src/style.scss +2 -1
  211. package/src/test/filter-and-sort-data-view.js +28 -0
  212. package/src/types.ts +56 -32
  213. package/tsconfig.tsbuildinfo +1 -1
  214. package/build/components/dataviews-bulk-actions-toolbar/index.js +0 -207
  215. package/build/components/dataviews-bulk-actions-toolbar/index.js.map +0 -1
  216. package/build-module/components/dataviews-bulk-actions-toolbar/index.js +0 -201
  217. package/build-module/components/dataviews-bulk-actions-toolbar/index.js.map +0 -1
  218. package/build-types/components/dataviews-bulk-actions-toolbar/index.d.ts +0 -2
  219. package/build-types/components/dataviews-bulk-actions-toolbar/index.d.ts.map +0 -1
  220. package/src/components/dataviews/stories/fixtures.js +0 -222
  221. package/src/components/dataviews/stories/index.story.js +0 -65
  222. package/src/components/dataviews-bulk-actions-toolbar/index.tsx +0 -288
  223. package/src/components/dataviews-bulk-actions-toolbar/style.scss +0 -45
@@ -1,222 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { trash } from '@wordpress/icons';
5
- import {
6
- Button,
7
- __experimentalText as Text,
8
- __experimentalHStack as HStack,
9
- __experimentalVStack as VStack,
10
- } from '@wordpress/components';
11
-
12
- /**
13
- * Internal dependencies
14
- */
15
- import { LAYOUT_TABLE } from '../../../constants';
16
-
17
- export const data = [
18
- {
19
- id: 1,
20
- title: 'Apollo',
21
- description: 'Apollo description',
22
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
23
- type: 'Not a planet',
24
- categories: [ 'Space', 'NASA' ],
25
- satellites: 0,
26
- },
27
- {
28
- id: 2,
29
- title: 'Space',
30
- description: 'Space description',
31
- image: 'https://live.staticflickr.com/5678/21911065441_92e2d44708_b.jpg',
32
- type: 'Not a planet',
33
- categories: [ 'Space' ],
34
- satellites: 0,
35
- },
36
- {
37
- id: 3,
38
- title: 'NASA',
39
- description: 'NASA photo',
40
- image: 'https://live.staticflickr.com/742/21712365770_8f70a2c91e_b.jpg',
41
- type: 'Not a planet',
42
- categories: [ 'NASA' ],
43
- satellites: 0,
44
- },
45
- {
46
- id: 4,
47
- title: 'Neptune',
48
- description: 'Neptune description',
49
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
50
- type: 'Ice giant',
51
- categories: [ 'Space', 'Planet', 'Solar system' ],
52
- satellites: 14,
53
- },
54
- {
55
- id: 5,
56
- title: 'Mercury',
57
- description: 'Mercury description',
58
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
59
- type: 'Terrestrial',
60
- categories: [ 'Space', 'Planet', 'Solar system' ],
61
- satellites: 0,
62
- },
63
- {
64
- id: 6,
65
- title: 'Venus',
66
- description: 'La planète Vénus',
67
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
68
- type: 'Terrestrial',
69
- categories: [ 'Space', 'Planet', 'Solar system' ],
70
- satellites: 0,
71
- },
72
- {
73
- id: 7,
74
- title: 'Earth',
75
- description: 'Earth description',
76
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
77
- type: 'Terrestrial',
78
- categories: [ 'Space', 'Planet', 'Solar system' ],
79
- satellites: 1,
80
- },
81
- {
82
- id: 8,
83
- title: 'Mars',
84
- description: 'Mars description',
85
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
86
- type: 'Terrestrial',
87
- categories: [ 'Space', 'Planet', 'Solar system' ],
88
- satellites: 2,
89
- },
90
- {
91
- id: 9,
92
- title: 'Jupiter',
93
- description: 'Jupiter description',
94
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
95
- type: 'Gas giant',
96
- categories: [ 'Space', 'Planet', 'Solar system' ],
97
- satellites: 95,
98
- },
99
- {
100
- id: 10,
101
- title: 'Saturn',
102
- description: 'Saturn description',
103
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
104
- type: 'Gas giant',
105
- categories: [ 'Space', 'Planet', 'Solar system' ],
106
- satellites: 146,
107
- },
108
- {
109
- id: 11,
110
- title: 'Uranus',
111
- description: 'Uranus description',
112
- image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
113
- type: 'Ice giant',
114
- categories: [ 'Space', 'Ice giant', 'Solar system' ],
115
- satellites: 28,
116
- },
117
- ];
118
-
119
- export const DEFAULT_VIEW = {
120
- type: LAYOUT_TABLE,
121
- search: '',
122
- page: 1,
123
- perPage: 10,
124
- fields: [ 'title', 'description', 'categories' ],
125
- layout: {},
126
- filters: [],
127
- };
128
-
129
- export const actions = [
130
- {
131
- id: 'delete',
132
- label: 'Delete item',
133
- isPrimary: true,
134
- icon: trash,
135
- hideModalHeader: true,
136
- RenderModal: ( { item, closeModal } ) => {
137
- return (
138
- <VStack spacing="5">
139
- <Text>
140
- { `Are you sure you want to delete "${ item.title }"?` }
141
- </Text>
142
- <HStack justify="right">
143
- <Button variant="tertiary" onClick={ closeModal }>
144
- Cancel
145
- </Button>
146
- <Button variant="primary" onClick={ closeModal }>
147
- Delete
148
- </Button>
149
- </HStack>
150
- </VStack>
151
- );
152
- },
153
- },
154
- {
155
- id: 'secondary',
156
- label: 'Secondary action',
157
- callback() {},
158
- },
159
- ];
160
-
161
- export const fields = [
162
- {
163
- label: 'Image',
164
- id: 'image',
165
- render: ( { item } ) => {
166
- return (
167
- <img src={ item.image } alt="" style={ { width: '100%' } } />
168
- );
169
- },
170
- enableSorting: false,
171
- },
172
- {
173
- label: 'Title',
174
- id: 'title',
175
- enableHiding: false,
176
- enableGlobalSearch: true,
177
- },
178
- {
179
- label: 'Type',
180
- id: 'type',
181
- enableHiding: false,
182
- elements: [
183
- { value: 'Not a planet', label: 'Not a planet' },
184
- { value: 'Ice giant', label: 'Ice giant' },
185
- { value: 'Terrestrial', label: 'Terrestrial' },
186
- { value: 'Gas giant', label: 'Gas giant' },
187
- ],
188
- },
189
- {
190
- label: 'Satellites',
191
- id: 'satellites',
192
- type: 'integer',
193
- enableSorting: true,
194
- },
195
- {
196
- label: 'Description',
197
- id: 'description',
198
- enableSorting: false,
199
- enableGlobalSearch: true,
200
- },
201
- {
202
- label: 'Categories',
203
- id: 'categories',
204
- elements: [
205
- { value: 'Space', label: 'Space' },
206
- { value: 'NASA', label: 'NASA' },
207
- { value: 'Planet', label: 'Planet' },
208
- { value: 'Solar system', label: 'Solar system' },
209
- { value: 'Ice giant', label: 'Ice giant' },
210
- ],
211
- filterBy: {
212
- operators: [ 'isAny', 'isNone', 'isAll', 'isNotAll' ],
213
- },
214
- getValue: ( { item } ) => {
215
- return item.categories;
216
- },
217
- render: ( { item } ) => {
218
- return item.categories.join( ',' );
219
- },
220
- enableSorting: false,
221
- },
222
- ];
@@ -1,65 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useState, useMemo } from '@wordpress/element';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import DataViews from '../index';
10
- import { DEFAULT_VIEW, actions, data, fields } from './fixtures';
11
- import { LAYOUT_GRID, LAYOUT_TABLE } from '../../../constants';
12
- import { filterSortAndPaginate } from '../../../filter-and-sort-data-view';
13
-
14
- const meta = {
15
- title: 'DataViews/DataViews',
16
- component: DataViews,
17
- };
18
- export default meta;
19
-
20
- export const Default = ( props ) => {
21
- const [ view, setView ] = useState( DEFAULT_VIEW );
22
- const { data: shownData, paginationInfo } = useMemo( () => {
23
- return filterSortAndPaginate( data, view, fields );
24
- }, [ view ] );
25
- return (
26
- <DataViews
27
- { ...props }
28
- paginationInfo={ paginationInfo }
29
- data={ shownData }
30
- view={ view }
31
- fields={ fields }
32
- onChangeView={ setView }
33
- />
34
- );
35
- };
36
- Default.args = {
37
- actions,
38
- defaultLayouts: {
39
- [ LAYOUT_TABLE ]: {
40
- layout: {
41
- primaryField: 'title',
42
- styles: {
43
- image: {
44
- width: 50,
45
- },
46
- title: {
47
- maxWidth: 400,
48
- },
49
- type: {
50
- maxWidth: 400,
51
- },
52
- description: {
53
- maxWidth: 200,
54
- },
55
- },
56
- },
57
- },
58
- [ LAYOUT_GRID ]: {
59
- layout: {
60
- mediaField: 'image',
61
- primaryField: 'title',
62
- },
63
- },
64
- },
65
- };
@@ -1,288 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import {
5
- ToolbarButton,
6
- Toolbar,
7
- ToolbarGroup,
8
- __unstableMotion as motion,
9
- __unstableAnimatePresence as AnimatePresence,
10
- } from '@wordpress/components';
11
- import { useMemo, useState, useRef, useContext } from '@wordpress/element';
12
- import { _n, sprintf, __ } from '@wordpress/i18n';
13
- import { closeSmall } from '@wordpress/icons';
14
- import { useReducedMotion } from '@wordpress/compose';
15
- import { useRegistry } from '@wordpress/data';
16
-
17
- /**
18
- * Internal dependencies
19
- */
20
- import { useSomeItemHasAPossibleBulkAction } from '../dataviews-bulk-actions';
21
- import DataViewsContext from '../dataviews-context';
22
- import { ActionWithModal } from '../dataviews-item-actions';
23
- import { LAYOUT_GRID, LAYOUT_TABLE } from '../../constants';
24
- import type { Action } from '../../types';
25
- import type { ActionTriggerProps } from '../dataviews-item-actions';
26
- import type { SetSelection } from '../../private-types';
27
-
28
- interface ActionButtonProps< Item > {
29
- action: Action< Item >;
30
- selectedItems: Item[];
31
- actionInProgress: string | null;
32
- setActionInProgress: ( actionId: string | null ) => void;
33
- }
34
-
35
- interface ToolbarContentProps< Item > {
36
- selection: string[];
37
- actionsToShow: Action< Item >[];
38
- selectedItems: Item[];
39
- onChangeSelection: SetSelection;
40
- }
41
-
42
- const SNACKBAR_VARIANTS = {
43
- init: {
44
- bottom: -48,
45
- },
46
- open: {
47
- bottom: 24,
48
- transition: {
49
- bottom: { type: 'tween', duration: 0.2, ease: [ 0, 0, 0.2, 1 ] },
50
- },
51
- },
52
- exit: {
53
- opacity: 0,
54
- bottom: 24,
55
- transition: {
56
- opacity: { type: 'tween', duration: 0.2, ease: [ 0, 0, 0.2, 1 ] },
57
- },
58
- },
59
- };
60
-
61
- function ActionTrigger< Item >( {
62
- action,
63
- onClick,
64
- isBusy,
65
- items,
66
- }: ActionTriggerProps< Item > ) {
67
- const label =
68
- typeof action.label === 'string' ? action.label : action.label( items );
69
- return (
70
- <ToolbarButton
71
- disabled={ isBusy }
72
- label={ label }
73
- icon={ action.icon }
74
- isDestructive={ action.isDestructive }
75
- size="compact"
76
- onClick={ onClick }
77
- isBusy={ isBusy }
78
- tooltipPosition="top"
79
- />
80
- );
81
- }
82
-
83
- const EMPTY_ARRAY: [] = [];
84
-
85
- function ActionButton< Item >( {
86
- action,
87
- selectedItems,
88
- actionInProgress,
89
- setActionInProgress,
90
- }: ActionButtonProps< Item > ) {
91
- const registry = useRegistry();
92
- const selectedEligibleItems = useMemo( () => {
93
- return selectedItems.filter( ( item ) => {
94
- return ! action.isEligible || action.isEligible( item );
95
- } );
96
- }, [ action, selectedItems ] );
97
- if ( 'RenderModal' in action ) {
98
- return (
99
- <ActionWithModal
100
- key={ action.id }
101
- action={ action }
102
- items={ selectedEligibleItems }
103
- ActionTrigger={ ActionTrigger }
104
- />
105
- );
106
- }
107
- return (
108
- <ActionTrigger
109
- key={ action.id }
110
- action={ action }
111
- onClick={ () => {
112
- setActionInProgress( action.id );
113
- action.callback( selectedItems, {
114
- registry,
115
- } );
116
- } }
117
- items={ selectedEligibleItems }
118
- isBusy={ actionInProgress === action.id }
119
- />
120
- );
121
- }
122
-
123
- function renderToolbarContent< Item >(
124
- selection: string[],
125
- actionsToShow: Action< Item >[],
126
- selectedItems: Item[],
127
- actionInProgress: string | null,
128
- setActionInProgress: ( actionId: string | null ) => void,
129
- onChangeSelection: SetSelection
130
- ) {
131
- return (
132
- <>
133
- <ToolbarGroup>
134
- <div className="dataviews-bulk-actions-toolbar__selection-count">
135
- { selection.length === 1
136
- ? __( '1 item selected' )
137
- : sprintf(
138
- // translators: %s: Total number of selected items.
139
- _n(
140
- '%s item selected',
141
- '%s items selected',
142
- selection.length
143
- ),
144
- selection.length
145
- ) }
146
- </div>
147
- </ToolbarGroup>
148
- <ToolbarGroup>
149
- { actionsToShow.map( ( action ) => {
150
- return (
151
- <ActionButton
152
- key={ action.id }
153
- action={ action }
154
- selectedItems={ selectedItems }
155
- actionInProgress={ actionInProgress }
156
- setActionInProgress={ setActionInProgress }
157
- />
158
- );
159
- } ) }
160
- </ToolbarGroup>
161
- <ToolbarGroup>
162
- <ToolbarButton
163
- icon={ closeSmall }
164
- showTooltip
165
- tooltipPosition="top"
166
- label={ __( 'Cancel' ) }
167
- disabled={ !! actionInProgress }
168
- onClick={ () => {
169
- onChangeSelection( EMPTY_ARRAY );
170
- } }
171
- />
172
- </ToolbarGroup>
173
- </>
174
- );
175
- }
176
-
177
- function ToolbarContent< Item >( {
178
- selection,
179
- actionsToShow,
180
- selectedItems,
181
- onChangeSelection,
182
- }: ToolbarContentProps< Item > ) {
183
- const [ actionInProgress, setActionInProgress ] = useState< string | null >(
184
- null
185
- );
186
- const buttons = useRef< JSX.Element | null >( null );
187
- if ( ! actionInProgress ) {
188
- if ( buttons.current ) {
189
- buttons.current = null;
190
- }
191
- return renderToolbarContent(
192
- selection,
193
- actionsToShow,
194
- selectedItems,
195
- actionInProgress,
196
- setActionInProgress,
197
- onChangeSelection
198
- );
199
- } else if ( ! buttons.current ) {
200
- buttons.current = renderToolbarContent(
201
- selection,
202
- actionsToShow,
203
- selectedItems,
204
- actionInProgress,
205
- setActionInProgress,
206
- onChangeSelection
207
- );
208
- }
209
- return buttons.current;
210
- }
211
-
212
- function _BulkActionsToolbar() {
213
- const {
214
- data,
215
- selection,
216
- actions = EMPTY_ARRAY,
217
- onChangeSelection,
218
- getItemId,
219
- } = useContext( DataViewsContext );
220
- const isReducedMotion = useReducedMotion();
221
- const selectedItems = useMemo( () => {
222
- return data.filter( ( item ) =>
223
- selection.includes( getItemId( item ) )
224
- );
225
- }, [ selection, data, getItemId ] );
226
-
227
- const actionsToShow = useMemo(
228
- () =>
229
- actions.filter( ( action ) => {
230
- return (
231
- action.supportsBulk &&
232
- action.icon &&
233
- selectedItems.some(
234
- ( item ) =>
235
- ! action.isEligible || action.isEligible( item )
236
- )
237
- );
238
- } ),
239
- [ actions, selectedItems ]
240
- );
241
-
242
- if (
243
- ( selection && selection.length === 0 ) ||
244
- actionsToShow.length === 0
245
- ) {
246
- return null;
247
- }
248
-
249
- return (
250
- <AnimatePresence>
251
- <motion.div
252
- layout={ ! isReducedMotion } // See https://www.framer.com/docs/animation/#layout-animations
253
- initial="init"
254
- animate="open"
255
- exit="exit"
256
- variants={ isReducedMotion ? undefined : SNACKBAR_VARIANTS }
257
- className="dataviews-bulk-actions-toolbar"
258
- >
259
- <Toolbar label={ __( 'Bulk actions' ) }>
260
- <div className="dataviews-bulk-actions-toolbar__wrapper">
261
- <ToolbarContent
262
- selection={ selection }
263
- actionsToShow={ actionsToShow }
264
- selectedItems={ selectedItems }
265
- onChangeSelection={ onChangeSelection }
266
- />
267
- </div>
268
- </Toolbar>
269
- </motion.div>
270
- </AnimatePresence>
271
- );
272
- }
273
-
274
- export default function BulkActionsToolbar() {
275
- const { data, actions = [], view } = useContext( DataViewsContext );
276
- const hasPossibleBulkAction = useSomeItemHasAPossibleBulkAction(
277
- actions,
278
- data
279
- );
280
- if (
281
- ! [ LAYOUT_TABLE, LAYOUT_GRID ].includes( view.type ) ||
282
- ! hasPossibleBulkAction
283
- ) {
284
- return null;
285
- }
286
-
287
- return <_BulkActionsToolbar />;
288
- }
@@ -1,45 +0,0 @@
1
- .dataviews-bulk-actions-toolbar {
2
- position: sticky;
3
- display: flex;
4
- flex-direction: column;
5
- align-content: center;
6
- flex-wrap: wrap;
7
- width: fit-content;
8
- margin-left: auto;
9
- margin-right: auto;
10
- bottom: $grid-unit-30;
11
- z-index: z-index(".dataviews-bulk-actions-toolbar");
12
-
13
- .components-accessible-toolbar {
14
- border-color: $gray-300;
15
- box-shadow: $shadow-popover;
16
-
17
- .components-toolbar-group {
18
- border-color: $gray-200;
19
-
20
- &:last-child {
21
- border: 0;
22
- }
23
- }
24
- }
25
-
26
- .dataviews-bulk-actions-toolbar__selection-count {
27
- display: flex;
28
- align-items: center;
29
- margin: 0 $grid-unit-10 0 $grid-unit-10;
30
- }
31
- }
32
-
33
- .dataviews-bulk-actions-toolbar__wrapper {
34
- display: flex;
35
- flex-grow: 1;
36
- width: 100%;
37
-
38
- .components-toolbar-group {
39
- align-items: center;
40
- }
41
-
42
- .components-button.is-busy {
43
- max-height: $button-size;
44
- }
45
- }