@wordpress/dataviews 9.0.1-next.6f42e1382.0 → 9.0.1-next.a730c9c8c.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 (175) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +106 -11
  3. package/build/components/dataviews-filters/input-widget.js +48 -4
  4. package/build/components/dataviews-filters/input-widget.js.map +1 -1
  5. package/build/dataform-controls/array.js +9 -7
  6. package/build/dataform-controls/array.js.map +1 -1
  7. package/build/dataform-controls/checkbox.js +31 -20
  8. package/build/dataform-controls/checkbox.js.map +1 -1
  9. package/build/dataform-controls/color.js +29 -24
  10. package/build/dataform-controls/color.js.map +1 -1
  11. package/build/dataform-controls/date.js +32 -24
  12. package/build/dataform-controls/date.js.map +1 -1
  13. package/build/dataform-controls/datetime.js +133 -19
  14. package/build/dataform-controls/datetime.js.map +1 -1
  15. package/build/dataform-controls/email.js +7 -1
  16. package/build/dataform-controls/email.js.map +1 -1
  17. package/build/dataform-controls/index.js +23 -0
  18. package/build/dataform-controls/index.js.map +1 -1
  19. package/build/dataform-controls/integer.js +47 -34
  20. package/build/dataform-controls/integer.js.map +1 -1
  21. package/build/dataform-controls/radio.js +42 -9
  22. package/build/dataform-controls/radio.js.map +1 -1
  23. package/build/dataform-controls/relative-date-control.js +6 -10
  24. package/build/dataform-controls/relative-date-control.js.map +1 -1
  25. package/build/dataform-controls/select.js +41 -10
  26. package/build/dataform-controls/select.js.map +1 -1
  27. package/build/dataform-controls/telephone.js +7 -1
  28. package/build/dataform-controls/telephone.js.map +1 -1
  29. package/build/dataform-controls/text.js +14 -2
  30. package/build/dataform-controls/text.js.map +1 -1
  31. package/build/dataform-controls/textarea.js +33 -20
  32. package/build/dataform-controls/textarea.js.map +1 -1
  33. package/build/dataform-controls/toggle-group.js +36 -6
  34. package/build/dataform-controls/toggle-group.js.map +1 -1
  35. package/build/dataform-controls/toggle.js +33 -22
  36. package/build/dataform-controls/toggle.js.map +1 -1
  37. package/build/dataform-controls/url.js +7 -1
  38. package/build/dataform-controls/url.js.map +1 -1
  39. package/build/dataform-controls/utils/validated-input.js +34 -32
  40. package/build/dataform-controls/utils/validated-input.js.map +1 -1
  41. package/build/dataforms-layouts/panel/dropdown.js +10 -14
  42. package/build/dataforms-layouts/panel/dropdown.js.map +1 -1
  43. package/build/dataforms-layouts/panel/index.js +24 -11
  44. package/build/dataforms-layouts/panel/index.js.map +1 -1
  45. package/build/dataforms-layouts/panel/modal.js +22 -27
  46. package/build/dataforms-layouts/panel/modal.js.map +1 -1
  47. package/build/dataforms-layouts/panel/summary-button.js +67 -0
  48. package/build/dataforms-layouts/panel/summary-button.js.map +1 -0
  49. package/build/normalize-fields.js +17 -0
  50. package/build/normalize-fields.js.map +1 -1
  51. package/build/types.js.map +1 -1
  52. package/build-module/components/dataviews-filters/input-widget.js +48 -4
  53. package/build-module/components/dataviews-filters/input-widget.js.map +1 -1
  54. package/build-module/dataform-controls/array.js +9 -7
  55. package/build-module/dataform-controls/array.js.map +1 -1
  56. package/build-module/dataform-controls/checkbox.js +31 -21
  57. package/build-module/dataform-controls/checkbox.js.map +1 -1
  58. package/build-module/dataform-controls/color.js +28 -24
  59. package/build-module/dataform-controls/color.js.map +1 -1
  60. package/build-module/dataform-controls/date.js +32 -24
  61. package/build-module/dataform-controls/date.js.map +1 -1
  62. package/build-module/dataform-controls/datetime.js +135 -21
  63. package/build-module/dataform-controls/datetime.js.map +1 -1
  64. package/build-module/dataform-controls/email.js +7 -1
  65. package/build-module/dataform-controls/email.js.map +1 -1
  66. package/build-module/dataform-controls/index.js +23 -0
  67. package/build-module/dataform-controls/index.js.map +1 -1
  68. package/build-module/dataform-controls/integer.js +46 -34
  69. package/build-module/dataform-controls/integer.js.map +1 -1
  70. package/build-module/dataform-controls/radio.js +44 -11
  71. package/build-module/dataform-controls/radio.js.map +1 -1
  72. package/build-module/dataform-controls/relative-date-control.js +6 -10
  73. package/build-module/dataform-controls/relative-date-control.js.map +1 -1
  74. package/build-module/dataform-controls/select.js +43 -12
  75. package/build-module/dataform-controls/select.js.map +1 -1
  76. package/build-module/dataform-controls/telephone.js +7 -1
  77. package/build-module/dataform-controls/telephone.js.map +1 -1
  78. package/build-module/dataform-controls/text.js +14 -2
  79. package/build-module/dataform-controls/text.js.map +1 -1
  80. package/build-module/dataform-controls/textarea.js +32 -20
  81. package/build-module/dataform-controls/textarea.js.map +1 -1
  82. package/build-module/dataform-controls/toggle-group.js +38 -8
  83. package/build-module/dataform-controls/toggle-group.js.map +1 -1
  84. package/build-module/dataform-controls/toggle.js +33 -23
  85. package/build-module/dataform-controls/toggle.js.map +1 -1
  86. package/build-module/dataform-controls/url.js +7 -1
  87. package/build-module/dataform-controls/url.js.map +1 -1
  88. package/build-module/dataform-controls/utils/validated-input.js +34 -33
  89. package/build-module/dataform-controls/utils/validated-input.js.map +1 -1
  90. package/build-module/dataforms-layouts/panel/dropdown.js +10 -15
  91. package/build-module/dataforms-layouts/panel/dropdown.js.map +1 -1
  92. package/build-module/dataforms-layouts/panel/index.js +24 -11
  93. package/build-module/dataforms-layouts/panel/index.js.map +1 -1
  94. package/build-module/dataforms-layouts/panel/modal.js +22 -28
  95. package/build-module/dataforms-layouts/panel/modal.js.map +1 -1
  96. package/build-module/dataforms-layouts/panel/summary-button.js +60 -0
  97. package/build-module/dataforms-layouts/panel/summary-button.js.map +1 -0
  98. package/build-module/normalize-fields.js +15 -0
  99. package/build-module/normalize-fields.js.map +1 -1
  100. package/build-module/types.js.map +1 -1
  101. package/build-types/components/dataform/stories/index.story.d.ts +3 -0
  102. package/build-types/components/dataform/stories/index.story.d.ts.map +1 -1
  103. package/build-types/components/dataviews/stories/fixtures.d.ts +4 -2
  104. package/build-types/components/dataviews/stories/fixtures.d.ts.map +1 -1
  105. package/build-types/components/dataviews-filters/input-widget.d.ts.map +1 -1
  106. package/build-types/dataform-controls/array.d.ts.map +1 -1
  107. package/build-types/dataform-controls/checkbox.d.ts.map +1 -1
  108. package/build-types/dataform-controls/color.d.ts.map +1 -1
  109. package/build-types/dataform-controls/date.d.ts.map +1 -1
  110. package/build-types/dataform-controls/datetime.d.ts.map +1 -1
  111. package/build-types/dataform-controls/email.d.ts.map +1 -1
  112. package/build-types/dataform-controls/index.d.ts +1 -1
  113. package/build-types/dataform-controls/index.d.ts.map +1 -1
  114. package/build-types/dataform-controls/integer.d.ts.map +1 -1
  115. package/build-types/dataform-controls/radio.d.ts.map +1 -1
  116. package/build-types/dataform-controls/relative-date-control.d.ts +6 -5
  117. package/build-types/dataform-controls/relative-date-control.d.ts.map +1 -1
  118. package/build-types/dataform-controls/select.d.ts.map +1 -1
  119. package/build-types/dataform-controls/telephone.d.ts.map +1 -1
  120. package/build-types/dataform-controls/text.d.ts +1 -1
  121. package/build-types/dataform-controls/text.d.ts.map +1 -1
  122. package/build-types/dataform-controls/textarea.d.ts +1 -1
  123. package/build-types/dataform-controls/textarea.d.ts.map +1 -1
  124. package/build-types/dataform-controls/toggle-group.d.ts.map +1 -1
  125. package/build-types/dataform-controls/toggle.d.ts.map +1 -1
  126. package/build-types/dataform-controls/url.d.ts.map +1 -1
  127. package/build-types/dataform-controls/utils/validated-input.d.ts +4 -4
  128. package/build-types/dataform-controls/utils/validated-input.d.ts.map +1 -1
  129. package/build-types/dataforms-layouts/panel/dropdown.d.ts +2 -1
  130. package/build-types/dataforms-layouts/panel/dropdown.d.ts.map +1 -1
  131. package/build-types/dataforms-layouts/panel/index.d.ts.map +1 -1
  132. package/build-types/dataforms-layouts/panel/modal.d.ts +2 -1
  133. package/build-types/dataforms-layouts/panel/modal.d.ts.map +1 -1
  134. package/build-types/dataforms-layouts/panel/summary-button.d.ts +15 -0
  135. package/build-types/dataforms-layouts/panel/summary-button.d.ts.map +1 -0
  136. package/build-types/field-types/stories/index.story.d.ts.map +1 -1
  137. package/build-types/normalize-fields.d.ts +3 -0
  138. package/build-types/normalize-fields.d.ts.map +1 -1
  139. package/build-types/types.d.ts +66 -3
  140. package/build-types/types.d.ts.map +1 -1
  141. package/build-wp/index.js +1468 -1422
  142. package/package.json +16 -15
  143. package/src/components/dataform/stories/index.story.tsx +436 -7
  144. package/src/components/dataviews/stories/fixtures.tsx +99 -41
  145. package/src/components/dataviews/stories/index.story.tsx +1 -1
  146. package/src/components/dataviews-filters/input-widget.tsx +44 -5
  147. package/src/components/dataviews-picker/stories/index.story.tsx +1 -1
  148. package/src/dataform-controls/array.tsx +4 -6
  149. package/src/dataform-controls/checkbox.tsx +41 -24
  150. package/src/dataform-controls/color.tsx +33 -24
  151. package/src/dataform-controls/date.tsx +47 -21
  152. package/src/dataform-controls/datetime.tsx +171 -23
  153. package/src/dataform-controls/email.tsx +9 -1
  154. package/src/dataform-controls/index.tsx +26 -0
  155. package/src/dataform-controls/integer.tsx +82 -49
  156. package/src/dataform-controls/radio.tsx +53 -11
  157. package/src/dataform-controls/relative-date-control.tsx +11 -10
  158. package/src/dataform-controls/select.tsx +53 -10
  159. package/src/dataform-controls/telephone.tsx +9 -1
  160. package/src/dataform-controls/text.tsx +18 -1
  161. package/src/dataform-controls/textarea.tsx +38 -24
  162. package/src/dataform-controls/toggle-group.tsx +50 -10
  163. package/src/dataform-controls/toggle.tsx +41 -24
  164. package/src/dataform-controls/url.tsx +9 -1
  165. package/src/dataform-controls/utils/validated-input.tsx +50 -50
  166. package/src/dataforms-layouts/panel/dropdown.tsx +12 -23
  167. package/src/dataforms-layouts/panel/index.tsx +39 -16
  168. package/src/dataforms-layouts/panel/modal.tsx +24 -30
  169. package/src/dataforms-layouts/panel/summary-button.tsx +92 -0
  170. package/src/field-types/stories/index.story.tsx +89 -1
  171. package/src/normalize-fields.ts +18 -0
  172. package/src/test/filter-and-sort-data-view.js +148 -138
  173. package/src/test/normalize-fields.ts +114 -0
  174. package/src/types.ts +73 -3
  175. package/tsconfig.tsbuildinfo +1 -1
@@ -25,8 +25,10 @@ export type Theme = {
25
25
 
26
26
  export type SpaceObject = {
27
27
  id: number;
28
- title: string;
29
- description: string;
28
+ name: {
29
+ title: string;
30
+ description: string;
31
+ };
30
32
  image: string;
31
33
  type: string;
32
34
  isPlanet: boolean;
@@ -40,9 +42,11 @@ export type SpaceObject = {
40
42
  export const data: SpaceObject[] = [
41
43
  {
42
44
  id: 1,
43
- title: 'Moon',
44
- description:
45
- 'The Moon is Earth’s only natural satellite, orbiting at an average distance of 384,400 kilometers with a synchronous rotation that leads to fixed lunar phases as seen from Earth. Its cratered surface and subtle glow define night skies, inspiring exploration missions and influencing tides and biological rhythms worldwide.',
45
+ name: {
46
+ title: 'Moon',
47
+ description:
48
+ "The Moon is Earth's only natural satellite, orbiting at an average distance of 384,400 kilometers with a synchronous rotation that leads to fixed lunar phases as seen from Earth. Its cratered surface and subtle glow define night skies, inspiring exploration missions and influencing tides and biological rhythms worldwide.",
49
+ },
46
50
  image: 'https://live.staticflickr.com/7398/9458193857_e1256123e3_z.jpg',
47
51
  type: 'Satellite',
48
52
  isPlanet: false,
@@ -54,8 +58,10 @@ export const data: SpaceObject[] = [
54
58
  },
55
59
  {
56
60
  id: 2,
57
- title: 'Io',
58
- description: 'Moon of Jupiter',
61
+ name: {
62
+ title: 'Io',
63
+ description: 'Moon of Jupiter',
64
+ },
59
65
  image: 'https://live.staticflickr.com/5482/9460973502_07e8ab81fe_z.jpg',
60
66
  type: 'Satellite',
61
67
  isPlanet: false,
@@ -67,8 +73,10 @@ export const data: SpaceObject[] = [
67
73
  },
68
74
  {
69
75
  id: 3,
70
- title: 'Europa',
71
- description: 'Moon of Jupiter',
76
+ name: {
77
+ title: 'Europa',
78
+ description: 'Moon of Jupiter',
79
+ },
72
80
  image: 'https://live.staticflickr.com/65535/31499273012_baf5f38cc1_z.jpg',
73
81
  type: 'Satellite',
74
82
  isPlanet: false,
@@ -80,8 +88,10 @@ export const data: SpaceObject[] = [
80
88
  },
81
89
  {
82
90
  id: 4,
83
- title: 'Ganymede',
84
- description: 'Largest moon of Jupiter',
91
+ name: {
92
+ title: 'Ganymede',
93
+ description: 'Largest moon of Jupiter',
94
+ },
85
95
  image: 'https://live.staticflickr.com/7816/33436473218_a836235935_k.jpg',
86
96
  type: 'Satellite',
87
97
  isPlanet: false,
@@ -93,8 +103,10 @@ export const data: SpaceObject[] = [
93
103
  },
94
104
  {
95
105
  id: 5,
96
- title: 'Callisto',
97
- description: 'Outermost Galilean moon of Jupiter',
106
+ name: {
107
+ title: 'Callisto',
108
+ description: 'Outermost Galilean moon of Jupiter',
109
+ },
98
110
  image: 'https://live.staticflickr.com/804/27604150528_4512448a9c_c.jpg',
99
111
  type: 'Satellite',
100
112
  isPlanet: false,
@@ -106,8 +118,10 @@ export const data: SpaceObject[] = [
106
118
  },
107
119
  {
108
120
  id: 6,
109
- title: 'Amalthea',
110
- description: 'Small irregular moon of Jupiter',
121
+ name: {
122
+ title: 'Amalthea',
123
+ description: 'Small irregular moon of Jupiter',
124
+ },
111
125
  image: 'https://upload.wikimedia.org/wikipedia/commons/6/62/Amalthea.gif',
112
126
  type: 'Satellite',
113
127
  isPlanet: false,
@@ -119,8 +133,10 @@ export const data: SpaceObject[] = [
119
133
  },
120
134
  {
121
135
  id: 7,
122
- title: 'Himalia',
123
- description: 'Largest irregular moon of Jupiter',
136
+ name: {
137
+ title: 'Himalia',
138
+ description: 'Largest irregular moon of Jupiter',
139
+ },
124
140
  image: 'https://upload.wikimedia.org/wikipedia/commons/c/c2/Cassini-Huygens_Image_of_Himalia.png',
125
141
  type: 'Satellite',
126
142
  isPlanet: false,
@@ -132,8 +148,10 @@ export const data: SpaceObject[] = [
132
148
  },
133
149
  {
134
150
  id: 8,
135
- title: 'Neptune',
136
- description: 'Ice giant in the Solar system',
151
+ name: {
152
+ title: 'Neptune',
153
+ description: 'Ice giant in the Solar system',
154
+ },
137
155
  image: 'https://live.staticflickr.com/65535/29523683990_000ff4720c_z.jpg',
138
156
  type: 'Ice giant',
139
157
  isPlanet: true,
@@ -145,8 +163,10 @@ export const data: SpaceObject[] = [
145
163
  },
146
164
  {
147
165
  id: 9,
148
- title: 'Triton',
149
- description: 'Largest moon of Neptune',
166
+ name: {
167
+ title: 'Triton',
168
+ description: 'Largest moon of Neptune',
169
+ },
150
170
  image: 'https://live.staticflickr.com/65535/50728384241_02c5126c30_h.jpg',
151
171
  type: 'Satellite',
152
172
  isPlanet: false,
@@ -158,8 +178,10 @@ export const data: SpaceObject[] = [
158
178
  },
159
179
  {
160
180
  id: 10,
161
- title: 'Nereid',
162
- description: 'Irregular moon of Neptune',
181
+ name: {
182
+ title: 'Nereid',
183
+ description: 'Irregular moon of Neptune',
184
+ },
163
185
  image: 'https://upload.wikimedia.org/wikipedia/commons/b/b0/Nereid-Voyager2.jpg',
164
186
  type: 'Satellite',
165
187
  isPlanet: false,
@@ -171,8 +193,10 @@ export const data: SpaceObject[] = [
171
193
  },
172
194
  {
173
195
  id: 11,
174
- title: 'Proteus',
175
- description: 'Second-largest moon of Neptune',
196
+ name: {
197
+ title: 'Proteus',
198
+ description: 'Second-largest moon of Neptune',
199
+ },
176
200
  image: 'https://live.staticflickr.com/65535/50727825808_bf427e007b_c.jpg',
177
201
  type: 'Satellite',
178
202
  isPlanet: false,
@@ -184,8 +208,10 @@ export const data: SpaceObject[] = [
184
208
  },
185
209
  {
186
210
  id: 12,
187
- title: 'Mercury',
188
- description: 'Terrestrial planet in the Solar system',
211
+ name: {
212
+ title: 'Mercury',
213
+ description: 'Terrestrial planet in the Solar system',
214
+ },
189
215
  image: 'https://live.staticflickr.com/813/40199101735_e5e92ffd11_z.jpg',
190
216
  type: 'Terrestrial',
191
217
  isPlanet: true,
@@ -197,8 +223,10 @@ export const data: SpaceObject[] = [
197
223
  },
198
224
  {
199
225
  id: 13,
200
- title: 'Venus',
201
- description: 'La planète Vénus',
226
+ name: {
227
+ title: 'Venus',
228
+ description: 'La planète Vénus',
229
+ },
202
230
  image: 'https://live.staticflickr.com/8025/7544560662_900e717727_z.jpg',
203
231
  type: 'Terrestrial',
204
232
  isPlanet: true,
@@ -210,8 +238,10 @@ export const data: SpaceObject[] = [
210
238
  },
211
239
  {
212
240
  id: 14,
213
- title: 'Earth',
214
- description: 'Terrestrial planet in the Solar system',
241
+ name: {
242
+ title: 'Earth',
243
+ description: 'Terrestrial planet in the Solar system',
244
+ },
215
245
  image: 'https://live.staticflickr.com/3762/9460163562_964fe6af07_z.jpg',
216
246
  type: 'Terrestrial',
217
247
  isPlanet: true,
@@ -223,8 +253,10 @@ export const data: SpaceObject[] = [
223
253
  },
224
254
  {
225
255
  id: 15,
226
- title: 'Mars',
227
- description: 'Terrestrial planet in the Solar system',
256
+ name: {
257
+ title: 'Mars',
258
+ description: 'Terrestrial planet in the Solar system',
259
+ },
228
260
  image: 'https://live.staticflickr.com/8151/7651156426_e047f4d219_z.jpg',
229
261
  type: 'Terrestrial',
230
262
  isPlanet: true,
@@ -236,8 +268,10 @@ export const data: SpaceObject[] = [
236
268
  },
237
269
  {
238
270
  id: 16,
239
- title: 'Jupiter',
240
- description: 'Gas giant in the Solar system',
271
+ name: {
272
+ title: 'Jupiter',
273
+ description: 'Gas giant in the Solar system',
274
+ },
241
275
  image: 'https://staging-jubilee.flickr.com/2853/9458010071_6e6fc41408_z.jpg',
242
276
  type: 'Gas giant',
243
277
  isPlanet: true,
@@ -249,8 +283,10 @@ export const data: SpaceObject[] = [
249
283
  },
250
284
  {
251
285
  id: 17,
252
- title: 'Saturn',
253
- description: 'Gas giant in the Solar system',
286
+ name: {
287
+ title: 'Saturn',
288
+ description: 'Gas giant in the Solar system',
289
+ },
254
290
  image: 'https://live.staticflickr.com/5524/9464658509_fc2d83dff5_z.jpg',
255
291
  type: 'Gas giant',
256
292
  isPlanet: true,
@@ -262,8 +298,10 @@ export const data: SpaceObject[] = [
262
298
  },
263
299
  {
264
300
  id: 18,
265
- title: 'Uranus',
266
- description: 'Ice giant in the Solar system',
301
+ name: {
302
+ title: 'Uranus',
303
+ description: 'Ice giant in the Solar system',
304
+ },
267
305
  image: 'https://live.staticflickr.com/65535/5553350875_3072df91e2_c.jpg',
268
306
  type: 'Ice giant',
269
307
  isPlanet: true,
@@ -722,7 +760,7 @@ export const actions: Action< SpaceObject >[] = [
722
760
  return (
723
761
  <VStack spacing="5">
724
762
  <Text>
725
- { `Are you sure you want to delete "${ items[ 0 ].title }"?` }
763
+ { `Are you sure you want to delete "${ items[ 0 ].name.title }"?` }
726
764
  </Text>
727
765
  <HStack justify="right">
728
766
  <Button
@@ -777,6 +815,15 @@ export const fields: Field< SpaceObject >[] = [
777
815
  filterBy: {
778
816
  operators: [ 'contains', 'notContains', 'startsWith' ],
779
817
  },
818
+ isValid: {
819
+ required: true,
820
+ },
821
+ getValue: ( { item } ) => item.name.title,
822
+ setValue: ( { value } ) => ( {
823
+ name: {
824
+ title: value,
825
+ },
826
+ } ),
780
827
  },
781
828
  {
782
829
  id: 'date',
@@ -806,6 +853,9 @@ export const fields: Field< SpaceObject >[] = [
806
853
  id: 'isPlanet',
807
854
  label: 'Is Planet',
808
855
  type: 'boolean',
856
+ setValue: ( { value } ) => ( {
857
+ isPlanet: value === 'true',
858
+ } ),
809
859
  elements: [
810
860
  { value: true, label: 'True' },
811
861
  { value: false, label: 'False' },
@@ -823,7 +873,15 @@ export const fields: Field< SpaceObject >[] = [
823
873
  type: 'text',
824
874
  enableSorting: false,
825
875
  enableGlobalSearch: true,
826
- filterBy: false,
876
+ filterBy: {
877
+ operators: [ 'contains', 'notContains', 'startsWith' ],
878
+ },
879
+ getValue: ( { item } ) => item.name.description,
880
+ setValue: ( { value } ) => ( {
881
+ name: {
882
+ description: value,
883
+ },
884
+ } ),
827
885
  },
828
886
  {
829
887
  label: 'Email',
@@ -96,7 +96,7 @@ export const Default = ( { perPageSizes = [ 10, 25, 50, 100 ] } ) => {
96
96
  onClick={ ( e ) => {
97
97
  e.stopPropagation();
98
98
  // eslint-disable-next-line no-alert
99
- alert( 'Clicked: ' + item.title );
99
+ alert( 'Clicked: ' + item.name.title );
100
100
  } }
101
101
  { ...props }
102
102
  />
@@ -32,13 +32,52 @@ export default function InputWidget( {
32
32
  const currentFilter = view.filters?.find(
33
33
  ( f ) => f.field === filter.field
34
34
  );
35
-
36
- const field = fields.find( ( f ) => f.id === filter.field );
37
35
  const currentValue = getCurrentValue( filter, currentFilter );
36
+
37
+ /*
38
+ * We are reusing the field.Edit component for filters. By doing so,
39
+ * we get for free a filter control specific to the field type
40
+ * and other aspects of the field API (Edit control configuration, etc.).
41
+ *
42
+ * This approach comes with an issue: the field.Edit controls work with getValue
43
+ * and setValue methods, which take an item (Item) as parameter. But, at this point,
44
+ * we don't have an item and we don't know how to create one, either.
45
+ *
46
+ * So, what we do is to prepare the data and the relevant field configuration
47
+ * as if Item was a plain object whose keys are the field ids:
48
+ *
49
+ * {
50
+ * [ fieldOne.id ]: value,
51
+ * [ fieldTwo.id ]: value,
52
+ * }
53
+ *
54
+ */
55
+ const field = useMemo( () => {
56
+ const currentField = fields.find( ( f ) => f.id === filter.field );
57
+ if ( currentField ) {
58
+ return {
59
+ ...currentField,
60
+ // Deactivate validation for filters.
61
+ isValid: {
62
+ required: false,
63
+ custom: () => null,
64
+ },
65
+ // Configure getValue/setValue as if Item was a plain object.
66
+ getValue: ( { item }: { item: any } ) =>
67
+ item[ currentField.id ],
68
+ setValue: ( { value }: { value: any } ) => ( {
69
+ [ currentField.id ]: value,
70
+ } ),
71
+ };
72
+ }
73
+ return currentField;
74
+ }, [ fields, filter.field ] );
75
+
38
76
  const data = useMemo( () => {
39
77
  return ( view.filters ?? [] ).reduce(
40
- ( acc, f ) => {
41
- acc[ f.field ] = f.value;
78
+ ( acc, activeFilter ) => {
79
+ // We can now assume the field is stored as a Item prop.
80
+ acc[ activeFilter.field ] = activeFilter.value;
42
81
  return acc;
43
82
  },
44
83
  {} as Record< string, any >
@@ -49,7 +88,7 @@ export default function InputWidget( {
49
88
  if ( ! field || ! currentFilter ) {
50
89
  return;
51
90
  }
52
- const nextValue = updatedData[ field.id ];
91
+ const nextValue = field.getValue( { item: updatedData } );
53
92
  if ( fastDeepEqual( nextValue, currentValue ) ) {
54
93
  return;
55
94
  }
@@ -86,7 +86,7 @@ export const Default = ( {
86
86
  .filter(
87
87
  ( item ) => selection?.includes( String( item.id ) )
88
88
  )
89
- .map( ( item ) => item.title )
89
+ .map( ( item ) => item.name.title )
90
90
  .join( ', ' );
91
91
  // eslint-disable-next-line no-alert
92
92
  window.alert( selectedItemNames );
@@ -15,8 +15,8 @@ export default function ArrayControl< Item >( {
15
15
  onChange,
16
16
  hideLabelFromVision,
17
17
  }: DataFormControlProps< Item > ) {
18
- const { id, label, placeholder, elements } = field;
19
- const value = field.getValue( { item: data } );
18
+ const { label, placeholder, elements, getValue, setValue } = field;
19
+ const value = getValue( { item: data } );
20
20
 
21
21
  const findElementByValue = useCallback(
22
22
  ( suggestionValue: string ) => {
@@ -61,11 +61,9 @@ export default function ArrayControl< Item >( {
61
61
  return tokenByLabel?.value || token;
62
62
  } );
63
63
 
64
- onChange( {
65
- [ id ]: stringTokens,
66
- } );
64
+ onChange( setValue( { item: data, value: stringTokens } ) );
67
65
  },
68
- [ id, onChange, findElementByLabel ]
66
+ [ onChange, setValue, data, findElementByLabel ]
69
67
  );
70
68
 
71
69
  return (
@@ -1,8 +1,13 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import deepMerge from 'deepmerge';
5
+
1
6
  /**
2
7
  * WordPress dependencies
3
8
  */
4
9
  import { privateApis } from '@wordpress/components';
5
- import { useState } from '@wordpress/element';
10
+ import { useState, useCallback } from '@wordpress/element';
6
11
 
7
12
  /**
8
13
  * Internal dependencies
@@ -18,7 +23,7 @@ export default function Checkbox< Item >( {
18
23
  data,
19
24
  hideLabelFromVision,
20
25
  }: DataFormControlProps< Item > ) {
21
- const { id, getValue, label, description } = field;
26
+ const { getValue, setValue, label, description } = field;
22
27
  const [ customValidity, setCustomValidity ] =
23
28
  useState<
24
29
  React.ComponentProps<
@@ -26,36 +31,48 @@ export default function Checkbox< Item >( {
26
31
  >[ 'customValidity' ]
27
32
  >( undefined );
28
33
 
34
+ const onChangeControl = useCallback( () => {
35
+ onChange(
36
+ setValue( { item: data, value: ! getValue( { item: data } ) } )
37
+ );
38
+ }, [ data, getValue, onChange, setValue ] );
39
+
40
+ const onValidateControl = useCallback(
41
+ ( newValue: any ) => {
42
+ const message = field.isValid?.custom?.(
43
+ deepMerge(
44
+ data,
45
+ setValue( {
46
+ item: data,
47
+ value: newValue,
48
+ } ) as Partial< Item >
49
+ ),
50
+ field
51
+ );
52
+
53
+ if ( message ) {
54
+ setCustomValidity( {
55
+ type: 'invalid',
56
+ message,
57
+ } );
58
+ return;
59
+ }
60
+
61
+ setCustomValidity( undefined );
62
+ },
63
+ [ data, field, setValue ]
64
+ );
65
+
29
66
  return (
30
67
  <ValidatedCheckboxControl
31
68
  required={ !! field.isValid?.required }
32
- onValidate={ ( newValue: any ) => {
33
- const message = field.isValid?.custom?.(
34
- {
35
- ...data,
36
- [ id ]: newValue,
37
- },
38
- field
39
- );
40
-
41
- if ( message ) {
42
- setCustomValidity( {
43
- type: 'invalid',
44
- message,
45
- } );
46
- return;
47
- }
48
-
49
- setCustomValidity( undefined );
50
- } }
69
+ onValidate={ onValidateControl }
51
70
  customValidity={ customValidity }
52
71
  hidden={ hideLabelFromVision }
53
72
  label={ label }
54
73
  help={ description }
55
74
  checked={ getValue( { item: data } ) }
56
- onChange={ () =>
57
- onChange( { [ id ]: ! getValue( { item: data } ) } )
58
- }
75
+ onChange={ onChangeControl }
59
76
  />
60
77
  );
61
78
  }
@@ -2,6 +2,7 @@
2
2
  * External dependencies
3
3
  */
4
4
  import { colord } from 'colord';
5
+ import deepMerge from 'deepmerge';
5
6
 
6
7
  /**
7
8
  * WordPress dependencies
@@ -75,7 +76,7 @@ export default function Color< Item >( {
75
76
  onChange,
76
77
  hideLabelFromVision,
77
78
  }: DataFormControlProps< Item > ) {
78
- const { id, label, placeholder, description } = field;
79
+ const { label, placeholder, description, setValue } = field;
79
80
  const value = field.getValue( { item: data } ) || '';
80
81
  const [ customValidity, setCustomValidity ] =
81
82
  useState<
@@ -86,40 +87,48 @@ export default function Color< Item >( {
86
87
 
87
88
  const handleColorChange = useCallback(
88
89
  ( colorObject: any ) => {
89
- onChange( { [ id ]: colorObject.toHex() } );
90
+ onChange( setValue( { item: data, value: colorObject.toHex() } ) );
90
91
  },
91
- [ id, onChange ]
92
+ [ data, onChange, setValue ]
92
93
  );
93
94
 
94
95
  const handleInputChange = useCallback(
95
96
  ( newValue: string | undefined ) => {
96
- onChange( { [ id ]: newValue || '' } );
97
+ onChange( setValue( { item: data, value: newValue || '' } ) );
97
98
  },
98
- [ id, onChange ]
99
+ [ data, onChange, setValue ]
100
+ );
101
+
102
+ const onValidateControl = useCallback(
103
+ ( newValue: any ) => {
104
+ const message = field.isValid?.custom?.(
105
+ deepMerge(
106
+ data,
107
+ setValue( {
108
+ item: data,
109
+ value: newValue,
110
+ } ) as Partial< Item >
111
+ ),
112
+ field
113
+ );
114
+
115
+ if ( message ) {
116
+ setCustomValidity( {
117
+ type: 'invalid',
118
+ message,
119
+ } );
120
+ return;
121
+ }
122
+
123
+ setCustomValidity( undefined );
124
+ },
125
+ [ data, field, setValue ]
99
126
  );
100
127
 
101
128
  return (
102
129
  <ValidatedInputControl
103
130
  required={ !! field.isValid?.required }
104
- onValidate={ ( newValue: any ) => {
105
- const message = field.isValid?.custom?.(
106
- {
107
- ...data,
108
- [ id ]: newValue,
109
- },
110
- field
111
- );
112
-
113
- if ( message ) {
114
- setCustomValidity( {
115
- type: 'invalid',
116
- message,
117
- } );
118
- return;
119
- }
120
-
121
- setCustomValidity( undefined );
122
- } }
131
+ onValidate={ onValidateControl }
123
132
  customValidity={ customValidity }
124
133
  label={ label }
125
134
  placeholder={ placeholder }