Skip to content

Commit

Permalink
feat(PRL-6536): Add changes to core framework for date field translat…
Browse files Browse the repository at this point in the history
…ions (#1699)
  • Loading branch information
vivek-sekhar authored Oct 29, 2024
1 parent 04652a0 commit 7a2b507
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
92 changes: 51 additions & 41 deletions src/main/modules/nunjucks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,57 @@ export class Nunjucks {
});

env.addGlobal('formItems', function (items: FormInput[], userAnswer: string | Record<string, string>) {
return items.map(i => ({
id: i.id,
text: this.env.globals.getContent.call(this, i.label),
name: i.name,
classes: i.classes,
disabled: i.disabled,
value: i.value ?? userAnswer?.[i.name as string] ?? (userAnswer as string),
attributes: i.attributes,
checked: i.selected ?? userAnswer?.[i.name as string]?.includes(i.value as string) ?? i.value === userAnswer,
hint: i.hint && {
html: this.env.globals.getContent.call(this, i.hint),
},
//divider: this.env.globals.getContent.call(this, i.divider),
divider:
typeof i.divider === 'function' ? this.env.globals.getContent.call(this, i.divider) : i.divider && 'or',
behaviour: (i.exclusive && 'exclusive') || this.env.globals.getContent.call(this, i.behaviour),
open: i.open,
conditional: (() => {
if (i.warning) {
return {
html: env.render(`${__dirname}/../../steps/common/error/warning.njk`, {
message: this.env.globals.getContent.call(this, i.warning),
warning: this.ctx.warning,
}),
};
} else if (i.conditionalText) {
return {
html: this.env.globals.getContent.call(this, i.conditionalText),
};
} else if (i.subFields) {
return {
html: env.render(`${__dirname}/../../steps/common/form/fields.njk`, {
...this.ctx,
form: { fields: i.subFields },
}),
};
} else {
return undefined;
}
})(),
}));
return items.map(i => {
const itemConfig = {
id: i.id,
text: this.env.globals.getContent.call(this, i.label),
name: i.name,
classes: i.classes,
disabled: i.disabled,
value: i.value ?? userAnswer?.[i.name as string] ?? (userAnswer as string),
attributes: i.attributes,
checked: i.selected ?? userAnswer?.[i.name as string]?.includes(i.value as string) ?? i.value === userAnswer,
hint: i.hint && {
html: this.env.globals.getContent.call(this, i.hint),
},
//divider: this.env.globals.getContent.call(this, i.divider),
divider:
typeof i.divider === 'function' ? this.env.globals.getContent.call(this, i.divider) : i.divider && 'or',
behaviour: (i.exclusive && 'exclusive') || this.env.globals.getContent.call(this, i.behaviour),
open: i.open,
conditional: (() => {
if (i.warning) {
return {
html: env.render(`${__dirname}/../../steps/common/error/warning.njk`, {
message: this.env.globals.getContent.call(this, i.warning),
warning: this.ctx.warning,
}),
};
} else if (i.conditionalText) {
return {
html: this.env.globals.getContent.call(this, i.conditionalText),
};
} else if (i.subFields) {
return {
html: env.render(`${__dirname}/../../steps/common/form/fields.njk`, {
...this.ctx,
form: { fields: i.subFields },
}),
};
} else {
return undefined;
}
})(),
};

if (i.name && ['day', 'month', 'year'].includes(i.name)) {
Object.assign(itemConfig, {
label: this.env.globals.getContent.call(this, i.label),
});
}

return itemConfig;
});
});

env.addGlobal('summaryDetailsHtml', function (subFields: FormInput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,14 @@ export const generateFormFields = (personalDetails: C100Applicant['personalDetai
attributes: { maxLength: 2, pattern: '[0-9]*', inputMode: 'numeric' },
},
{
//label: l => l.dateFormat['month'],
label: l => l.month,
label: l => l.dateFormat['month'],
name: 'month',
value: dateOfBirth!.month,
classes: 'govuk-input--width-2',
attributes: { maxLength: 2, pattern: '[0-9]*', inputMode: 'numeric' },
},
{
//label: l => l.dateFormat['year'],
label: l => l.year,
label: l => l.dateFormat['year'],
name: 'year',
value: dateOfBirth!.year,
classes: 'govuk-input--width-4',
Expand Down

0 comments on commit 7a2b507

Please sign in to comment.