Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/server/plugins/engine/components/ComponentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class ComponentBase {

isFormComponent = false
model: FormModel
def: ComponentDef

/** joi schemas based on a component defined in the form JSON. This validates a user's answer and is generated from {@link ComponentDef} */
formSchema: ComponentSchema = joi.string()
Expand All @@ -45,6 +46,7 @@ export class ComponentBase {
this.type = def.type
this.name = def.name
this.title = def.title
this.def = def

if ('schema' in def) {
this.schema = def.schema
Expand Down
9 changes: 4 additions & 5 deletions src/server/plugins/engine/components/ComponentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,11 @@ export class ComponentCollection {
labelOverrides[subField.name] = patchedSchema
}
} else {
const fieldDef = field.def
const translatedLabel =
translator.tComponent(
field as unknown as ComponentDef,
'shortDescription'
) ||
translator.tComponent(field as unknown as ComponentDef, 'title')
translator.tComponent(fieldDef, 'errorDescription') ||
translator.tComponent(fieldDef, 'shortDescription') ||
translator.tComponent(fieldDef, 'title')
const messagesOverride =
field.getValidationMessagesOverride(translator)
let patchedSchema = field.formSchema
Expand Down
3 changes: 1 addition & 2 deletions src/server/plugins/engine/components/DeclarationField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ComponentType,
hasFormComponents,
isFormType,
type ComponentDef,
type DeclarationFieldComponent,
type Item
} from '@defra/forms-model'
Expand Down Expand Up @@ -148,7 +147,7 @@ export class DeclarationField extends FormComponent {
'components.declarationField.defaultLabel'
)
} = this
const content = tComponent(this as unknown as ComponentDef, 'content')
const content = tComponent(this.def, 'content')

const viewModel = super.getViewModel(context)
let { fieldset, label } = viewModel
Expand Down
6 changes: 2 additions & 4 deletions src/server/plugins/engine/components/FormComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
type ComponentDef,
type FormComponentsDef,
type FormMetadata,
type Item
Expand Down Expand Up @@ -178,15 +177,14 @@ export class FormComponent extends ComponentBase {
const isRequired = !('required' in options) || options.required !== false
const hideOptional = 'optionalText' in options && options.optionalText

const resolvedTitle =
tComponent(this as unknown as ComponentDef, 'title') || title
const resolvedTitle = tComponent(this.def, 'title') || title
const optionalTag =
!isRequired && !hideOptional ? ` ${t('common.optional')}` : ''
const label = `${resolvedTitle}${optionalTag}`

if (hint) {
viewModel.hint = {
text: tComponent(this as unknown as ComponentDef, 'hint') || hint
text: tComponent(this.def, 'hint') || hint
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/server/plugins/engine/components/Markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ComponentDef, type MarkdownComponent } from '@defra/forms-model'
import { type MarkdownComponent } from '@defra/forms-model'

import { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'
import { type RenderContext } from '~/src/server/plugins/engine/components/types.js'
Expand Down Expand Up @@ -28,8 +28,7 @@ export class Markdown extends ComponentBase {

return {
...viewModel,
content:
tComponent(this as unknown as ComponentDef, 'content') || content,
content: tComponent(this.def, 'content') || content,
headerStartLevel: this.headerStartLevel
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/plugins/engine/components/TextField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('TextField', () => {
language: 'en-GB'
}
})
expect(tComponent).toHaveBeenCalledWith(field, 'title')
expect(tComponent).toHaveBeenCalledWith(field.def, 'title')
expect(viewModel.label.text).toBe('Translated title')
})

Expand All @@ -295,7 +295,7 @@ describe('TextField', () => {
language: 'en-GB'
}
})
expect(tComponent).toHaveBeenCalledWith(hintField, 'hint')
expect(tComponent).toHaveBeenCalledWith(hintField.def, 'hint')
expect(viewModel.hint?.text).toBe('Translated hint')
})

Expand Down
1 change: 1 addition & 0 deletions src/server/plugins/engine/i18n/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type FormDefinitionTranslations = Record<
hint: string
content: string
shortDescription: string
errorDescription: string
paymentDescription: string
}>
>
Expand Down
Loading