@ukhomeoffice/cop-react-form-renderer 3.0.2 → 3.0.3

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.
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
+
8
10
  var _Container = _interopRequireDefault(require("../Container"));
9
11
 
10
12
  var _Data = _interopRequireDefault(require("../Data"));
@@ -35,6 +37,8 @@ var getFormPage = function getFormPage(pageOptions, formComponents, formData) {
35
37
  return null;
36
38
  }
37
39
 
40
+ pageOptions = interpolatePageOptions(pageOptions, formData);
41
+ formComponents = interpolateFormComponents(formComponents, formData);
38
42
  var components = pageOptions.components.map(function (componentOptions) {
39
43
  if (typeof componentOptions === 'string') {
40
44
  return (0, _getParagraphFromText.default)(componentOptions);
@@ -57,6 +61,42 @@ var getFormPage = function getFormPage(pageOptions, formComponents, formData) {
57
61
  actions: actions
58
62
  }));
59
63
  };
64
+ /**
65
+ * Interpolate 'Variable Expression' using formData - for local use only.
66
+ * @param {object} pageOptions The JSON page.
67
+ * @param {object} formData The top-level form data, used for setting up components.
68
+ * @returns interpolated pageOptions.
69
+ */
70
+
71
+
72
+ var interpolatePageOptions = function interpolatePageOptions(pageOptions, formData) {
73
+ return JSON.parse(_copReactComponents.Utils.interpolateString(JSON.stringify(pageOptions), formData));
74
+ };
75
+ /**
76
+ * Interpolate 'Variable Expression' using formData excluding each component's data block - for local use only.
77
+ * @param {Array} formComponents The components defined at the top-level of the form.
78
+ * @param {object} formData The top-level form data, used for setting up components.
79
+ * @returns interpolated formComponents
80
+ */
81
+
82
+
83
+ var interpolateFormComponents = function interpolateFormComponents(formComponents, formData) {
84
+ return formComponents.map(function (formComponent) {
85
+ var formComponentDataUrl = undefined;
86
+
87
+ if (formComponent.data && formComponent.data.url) {
88
+ formComponentDataUrl = formComponent.data.url;
89
+ }
90
+
91
+ var interpolatedFormComponent = JSON.parse(_copReactComponents.Utils.interpolateString(JSON.stringify(formComponent), formData));
92
+
93
+ if (formComponentDataUrl) {
94
+ interpolatedFormComponent.data.url = formComponentDataUrl;
95
+ }
96
+
97
+ return interpolatedFormComponent;
98
+ });
99
+ };
60
100
 
61
101
  var _default = getFormPage;
62
102
  exports.default = _default;
@@ -32,6 +32,18 @@ describe('utils', function () {
32
32
  data: {
33
33
  url: '${urls.refData}/v3/charlies'
34
34
  }
35
+ }, // eslint-disable-next-line no-template-curly-in-string
36
+ {
37
+ id: 'd',
38
+ fieldId: 'd',
39
+ label: 'Roger ${currentUser.firstname}',
40
+ type: 'text'
41
+ }, // eslint-disable-next-line no-template-curly-in-string
42
+ {
43
+ id: 'e',
44
+ fieldId: 'e',
45
+ label: 'Bravo ${currentUser.surname}',
46
+ type: 'text'
35
47
  }];
36
48
  var FORM_DATA = {
37
49
  urls: {
@@ -165,6 +177,59 @@ describe('utils', function () {
165
177
  var C = FORM_COMPONENTS[2];
166
178
  expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, {})).toEqual({
167
179
  title: PAGE.title,
180
+ components: [{
181
+ type: 'html',
182
+ tagName: 'p',
183
+ content: PAGE.components[0]
184
+ }, _objectSpread(_objectSpread({}, PAGE.components[1]), {}, {
185
+ full_path: PAGE.components[1].fieldId
186
+ }), {
187
+ type: 'html',
188
+ tagName: 'p',
189
+ content: PAGE.components[2]
190
+ }, // eslint-disable-next-line no-template-curly-in-string
191
+ _objectSpread(_objectSpread({
192
+ use: 'c'
193
+ }, C), {}, {
194
+ cya_label: C.label,
195
+ full_path: C.fieldId,
196
+ data: {
197
+ url: '${urls.refData}/v3/charlies'
198
+ }
199
+ })],
200
+ formData: {}
201
+ });
202
+ });
203
+ it('should interpolate and handle a page that references a form-level component with formData', function () {
204
+ var PAGE = {
205
+ // eslint-disable-next-line no-template-curly-in-string
206
+ title: 'Page ${postFixTitle}',
207
+ components: ["Opening paragraph", {
208
+ type: 'heading',
209
+ size: 'l',
210
+ content: 'Page heading'
211
+ }, "Closing paragraph", {
212
+ use: 'c'
213
+ }, {
214
+ use: 'd'
215
+ }, "Kevin", {
216
+ use: 'e'
217
+ }]
218
+ };
219
+
220
+ var DATA = _objectSpread(_objectSpread({}, FORM_DATA), {}, {
221
+ currentUser: {
222
+ firstname: 'Bob',
223
+ surname: 'Kevin'
224
+ },
225
+ postFixTitle: 'Everyone'
226
+ });
227
+
228
+ var C = FORM_COMPONENTS[2];
229
+ var D = FORM_COMPONENTS[3];
230
+ var E = FORM_COMPONENTS[4];
231
+ expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, DATA)).toEqual({
232
+ title: 'Page Everyone',
168
233
  components: [{
169
234
  type: 'html',
170
235
  tagName: 'p',
@@ -179,9 +244,28 @@ describe('utils', function () {
179
244
  use: 'c'
180
245
  }, C), {}, {
181
246
  cya_label: C.label,
247
+ data: {
248
+ url: "".concat(FORM_DATA.urls.refData, "/v3/charlies")
249
+ },
182
250
  full_path: C.fieldId
251
+ }), _objectSpread(_objectSpread({
252
+ use: 'd'
253
+ }, D), {}, {
254
+ label: 'Roger ' + DATA.currentUser.firstname,
255
+ cya_label: 'Roger ' + DATA.currentUser.firstname,
256
+ full_path: D.fieldId
257
+ }), {
258
+ type: 'html',
259
+ tagName: 'p',
260
+ content: PAGE.components[5]
261
+ }, _objectSpread(_objectSpread({
262
+ use: 'e'
263
+ }, E), {}, {
264
+ label: 'Bravo ' + DATA.currentUser.surname,
265
+ cya_label: 'Bravo ' + DATA.currentUser.surname,
266
+ full_path: E.fieldId
183
267
  })],
184
- formData: {}
268
+ formData: DATA
185
269
  });
186
270
  });
187
271
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",