@planning-inspectorate/dynamic-forms 3.22.0 → 3.23.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@planning-inspectorate/dynamic-forms",
3
- "version": "3.22.0",
3
+ "version": "3.23.0",
4
4
  "description": "This package is for building GDS forms using a configuration-based approach.",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -23,9 +23,8 @@
23
23
  id: question.fieldName,
24
24
  name: question.fieldName,
25
25
  value: question.value,
26
- suffix: {
27
- text: question.suffix
28
- },
26
+ prefix: question.prefix,
27
+ suffix: question.suffix,
29
28
  errorMessage: errors[question.fieldName] and {
30
29
  text: errors[question.fieldName].msg
31
30
  },
@@ -2,8 +2,9 @@ export class NumberEntryQuestion extends Question {
2
2
  /**
3
3
  * @param {import('#typedefs/question-props.d.ts').NumberEntryQuestionParams} params
4
4
  */
5
- constructor({ label, suffix, ...parentParams }: import("#typedefs/question-props.d.ts").NumberEntryQuestionParams);
6
- suffix: string | undefined;
5
+ constructor({ label, prefix, suffix, ...parentParams }: import("#typedefs/question-props.d.ts").NumberEntryQuestionParams);
6
+ prefix: import("#typedefs/question-props.d.ts").Affix | undefined;
7
+ suffix: import("#typedefs/question-props.d.ts").Affix | undefined;
7
8
  label: string | undefined;
8
9
  answerForViewModel(answers: any): string | number;
9
10
  }
@@ -1 +1 @@
1
- {"version":3,"file":"question.d.ts","sourceRoot":"","sources":["question.js"],"names":[],"mappings":"AAGA;IACC;;OAEG;IACH,gDAFW,OAAO,+BAA+B,EAAE,yBAAyB,EAY3E;IAFA,2BAAoB;IACpB,0BAAkB;IAGnB,kDAEC;CASD;;yBA9BwB,WAAW"}
1
+ {"version":3,"file":"question.d.ts","sourceRoot":"","sources":["question.js"],"names":[],"mappings":"AAwBA;IACC;;OAEG;IACH,wDAFW,OAAO,+BAA+B,EAAE,yBAAyB,EAa3E;IAHA,kEAAoB;IACpB,kEAAuC;IACvC,0BAAkB;IAGnB,kDAEC;CAUD;;yBArDwB,WAAW"}
@@ -1,11 +1,32 @@
1
1
  import { Question } from '#question';
2
2
  import { getPersistedNumberAnswer } from '../utils/persisted-number-answer.js';
3
3
 
4
+ /**
5
+ * Normalises a suffix param into the govuk-frontend Affix shape.
6
+ * Supports the legacy plain-string format for backwards compatibility,
7
+ * emitting a deprecation warning when used.
8
+ *
9
+ * @param {string | import('#typedefs/question-props.d.ts').Affix | undefined} value
10
+ * @returns {import('#typedefs/question-props.d.ts').Affix | undefined}
11
+ */
12
+ function toAffix(value) {
13
+ if (value === undefined) {
14
+ return undefined;
15
+ }
16
+ if (typeof value === 'string') {
17
+ console.warn(
18
+ `NumberEntryQuestion: passing a string to 'suffix' is deprecated, use a govuk affix object ({ text, classes? }) instead`
19
+ );
20
+ return { text: value };
21
+ }
22
+ return value;
23
+ }
24
+
4
25
  export class NumberEntryQuestion extends Question {
5
26
  /**
6
27
  * @param {import('#typedefs/question-props.d.ts').NumberEntryQuestionParams} params
7
28
  */
8
- constructor({ label, suffix, ...parentParams }) {
29
+ constructor({ label, prefix, suffix, ...parentParams }) {
9
30
  super({
10
31
  // default but allow overrides
11
32
  capitaliseAnswer: false,
@@ -13,7 +34,8 @@ export class NumberEntryQuestion extends Question {
13
34
  viewFolder: 'number-entry'
14
35
  });
15
36
 
16
- this.suffix = suffix;
37
+ this.prefix = prefix;
38
+ this.suffix = toAffix(suffix, 'suffix');
17
39
  this.label = label;
18
40
  }
19
41
 
@@ -26,6 +48,7 @@ export class NumberEntryQuestion extends Question {
26
48
  */
27
49
  addCustomDataToViewModel(viewModel) {
28
50
  viewModel.question.label = this.label;
51
+ viewModel.question.prefix = this.prefix;
29
52
  viewModel.question.suffix = this.suffix;
30
53
  }
31
54
  }
@@ -14,4 +14,69 @@ describe('number-entry', () => {
14
14
 
15
15
  assert.strictEqual(question.capitaliseAnswer, true);
16
16
  });
17
+
18
+ it('should accept Affix objects for prefix and suffix', () => {
19
+ const PREFIX = { text: '£', classes: 'govuk-!-font-weight-bold' };
20
+ const SUFFIX = { text: 'kg' };
21
+
22
+ const question = new NumberEntryQuestion({
23
+ title: 'Question',
24
+ question: 'Question',
25
+ fieldName: 'question',
26
+ url: 'Question',
27
+ prefix: PREFIX,
28
+ suffix: SUFFIX
29
+ });
30
+
31
+ assert.strictEqual(question.prefix, PREFIX);
32
+ assert.strictEqual(question.suffix, SUFFIX);
33
+ });
34
+
35
+ it('should convert string suffix to Affix object and warn about deprecation', (t) => {
36
+ const warnMock = t.mock.method(console, 'warn', () => {});
37
+
38
+ const question = new NumberEntryQuestion({
39
+ title: 'Question',
40
+ question: 'Question',
41
+ fieldName: 'question',
42
+ url: 'Question',
43
+ suffix: 'kg'
44
+ });
45
+
46
+ assert.deepStrictEqual(question.suffix, { text: 'kg' });
47
+ assert.strictEqual(warnMock.mock.calls.length, 1);
48
+ assert.match(warnMock.mock.calls[0].arguments[0], /suffix.*deprecated/);
49
+ });
50
+
51
+ it('should leave prefix and suffix undefined when not provided', () => {
52
+ const question = new NumberEntryQuestion({
53
+ title: 'Question',
54
+ question: 'Question',
55
+ fieldName: 'question',
56
+ url: 'Question'
57
+ });
58
+
59
+ assert.strictEqual(question.prefix, undefined);
60
+ assert.strictEqual(question.suffix, undefined);
61
+ });
62
+
63
+ it('should add prefix and suffix to the view model', () => {
64
+ const PREFIX = { text: '£' };
65
+ const SUFFIX = { text: 'kg' };
66
+
67
+ const question = new NumberEntryQuestion({
68
+ title: 'Question',
69
+ question: 'Question',
70
+ fieldName: 'question',
71
+ url: 'Question',
72
+ prefix: PREFIX,
73
+ suffix: SUFFIX
74
+ });
75
+
76
+ const viewModel = { question: {} };
77
+ question.addCustomDataToViewModel(viewModel);
78
+
79
+ assert.strictEqual(viewModel.question.prefix, PREFIX);
80
+ assert.strictEqual(viewModel.question.suffix, SUFFIX);
81
+ });
17
82
  });
@@ -86,7 +86,7 @@ export type DividerOption = { divider?: string };
86
86
 
87
87
  export type Option = SelectableOption | DividerOption;
88
88
 
89
- type Affix = {
89
+ export type Affix = {
90
90
  text: string;
91
91
  classes?: string;
92
92
  };
@@ -228,7 +228,8 @@ export type MultiFieldInputQuestionProps = MultiFieldInputQuestionParams & {
228
228
  };
229
229
 
230
230
  export type NumberEntryQuestionParams = CommonQuestionParams & {
231
- suffix?: string;
231
+ prefix?: Affix;
232
+ suffix?: string | Affix;
232
233
  label?: string;
233
234
  };
234
235