@defra/forms-model 3.0.612 → 3.0.613

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.
@@ -2,6 +2,21 @@ import { ComponentType } from "../../../components/enums.js";
2
2
  import { PreviewComponent } from "./preview.js";
3
3
  import { Question, QuestionComponentElements } from "./question.js";
4
4
 
5
+ /**
6
+ * Formats a currency amount with thousand separators and two decimal places
7
+ * @param {number} value
8
+ * @param {'en-GB'} [locale] - locale for formatting
9
+ * @param {'GBP'} [currency] - currency code
10
+ * @returns {string} Formatted amount (e.g., "£1,234.56")
11
+ */
12
+ function formatCurrency(value, locale = 'en-GB', currency = 'GBP') {
13
+ const formatter = new Intl.NumberFormat(locale, {
14
+ style: 'currency',
15
+ currency
16
+ });
17
+ return formatter.format(value);
18
+ }
19
+
5
20
  /**
6
21
  * @implements {PaymentElements}
7
22
  */
@@ -99,7 +114,7 @@ export class PaymentQuestion extends Question {
99
114
  * @returns {PaymentModel}
100
115
  */
101
116
  _renderInput() {
102
- const amount = typeof this._paymentAmount === 'number' ? this._paymentAmount.toFixed(2) : '0.00';
117
+ const amount = typeof this._paymentAmount === 'number' ? formatCurrency(this._paymentAmount) : '£0.00';
103
118
  return {
104
119
  ...super._renderInput(),
105
120
  amount,
@@ -1 +1 @@
1
- {"version":3,"file":"payment.js","names":["ComponentType","PreviewComponent","Question","QuestionComponentElements","PaymentComponentPreviewElements","_paymentAmount","_paymentDescription","constructor","component","options","amount","description","values","paymentAmount","paymentDescription","PaymentQuestion","componentType","PaymentField","_questionTemplate","PATH","_fieldName","htmlElements","questionRenderer","val","render","_renderInput","toFixed","headingClasses"],"sources":["../../../../../src/form/form-editor/preview/payment.js"],"sourcesContent":["import { ComponentType } from '~/src/components/enums.js'\nimport { PreviewComponent } from '~/src/form/form-editor/preview/preview.js'\nimport {\n Question,\n QuestionComponentElements\n} from '~/src/form/form-editor/preview/question.js'\n\n/**\n * @implements {PaymentElements}\n */\nexport class PaymentComponentPreviewElements extends QuestionComponentElements {\n /**\n * @type {number}\n * @protected\n */\n _paymentAmount = 0\n\n /**\n * @type {string}\n * @protected\n */\n _paymentDescription = ''\n\n /**\n * @param {PaymentFieldComponent} component\n */\n constructor(component) {\n super(component)\n this._paymentAmount = component.options.amount\n this._paymentDescription = component.options.description\n }\n\n /**\n * @returns {PaymentSettings}\n */\n get values() {\n return {\n ...super.values,\n paymentAmount: this._paymentAmount,\n paymentDescription: this._paymentDescription\n }\n }\n}\n\nexport class PaymentQuestion extends Question {\n /**\n * @type {ComponentType}\n */\n componentType = ComponentType.PaymentField\n /**\n * @type {string}\n * @protected\n */\n _questionTemplate = PreviewComponent.PATH + 'paymentfield.njk'\n _fieldName = 'PaymentField'\n\n /**\n * @type {number}\n * @protected\n */\n _paymentAmount = 0\n\n /**\n * @type {string}\n * @protected\n */\n _paymentDescription = ''\n\n /**\n * @param {PaymentElements} htmlElements\n * @param {QuestionRenderer} questionRenderer\n */\n constructor(htmlElements, questionRenderer) {\n super(htmlElements, questionRenderer)\n this._paymentAmount = htmlElements.values.paymentAmount\n this._paymentDescription = htmlElements.values.paymentDescription\n }\n\n get paymentAmount() {\n return this._paymentAmount\n }\n\n /**\n * @param {number} val\n */\n set paymentAmount(val) {\n this._paymentAmount = val\n this.render()\n }\n\n get paymentDescription() {\n return this._paymentDescription\n }\n\n /**\n * @param {string} val\n */\n set paymentDescription(val) {\n this._paymentDescription = val\n this.render()\n }\n\n /**\n * @protected\n * @returns {PaymentModel}\n */\n _renderInput() {\n const amount =\n typeof this._paymentAmount === 'number'\n ? this._paymentAmount.toFixed(2)\n : '0.00'\n\n return {\n ...super._renderInput(),\n amount,\n description: this._paymentDescription || 'Payment description',\n headingClasses: 'govuk-heading-m'\n }\n }\n}\n\n/**\n * @import { PaymentSettings, PaymentElements, QuestionRenderer } from '~/src/form/form-editor/preview/types.js'\n * @import { PaymentModel } from '~/src/form/form-editor/macros/types.js'\n * @import { PaymentFieldComponent } from '~/src/components/types.js'\n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AO,IAAI,CAACR,cAAc;EAC5B;;EAEA;AACF;AACA;EACE,IAAIQ,aAAaA,CAACU,GAAG,EAAE;IACrB,IAAI,CAAClB,cAAc,GAAGkB,GAAG;IACzB,IAAI,CAACC,MAAM,CAAC,CAAC;EACf;EAEA,IAAIV,kBAAkBA,CAAA,EAAG;IACvB,OAAO,IAAI,CAACR,mBAAmB;EACjC;;EAEA;AACF;AACA;EACE,IAAIQ,kBAAkBA,CAACS,GAAG,EAAE;IAC1B,IAAI,CAACjB,mBAAmB,GAAGiB,GAAG;IAC9B,IAAI,CAACC,MAAM,CAAC,CAAC;EACf;;EAEA;AACF;AACA;AACA;EACEC,YAAYA,CAAA,EAAG;IACb,MAAMf,MAAM,GACV,OAAO,IAAI,CAACL,cAAc,KAAK,QAAQ,GACnC,IAAI,CAACA,cAAc,CAACqB,OAAO,CAAC,CAAC,CAAC,GAC9B,MAAM;IAEZ,OAAO;MACL,GAAG,KAAK,CAACD,YAAY,CAAC,CAAC;MACvBf,MAAM;MACNC,WAAW,EAAE,IAAI,CAACL,mBAAmB,IAAI,qBAAqB;MAC9DqB,cAAc,EAAE;IAClB,CAAC;EACH;AACF;;AAEA;AACA;AACA;AACA;AACA","ignoreList":[]}
1
+ {"version":3,"file":"payment.js","names":["ComponentType","PreviewComponent","Question","QuestionComponentElements","formatCurrency","value","locale","currency","formatter","Intl","NumberFormat","style","format","PaymentComponentPreviewElements","_paymentAmount","_paymentDescription","constructor","component","options","amount","description","values","paymentAmount","paymentDescription","PaymentQuestion","componentType","PaymentField","_questionTemplate","PATH","_fieldName","htmlElements","questionRenderer","val","render","_renderInput","headingClasses"],"sources":["../../../../../src/form/form-editor/preview/payment.js"],"sourcesContent":["import { ComponentType } from '~/src/components/enums.js'\nimport { PreviewComponent } from '~/src/form/form-editor/preview/preview.js'\nimport {\n Question,\n QuestionComponentElements\n} from '~/src/form/form-editor/preview/question.js'\n\n/**\n * Formats a currency amount with thousand separators and two decimal places\n * @param {number} value\n * @param {'en-GB'} [locale] - locale for formatting\n * @param {'GBP'} [currency] - currency code\n * @returns {string} Formatted amount (e.g., \"£1,234.56\")\n */\nfunction formatCurrency(value, locale = 'en-GB', currency = 'GBP') {\n const formatter = new Intl.NumberFormat(locale, {\n style: 'currency',\n currency\n })\n\n return formatter.format(value)\n}\n\n/**\n * @implements {PaymentElements}\n */\nexport class PaymentComponentPreviewElements extends QuestionComponentElements {\n /**\n * @type {number}\n * @protected\n */\n _paymentAmount = 0\n\n /**\n * @type {string}\n * @protected\n */\n _paymentDescription = ''\n\n /**\n * @param {PaymentFieldComponent} component\n */\n constructor(component) {\n super(component)\n this._paymentAmount = component.options.amount\n this._paymentDescription = component.options.description\n }\n\n /**\n * @returns {PaymentSettings}\n */\n get values() {\n return {\n ...super.values,\n paymentAmount: this._paymentAmount,\n paymentDescription: this._paymentDescription\n }\n }\n}\n\nexport class PaymentQuestion extends Question {\n /**\n * @type {ComponentType}\n */\n componentType = ComponentType.PaymentField\n /**\n * @type {string}\n * @protected\n */\n _questionTemplate = PreviewComponent.PATH + 'paymentfield.njk'\n _fieldName = 'PaymentField'\n\n /**\n * @type {number}\n * @protected\n */\n _paymentAmount = 0\n\n /**\n * @type {string}\n * @protected\n */\n _paymentDescription = ''\n\n /**\n * @param {PaymentElements} htmlElements\n * @param {QuestionRenderer} questionRenderer\n */\n constructor(htmlElements, questionRenderer) {\n super(htmlElements, questionRenderer)\n this._paymentAmount = htmlElements.values.paymentAmount\n this._paymentDescription = htmlElements.values.paymentDescription\n }\n\n get paymentAmount() {\n return this._paymentAmount\n }\n\n /**\n * @param {number} val\n */\n set paymentAmount(val) {\n this._paymentAmount = val\n this.render()\n }\n\n get paymentDescription() {\n return this._paymentDescription\n }\n\n /**\n * @param {string} val\n */\n set paymentDescription(val) {\n this._paymentDescription = val\n this.render()\n }\n\n /**\n * @protected\n * @returns {PaymentModel}\n */\n _renderInput() {\n const amount =\n typeof this._paymentAmount === 'number'\n ? formatCurrency(this._paymentAmount)\n : '£0.00'\n\n return {\n ...super._renderInput(),\n amount,\n description: this._paymentDescription || 'Payment description',\n headingClasses: 'govuk-heading-m'\n }\n }\n}\n\n/**\n * @import { PaymentSettings, PaymentElements, QuestionRenderer } from '~/src/form/form-editor/preview/types.js'\n * @import { PaymentModel } from '~/src/form/form-editor/macros/types.js'\n * @import { PaymentFieldComponent } from '~/src/components/types.js'\n */\n"],"mappings":"AAAA,SAASA,aAAa;AACtB,SAASC,gBAAgB;AACzB,SACEC,QAAQ,EACRC,yBAAyB;;AAG3B;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,cAAcA,CAACC,KAAK,EAAEC,MAAM,GAAG,OAAO,EAAEC,QAAQ,GAAG,KAAK,EAAE;EACjE,MAAMC,SAAS,GAAG,IAAIC,IAAI,CAACC,YAAY,CAACJ,MAAM,EAAE;IAC9CK,KAAK,EAAE,UAAU;IACjBJ;EACF,CAAC,CAAC;EAEF,OAAOC,SAAS,CAACI,MAAM,CAACP,KAAK,CAAC;AAChC;;AAEA;AACA;AACA;AACA,OAAO,MAAMQ,+BAA+B,SAASV,yBAAyB,CAAC;EAC7E;AACF;AACA;AACA;EACEW,cAAc,GAAG,CAAC;;EAElB;AACF;AACA;AACA;EACEC,mBAAmB,GAAG,EAAE;;EAExB;AACF;AACA;EACEC,WAAWA,CAACC,SAAS,EAAE;IACrB,KAAK,CAACA,SAAS,CAAC;IAChB,IAAI,CAACH,cAAc,GAAGG,SAAS,CAACC,OAAO,CAACC,MAAM;IAC9C,IAAI,CAACJ,mBAAmB,GAAGE,SAAS,CAACC,OAAO,CAACE,WAAW;EAC1D;;EAEA;AACF;AACA;EACE,IAAIC,MAAMA,CAAA,EAAG;IACX,OAAO;MACL,GAAG,KAAK,CAACA,MAAM;MACfC,aAAa,EAAE,IAAI,CAACR,cAAc;MAClCS,kBAAkB,EAAE,IAAI,CAACR;IAC3B,CAAC;EACH;AACF;AAEA,OAAO,MAAMS,eAAe,SAAStB,QAAQ,CAAC;EAC5C;AACF;AACA;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C;EACH;AACF;;AAEA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"payment.d.ts","sourceRoot":"","sources":["../../../../../src/form/form-editor/preview/payment.js"],"names":[],"mappings":"AAOA;;GAEG;AACH;IAaE;;OAEG;IACH,uBAFW,qBAAqB,EAM/B;IAnBD;;;OAGG;IACH,0BAHU,MAAM,CAGE;IAElB;;;OAGG;IACH,+BAHU,MAAM,CAGQ;IAWxB;;OAEG;IACH,cAFa,eAAe,CAQ3B;CACF;AAED;IAwBE;;;OAGG;IACH,0BAHW,eAAe,oBACf,gBAAgB,EAM1B;IApBD;;;OAGG;IACH,0BAHU,MAAM,CAGE;IAElB;;;OAGG;IACH,+BAHU,MAAM,CAGQ;IAgBxB;;OAEG;IACH,uBAFW,MAAM,EAKhB;IAVD,qBAKW,MAAM,CAHhB;IAcD;;OAEG;IACH,4BAFW,MAAM,EAKhB;IAVD,0BAKW,MAAM,CAHhB;IAUD;;;OAGG;IACH,0BAFa,YAAY,CAcxB;CACF;qCAGsE,yCAAyC;0CArHzG,4CAA4C;qCAqHoB,yCAAyC;2CAEtE,2BAA2B;yBAvH9D,4CAA4C;kCAsHlB,wCAAwC;sCADF,yCAAyC"}
1
+ {"version":3,"file":"payment.d.ts","sourceRoot":"","sources":["../../../../../src/form/form-editor/preview/payment.js"],"names":[],"mappings":"AAuBA;;GAEG;AACH;IAaE;;OAEG;IACH,uBAFW,qBAAqB,EAM/B;IAnBD;;;OAGG;IACH,0BAHU,MAAM,CAGE;IAElB;;;OAGG;IACH,+BAHU,MAAM,CAGQ;IAWxB;;OAEG;IACH,cAFa,eAAe,CAQ3B;CACF;AAED;IAwBE;;;OAGG;IACH,0BAHW,eAAe,oBACf,gBAAgB,EAM1B;IApBD;;;OAGG;IACH,0BAHU,MAAM,CAGE;IAElB;;;OAGG;IACH,+BAHU,MAAM,CAGQ;IAgBxB;;OAEG;IACH,uBAFW,MAAM,EAKhB;IAVD,qBAKW,MAAM,CAHhB;IAcD;;OAEG;IACH,4BAFW,MAAM,EAKhB;IAVD,0BAKW,MAAM,CAHhB;IAUD;;;OAGG;IACH,0BAFa,YAAY,CAcxB;CACF;qCAGsE,yCAAyC;0CArIzG,4CAA4C;qCAqIoB,yCAAyC;2CAEtE,2BAA2B;yBAvI9D,4CAA4C;kCAsIlB,wCAAwC;sCADF,yCAAyC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/forms-model",
3
- "version": "3.0.612",
3
+ "version": "3.0.613",
4
4
  "description": "A hapi plugin providing the model for Defra forms",
5
5
  "homepage": "https://github.com/DEFRA/forms-designer/tree/main/model#readme",
6
6
  "types": "dist/types/index.d.ts",
@@ -5,6 +5,22 @@ import {
5
5
  QuestionComponentElements
6
6
  } from '~/src/form/form-editor/preview/question.js'
7
7
 
8
+ /**
9
+ * Formats a currency amount with thousand separators and two decimal places
10
+ * @param {number} value
11
+ * @param {'en-GB'} [locale] - locale for formatting
12
+ * @param {'GBP'} [currency] - currency code
13
+ * @returns {string} Formatted amount (e.g., "£1,234.56")
14
+ */
15
+ function formatCurrency(value, locale = 'en-GB', currency = 'GBP') {
16
+ const formatter = new Intl.NumberFormat(locale, {
17
+ style: 'currency',
18
+ currency
19
+ })
20
+
21
+ return formatter.format(value)
22
+ }
23
+
8
24
  /**
9
25
  * @implements {PaymentElements}
10
26
  */
@@ -107,8 +123,8 @@ export class PaymentQuestion extends Question {
107
123
  _renderInput() {
108
124
  const amount =
109
125
  typeof this._paymentAmount === 'number'
110
- ? this._paymentAmount.toFixed(2)
111
- : '0.00'
126
+ ? formatCurrency(this._paymentAmount)
127
+ : '£0.00'
112
128
 
113
129
  return {
114
130
  ...super._renderInput(),