-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathListFormComponent.ts
More file actions
158 lines (129 loc) · 3.84 KB
/
ListFormComponent.ts
File metadata and controls
158 lines (129 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
type Item,
type List,
type ListComponentsDef,
type SelectionComponentsDef,
type YesNoFieldComponent
} from '@defra/forms-model'
import joi, {
type ArraySchema,
type BooleanSchema,
type NumberSchema,
type StringSchema
} from 'joi'
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import { type ListItem } from '~/src/server/plugins/engine/components/types.js'
import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
import {
type ErrorMessageTemplateList,
type FormPayload,
type FormSubmissionError,
type FormSubmissionState
} from '~/src/server/plugins/engine/types.js'
export class ListFormComponent extends FormComponent {
declare options: Extract<
SelectionComponentsDef,
{ options: object }
>['options']
declare formSchema:
| ArraySchema<string>
| ArraySchema<number>
| BooleanSchema<string>
| NumberSchema<string>
| NumberSchema
| StringSchema
declare stateSchema:
| ArraySchema<string>
| ArraySchema<number>
| BooleanSchema<string>
| NumberSchema<string>
| NumberSchema
| StringSchema
list?: List
listType: List['type'] = 'string'
get items(): Item[] {
return this.list?.items ?? []
}
get values(): Item['value'][] {
return this.items.map(({ value }) => value)
}
constructor(
def:
| SelectionComponentsDef // Allow for Yes/No field custom list
| (YesNoFieldComponent & Pick<ListComponentsDef, 'list'>),
props: ConstructorParameters<typeof FormComponent>[1]
) {
super(def, props)
const { options } = def
const { model } = props
if ('list' in def) {
this.list = model.getList(def.list)
this.listType = this.list?.type ?? 'string'
}
let formSchema = joi[this.listType]()
.valid(...this.values)
.label(this.label)
.required()
if (options.customValidationMessages) {
formSchema = formSchema.messages(options.customValidationMessages)
}
this.formSchema = formSchema
this.stateSchema = formSchema.default(null).allow(null)
this.options = options
}
getFormValueFromState(
state: FormSubmissionState
): Item['value'] | Item['value'][] | undefined {
const { name, items } = this
const value = state[name]
// Allow for array values via subclass
const values = this.isValue(value) ? [value].flat() : []
const selected = items.filter((item) => values.includes(item.value))
return selected.at(0)?.value
}
getDisplayStringFromState(state: FormSubmissionState) {
const { items } = this
// Allow for array values via subclass
const value = this.getFormValueFromState(state)
const values = [value ?? []].flat()
return items
.filter((item) => values.includes(item.value))
.map((item) => item.text)
.join(', ')
}
getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {
const { items: listItems } = this
const viewModel = super.getViewModel(payload, errors)
const { value } = viewModel
// Support multiple values for checkboxes
const values = this.isValue(value) ? [value].flat() : []
const items = listItems.map((item) => {
const selected = values.includes(item.value)
const itemModel: ListItem = { ...item, selected }
if ('id' in itemModel) {
delete itemModel.id
}
if (item.description) {
itemModel.hint = {
text: item.description
}
}
return itemModel
})
return {
...viewModel,
items
}
}
/**
* For error preview page that shows all possible errors on a component
*/
getAllPossibleErrors(): ErrorMessageTemplateList {
return {
baseErrors: [
{ type: 'selectRequired', template: messageTemplate.selectRequired }
],
advancedSettingsErrors: []
}
}
}