@defra/forms-model 3.0.452 → 3.0.454

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 (74) hide show
  1. package/dist/module/form/form-editor/__stubs__/preview.js +67 -1
  2. package/dist/module/form/form-editor/__stubs__/preview.js.map +1 -1
  3. package/dist/module/form/form-editor/index.js.map +1 -1
  4. package/dist/module/form/form-editor/preview/autocomplete.js +69 -0
  5. package/dist/module/form/form-editor/preview/autocomplete.js.map +1 -0
  6. package/dist/module/form/form-editor/preview/checkbox-sortable.js +12 -0
  7. package/dist/module/form/form-editor/preview/checkbox-sortable.js.map +1 -0
  8. package/dist/module/form/form-editor/preview/date-input.js +2 -1
  9. package/dist/module/form/form-editor/preview/date-input.js.map +1 -1
  10. package/dist/module/form/form-editor/preview/email-address.js +1 -1
  11. package/dist/module/form/form-editor/preview/email-address.js.map +1 -1
  12. package/dist/module/form/form-editor/preview/index.js +2 -0
  13. package/dist/module/form/form-editor/preview/index.js.map +1 -1
  14. package/dist/module/form/form-editor/preview/list.js +13 -2
  15. package/dist/module/form/form-editor/preview/list.js.map +1 -1
  16. package/dist/module/form/form-editor/preview/long-answer.js +1 -1
  17. package/dist/module/form/form-editor/preview/long-answer.js.map +1 -1
  18. package/dist/module/form/form-editor/preview/month-year.js +2 -1
  19. package/dist/module/form/form-editor/preview/month-year.js.map +1 -1
  20. package/dist/module/form/form-editor/preview/phone-number.js +1 -1
  21. package/dist/module/form/form-editor/preview/phone-number.js.map +1 -1
  22. package/dist/module/form/form-editor/preview/question.js +10 -2
  23. package/dist/module/form/form-editor/preview/question.js.map +1 -1
  24. package/dist/module/form/form-editor/preview/radio-sortable.js +2 -1
  25. package/dist/module/form/form-editor/preview/radio-sortable.js.map +1 -1
  26. package/dist/module/form/form-editor/preview/radio.js +2 -1
  27. package/dist/module/form/form-editor/preview/radio.js.map +1 -1
  28. package/dist/module/form/form-editor/preview/types.js.map +1 -1
  29. package/dist/module/form/form-editor/preview/uk-address.js +2 -1
  30. package/dist/module/form/form-editor/preview/uk-address.js.map +1 -1
  31. package/dist/module/form/form-editor/preview/yes-no.js +2 -1
  32. package/dist/module/form/form-editor/preview/yes-no.js.map +1 -1
  33. package/dist/module/form/form-editor/types.js.map +1 -1
  34. package/dist/types/form/form-editor/__stubs__/preview.d.ts +30 -0
  35. package/dist/types/form/form-editor/__stubs__/preview.d.ts.map +1 -1
  36. package/dist/types/form/form-editor/index.d.ts +2 -2
  37. package/dist/types/form/form-editor/index.d.ts.map +1 -1
  38. package/dist/types/form/form-editor/preview/autocomplete.d.ts +32 -0
  39. package/dist/types/form/form-editor/preview/autocomplete.d.ts.map +1 -0
  40. package/dist/types/form/form-editor/preview/checkbox-sortable.d.ts +4 -0
  41. package/dist/types/form/form-editor/preview/checkbox-sortable.d.ts.map +1 -0
  42. package/dist/types/form/form-editor/preview/index.d.ts +2 -0
  43. package/dist/types/form/form-editor/preview/list.d.ts +15 -3
  44. package/dist/types/form/form-editor/preview/list.d.ts.map +1 -1
  45. package/dist/types/form/form-editor/preview/month-year.d.ts.map +1 -1
  46. package/dist/types/form/form-editor/preview/question.d.ts +1 -0
  47. package/dist/types/form/form-editor/preview/question.d.ts.map +1 -1
  48. package/dist/types/form/form-editor/preview/radio-sortable.d.ts.map +1 -1
  49. package/dist/types/form/form-editor/preview/radio.d.ts.map +1 -1
  50. package/dist/types/form/form-editor/preview/types.d.ts +6 -1
  51. package/dist/types/form/form-editor/preview/types.d.ts.map +1 -1
  52. package/dist/types/form/form-editor/preview/uk-address.d.ts.map +1 -1
  53. package/dist/types/form/form-editor/preview/yes-no.d.ts.map +1 -1
  54. package/dist/types/form/form-editor/types.d.ts +2 -2
  55. package/dist/types/form/form-editor/types.d.ts.map +1 -1
  56. package/package.json +1 -1
  57. package/src/form/form-editor/__stubs__/preview.js +64 -1
  58. package/src/form/form-editor/index.ts +1 -1
  59. package/src/form/form-editor/preview/autocomplete.js +67 -0
  60. package/src/form/form-editor/preview/checkbox-sortable.js +12 -0
  61. package/src/form/form-editor/preview/date-input.js +2 -2
  62. package/src/form/form-editor/preview/email-address.js +1 -1
  63. package/src/form/form-editor/preview/index.js +2 -0
  64. package/src/form/form-editor/preview/list.js +12 -2
  65. package/src/form/form-editor/preview/long-answer.js +1 -1
  66. package/src/form/form-editor/preview/month-year.js +2 -1
  67. package/src/form/form-editor/preview/phone-number.js +1 -1
  68. package/src/form/form-editor/preview/question.js +10 -2
  69. package/src/form/form-editor/preview/radio-sortable.js +2 -1
  70. package/src/form/form-editor/preview/radio.js +2 -1
  71. package/src/form/form-editor/preview/types.ts +6 -0
  72. package/src/form/form-editor/preview/uk-address.js +2 -1
  73. package/src/form/form-editor/preview/yes-no.js +2 -1
  74. package/src/form/form-editor/types.ts +2 -2
@@ -85,6 +85,26 @@ export class QuestionPreviewElements {
85
85
  setPreviewHTML(_value) {
86
86
  // Not implemented for server side render
87
87
  }
88
+
89
+ /**
90
+ * @param {HTMLElement} _value
91
+ */
92
+ setPreviewDOM(_value) {
93
+ // Not implemented for server side render
94
+ }
95
+ }
96
+
97
+ /**
98
+ * @implements {AutocompleteElements}
99
+ */
100
+ export class AutocompletePreviewElements extends QuestionPreviewElements {
101
+ /**
102
+ * @param {BaseSettings & {autocompleteOptions: string}} elements
103
+ */
104
+ constructor({ autocompleteOptions, ...elements }) {
105
+ super(elements)
106
+ this.autocompleteOptions = autocompleteOptions
107
+ }
88
108
  }
89
109
 
90
110
  export const baseElements = /** @type {BaseSettings} */ ({
@@ -95,7 +115,50 @@ export const baseElements = /** @type {BaseSettings} */ ({
95
115
  shortDesc: ''
96
116
  })
97
117
 
118
+ const list1Id = '414d82a3-4cab-416a-bd54-6b86fbd51120'
119
+ const list2Id = '801385a4-81e6-4171-96c3-6c6727d97f22'
120
+ const list3Id = 'e6e3f621-b875-4ca3-a054-cca9149149dd'
121
+ const list4Id = 'd71b3909-582f-4e90-b6f5-490b89a6eb8f'
122
+
123
+ const listElementsBase = /** @type {BaseSettings} */ ({
124
+ ...baseElements,
125
+ items: [
126
+ {
127
+ label: { text: 'Treasure Hunting' },
128
+ text: 'Treasure Hunting',
129
+ value: 'Treasure Hunting',
130
+ id: list1Id
131
+ },
132
+ {
133
+ label: { text: 'Rescuing the princess' },
134
+ text: 'Rescuing the princess',
135
+ value: 'Rescuing the princess',
136
+ id: list2Id
137
+ },
138
+ {
139
+ label: { text: 'Saving a city' },
140
+ text: 'Saving a city',
141
+ value: 'Saving a city',
142
+ id: list3Id
143
+ },
144
+ {
145
+ label: { text: 'Defeating the baron' },
146
+ text: 'Defeating the baron',
147
+ value: 'Defeating the baron',
148
+ id: list4Id
149
+ }
150
+ ]
151
+ })
152
+
153
+ export const listElementsStub = {
154
+ list1Id,
155
+ list2Id,
156
+ list3Id,
157
+ list4Id,
158
+ baseElements: listElementsBase
159
+ }
160
+
98
161
  /**
99
162
  * @import { ListElement } from '~/src/form/form-editor/types.js'
100
- * @import { BaseSettings, ListElements, RenderContext, QuestionBaseModel, QuestionElements, QuestionRenderer } from '~/src/form/form-editor/preview/types.js'
163
+ * @import { BaseSettings, ListElements, RenderContext, QuestionBaseModel, QuestionElements, QuestionRenderer, AutocompleteElements } from '~/src/form/form-editor/preview/types.js'
101
164
  */
@@ -464,7 +464,7 @@ export const customValidator = Joi.extend((joi: Joi.Root) => {
464
464
  }) as CustomValidator
465
465
 
466
466
  export const autoCompleteOptionsSchema = customValidator
467
- .dsv<{ text: string; value: string }>()
467
+ .dsv<{ text: string; value: string }[]>()
468
468
  .row(/\r?\n/)
469
469
  .col(':')
470
470
  .keys(['text', 'value'])
@@ -0,0 +1,67 @@
1
+ import { autoCompleteOptionsSchema } from '~/src/form/form-editor/index.js'
2
+ import { Question } from '~/src/form/form-editor/preview/question.js'
3
+
4
+ export class AutocompleteQuestion extends Question {
5
+ _questionTemplate = Question.PATH + 'autocompletefield.njk'
6
+ /**
7
+ * @type {string}
8
+ * @protected
9
+ */
10
+ _fieldName = 'autoCompleteField'
11
+ /**
12
+ * @type {ListElement[]}
13
+ * @private
14
+ */
15
+ _autocompleteList = []
16
+
17
+ /**
18
+ * @param {AutocompleteElements} autocompleteElements
19
+ * @param {QuestionRenderer} questionRenderer
20
+ */
21
+ constructor(autocompleteElements, questionRenderer) {
22
+ super(autocompleteElements, questionRenderer)
23
+ this.setAutocompleteList(autocompleteElements.autocompleteOptions)
24
+ }
25
+
26
+ get autoCompleteList() {
27
+ return [{ id: '', value: '', text: '' }, ...this._autocompleteList]
28
+ }
29
+
30
+ /**
31
+ * @param {string} listHTML
32
+ */
33
+ setAutocompleteList(listHTML) {
34
+ const validationResult =
35
+ /** @type {ValidationResult<{text: string, value: string}[]>} */ (
36
+ autoCompleteOptionsSchema.validate(listHTML)
37
+ )
38
+
39
+ if (!validationResult.error) {
40
+ this._autocompleteList = validationResult.value.map(
41
+ ({ text, value }) => ({
42
+ id: text,
43
+ text,
44
+ value
45
+ })
46
+ )
47
+ this.render()
48
+ }
49
+ }
50
+
51
+ get renderInput() {
52
+ return {
53
+ id: this._fieldName,
54
+ name: this._fieldName,
55
+ attributes: { 'data-module': 'govuk-accessible-autocomplete' },
56
+ hint: this.hint,
57
+ label: this.label,
58
+ items: this.autoCompleteList
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * @import { ValidationResult } from 'joi'
65
+ * @import { ListElement } from '~/src/form/form-editor/types.js'
66
+ * @import { AutocompleteElements, QuestionRenderer } from '~/src/form/form-editor/preview/types.js'
67
+ */
@@ -0,0 +1,12 @@
1
+ import { ListSortableQuestion } from '~/src/form/form-editor/preview/list-sortable.js'
2
+ import { Question } from '~/src/form/form-editor/preview/question.js'
3
+
4
+ export class CheckboxSortableQuestion extends ListSortableQuestion {
5
+ /**
6
+ * @type {string}
7
+ * @protected
8
+ */
9
+ _questionTemplate = Question.PATH + 'checkboxesfield.njk'
10
+ listRenderId = 'checkboxField'
11
+ listRenderName = 'checkboxField'
12
+ }
@@ -1,10 +1,10 @@
1
1
  import { FieldsetQuestion } from '~/src/form/form-editor/preview/fieldset-question.js'
2
-
2
+ import { Question } from '~/src/form/form-editor/preview/question.js'
3
3
  export class DateInputQuestion extends FieldsetQuestion {
4
4
  /**
5
5
  * @type {string}
6
6
  * @protected
7
7
  */
8
- _questionTemplate = 'date-input.njk'
8
+ _questionTemplate = Question.PATH + 'date-input.njk'
9
9
  _fieldName = 'dateInput'
10
10
  }
@@ -1,6 +1,6 @@
1
1
  import { Question } from '~/src/form/form-editor/preview/question.js'
2
2
 
3
3
  export class EmailAddressQuestion extends Question {
4
- _questionTemplate = 'emailaddressfield.njk'
4
+ _questionTemplate = Question.PATH + 'emailaddressfield.njk'
5
5
  _fieldName = 'emailAddressField'
6
6
  }
@@ -1,3 +1,5 @@
1
+ export * from '~/src/form/form-editor/preview/autocomplete.js'
2
+ export * from '~/src/form/form-editor/preview/checkbox-sortable.js'
1
3
  export * from '~/src/form/form-editor/preview/date-input.js'
2
4
  export * from '~/src/form/form-editor/preview/email-address.js'
3
5
  export * from '~/src/form/form-editor/preview/list.js'
@@ -3,7 +3,7 @@ import { Question } from '~/src/form/form-editor/preview/question.js'
3
3
  const DefaultListConst = {
4
4
  TextElementId: 'radioText',
5
5
  HintElementId: 'radioHint',
6
- Template: 'radios.njk',
6
+ Template: Question.PATH + 'radios.njk',
7
7
  Input: 'listInput',
8
8
  RenderName: 'listInputField'
9
9
  }
@@ -55,6 +55,16 @@ export class ListQuestion extends Question {
55
55
  this._listElements = listElements
56
56
  }
57
57
 
58
+ /**
59
+ * @returns {{
60
+ * formGroup?: {afterInputs: {html: string}};
61
+ * hint: DefaultComponent;
62
+ * name: string;
63
+ * fieldset?: GovukFieldset;
64
+ * id: string;
65
+ * items: ListItemReadonly[]
66
+ * }}
67
+ */
58
68
  get renderInput() {
59
69
  const afterInputs =
60
70
  /** @type {{ formGroup?: { afterInputs: { html: string } } }} */ (
@@ -198,5 +208,5 @@ export class ListQuestion extends Question {
198
208
 
199
209
  /**
200
210
  * @import { ListElement, ListItemReadonly } from '~/src/form/form-editor/types.js'
201
- * @import { ListenerRow, ListElements, QuestionRenderer, HTMLBuilder } from '~/src/form/form-editor/preview/types.js'
211
+ * @import { ListenerRow, ListElements, QuestionRenderer, HTMLBuilder, DefaultComponent, GovukFieldset } from '~/src/form/form-editor/preview/types.js'
202
212
  */
@@ -5,7 +5,7 @@ export class LongAnswerQuestion extends Question {
5
5
  * @type {string}
6
6
  * @protected
7
7
  */
8
- _questionTemplate = 'textarea.njk'
8
+ _questionTemplate = Question.PATH + 'textarea.njk'
9
9
  /**
10
10
  * @type {string}
11
11
  * @protected
@@ -1,11 +1,12 @@
1
1
  import { FieldsetQuestion } from '~/src/form/form-editor/preview/fieldset-question.js'
2
+ import { Question } from '~/src/index.js'
2
3
 
3
4
  export class MonthYearQuestion extends FieldsetQuestion {
4
5
  /**
5
6
  * @type {string}
6
7
  * @protected
7
8
  */
8
- _questionTemplate = 'monthyearfield.njk'
9
+ _questionTemplate = Question.PATH + 'monthyearfield.njk'
9
10
  _fieldName = 'monthYear'
10
11
 
11
12
  /**
@@ -1,7 +1,7 @@
1
1
  import { Question } from '~/src/form/form-editor/preview/question.js'
2
2
 
3
3
  export class PhoneNumberQuestion extends Question {
4
- _questionTemplate = 'telephonenumberfield.njk'
4
+ _questionTemplate = Question.PATH + 'telephonenumberfield.njk'
5
5
  _fieldName = 'phoneNumberField'
6
6
  }
7
7
 
@@ -9,11 +9,13 @@
9
9
  * server side.
10
10
  */
11
11
  export class Question {
12
+ static PATH = 'preview-components/'
13
+
12
14
  /**
13
15
  * @type {string}
14
16
  * @protected
15
17
  */
16
- _questionTemplate = 'textfield.njk'
18
+ _questionTemplate = Question.PATH + 'textfield.njk'
17
19
  /**
18
20
  * @type { string|null }
19
21
  * @protected
@@ -30,6 +32,12 @@ export class Question {
30
32
  */
31
33
  _questionRenderer
32
34
 
35
+ /**
36
+ * @type {QuestionElements}
37
+ * @private
38
+ */
39
+ _htmlElements
40
+
33
41
  /**
34
42
  * @param {QuestionElements} htmlElements
35
43
  * @param {QuestionRenderer} questionRenderer
@@ -39,7 +47,7 @@ export class Question {
39
47
 
40
48
  /**
41
49
  * @type {QuestionElements}
42
- * @private
50
+ * @protected
43
51
  */
44
52
  this._htmlElements = htmlElements
45
53
  /**
@@ -1,5 +1,6 @@
1
1
  import { ListSortableQuestion } from '~/src/form/form-editor/preview/list-sortable.js'
2
+ import { Question } from '~/src/index.js'
2
3
 
3
4
  export class RadioSortableQuestion extends ListSortableQuestion {
4
- _questionTemplate = 'radios.njk'
5
+ _questionTemplate = Question.PATH + 'radios.njk'
5
6
  }
@@ -1,5 +1,6 @@
1
1
  import { ListQuestion } from '~/src/form/form-editor/preview/list.js'
2
+ import { Question } from '~/src/index.js'
2
3
 
3
4
  export class RadioQuestion extends ListQuestion {
4
- _questionTemplate = 'radios.njk'
5
+ _questionTemplate = Question.PATH + 'radios.njk'
5
6
  }
@@ -1,3 +1,4 @@
1
+ import { type AutocompleteQuestion } from '~/src/form/form-editor/preview/autocomplete.js'
1
2
  import { type DateInputQuestion } from '~/src/form/form-editor/preview/date-input.js'
2
3
  import { type EmailAddressQuestion } from '~/src/form/form-editor/preview/email-address.js'
3
4
  import { type ListSortableQuestion } from '~/src/form/form-editor/preview/list-sortable.js'
@@ -52,6 +53,10 @@ export type ListenerRow = [
52
53
  export interface QuestionElements {
53
54
  readonly values: BaseSettings
54
55
  setPreviewHTML(value: string): void
56
+ setPreviewDOM(element: HTMLElement): void
57
+ }
58
+ export interface AutocompleteElements extends QuestionElements {
59
+ autocompleteOptions: string
55
60
  }
56
61
 
57
62
  export interface RenderContext {
@@ -80,3 +85,4 @@ export type PreviewQuestion =
80
85
  | RadioSortableQuestion
81
86
  | ShortAnswerQuestion
82
87
  | UkAddressQuestion
88
+ | AutocompleteQuestion
@@ -1,7 +1,8 @@
1
1
  import { FieldsetQuestion } from '~/src/form/form-editor/preview/fieldset-question.js'
2
+ import { Question } from '~/src/index.js'
2
3
 
3
4
  export class UkAddressQuestion extends FieldsetQuestion {
4
- _questionTemplate = 'ukaddressfield.njk'
5
+ _questionTemplate = Question.PATH + 'ukaddressfield.njk'
5
6
  _fieldName = 'addressField'
6
7
  }
7
8
 
@@ -1,11 +1,12 @@
1
1
  import { FieldsetQuestion } from '~/src/form/form-editor/preview/fieldset-question.js'
2
+ import { Question } from '~/src/index.js'
2
3
 
3
4
  export class YesNoQuestion extends FieldsetQuestion {
4
5
  /**
5
6
  * @type {string}
6
7
  * @protected
7
8
  */
8
- _questionTemplate = 'radios.njk'
9
+ _questionTemplate = Question.PATH + 'radios.njk'
9
10
  _fieldName = 'yesNo'
10
11
 
11
12
  /**
@@ -350,7 +350,7 @@ export interface ListElement extends ListItem {
350
350
  readonly id: string
351
351
  text: string
352
352
  value: string | boolean
353
- label: ListLabel
353
+ label?: ListLabel
354
354
  }
355
355
 
356
356
  export interface ReadonlyHint {
@@ -361,7 +361,7 @@ export interface ListItemReadonly extends ListElement {
361
361
  readonly text: string
362
362
  readonly hint?: ReadonlyHint
363
363
  readonly value: string | boolean
364
- readonly label: ListLabel
364
+ readonly label?: ListLabel
365
365
  }
366
366
 
367
367
  export interface DateItem {