Skip to content

Commit f54c402

Browse files
colinrotherhamlhokktyn
authored andcommitted
refactor: allow component inputs to be more configurable
Alter the date-input and postal-address-object template macros to allow the addition and overriding of certain macro parameters. Signed-off-by: Colin Rotherham <work@colinr.com>
1 parent 14ed92b commit f54c402

8 files changed

Lines changed: 505 additions & 50 deletions

File tree

app/views/casa/components/date-input/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@ casaGovukDateInput({
2323
})
2424
```
2525

26+
With configurable items:
27+
28+
```nunjucks
29+
{% from "casa/components/date-input/macro.njk" import casaGovukDateInput %}
30+
31+
casaGovukDateInput({
32+
namePrefix: "dateOfBirth",
33+
casaValue: formData.dateOfBirth,
34+
casaErrors: formErrors,
35+
items: [
36+
{
37+
autocomplete: "bday-day",
38+
attributes: {
39+
min: "1",
40+
max: "31"
41+
}
42+
},
43+
{
44+
autocomplete: "bday-month",
45+
attributes: {
46+
min: "1",
47+
max: "12"
48+
}
49+
},
50+
{
51+
autocomplete: "bday-year",
52+
attributes: {
53+
min: "1",
54+
max: "9999"
55+
}
56+
}
57+
]
58+
})
59+
```
60+
2661
## Companion validation
2762

2863
There is a companion validation rule - [`dateObject`](../../../../../docs/field-validation-rules.md#dateObject) - that you can use to ensure the dates are passed in correctly.

app/views/casa/components/date-input/template.njk

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,32 @@
2020
id: params.id if params.id else 'f-' + params.namePrefix,
2121
namePrefix: '',
2222
attributes: mergedAttributes,
23-
items: [{
24-
label: t('macros:dateInput.day'),
25-
name: params.namePrefix + '[dd]',
26-
id: 'f-' + params.namePrefix + '[dd]',
27-
value: params.casaValue.dd,
28-
classes: 'govuk-input--width-2 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[dd]') or not hasSuffixHighlights)
29-
}, {
30-
label: t('macros:dateInput.month'),
31-
name: params.namePrefix + '[mm]',
32-
id: 'f-' + params.namePrefix + '[mm]',
33-
value: params.casaValue.mm,
34-
classes: 'govuk-input--width-2 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[mm]') or not hasSuffixHighlights),
35-
attributes: attributes_mm
36-
}, {
37-
label: t('macros:dateInput.year'),
38-
name: params.namePrefix + '[yyyy]',
39-
id: 'f-' + params.namePrefix + '[yyyy]',
40-
value: params.casaValue.yyyy,
41-
classes: 'govuk-input--width-4 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[yyyy]') or not hasSuffixHighlights)
42-
}],
23+
items: [
24+
mergeObjectsDeep({
25+
label: t('macros:dateInput.day'),
26+
classes: 'govuk-input--width-2 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[dd]') or not hasSuffixHighlights)
27+
}, params.items[0] if params.items[0] else {}, {
28+
id: 'f-' + params.namePrefix + '[dd]',
29+
name: params.namePrefix + '[dd]',
30+
value: params.casaValue.dd
31+
}),
32+
mergeObjectsDeep({
33+
label: t('macros:dateInput.month'),
34+
classes: 'govuk-input--width-2 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[mm]') or not hasSuffixHighlights)
35+
}, params.items[1] if params.items[1] else {}, {
36+
id: 'f-' + params.namePrefix + '[mm]',
37+
name: params.namePrefix + '[mm]',
38+
value: params.casaValue.mm
39+
}),
40+
mergeObjectsDeep({
41+
label: t('macros:dateInput.year'),
42+
classes: 'govuk-input--width-4 ' + (inputErrorClass if includes(fieldErrors[0].focusSuffix, '[yyyy]') or not hasSuffixHighlights)
43+
}, params.items[2] if params.items[2] else {}, {
44+
id: 'f-' + params.namePrefix + '[yyyy]',
45+
name: params.namePrefix + '[yyyy]',
46+
value: params.casaValue.yyyy
47+
})
48+
],
4349
errorMessage: {
4450
text: t(params.casaErrors[params.namePrefix][0].inline)
4551
} if params.casaErrors[params.namePrefix] else null

app/views/casa/components/postal-address-object/README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
Custom parameters:
44

5-
* `casaValue` - the value of the chosen radio button. This is a convenience for toggling the `checked` flag on the appropriate `item`, but you can also manually set `checked` on each item if you need to use more specific logic for determining checked state.
5+
* `value` - the value of the address object, in `{address1: '', address2: '', address3: '', address4: '', postcode: ''}` format
66
* `casaErrors` - form errors (just pass `formErrors`)
7+
* `address[1-4]` and `postcode` - for specifying extra attributes for each of the address input components
78

89
## Example usage
910

@@ -15,7 +16,34 @@ Basic example:
1516
{{ casaPostalAddressObject({
1617
name: 'address',
1718
value: formData.address,
18-
casaErrors: formErrors,
19+
casaErrors: formErrors
20+
}) }}
21+
```
22+
23+
With configurable items:
24+
25+
```nunjucks
26+
{% from "casa/components/postal-address-object/macro.njk" import casaPostalAddressObject %}
27+
28+
{{ casaPostalAddressObject({
29+
name: 'address',
30+
value: formData.address,
31+
address1: {
32+
autocomplete: 'address-line1'
33+
},
34+
address2: {
35+
autocomplete: 'address-line2'
36+
},
37+
address3: {
38+
autocomplete: 'address-level2'
39+
},
40+
address4: {
41+
autocomplete: 'address-level1'
42+
},
43+
postcode: {
44+
autocomplete: 'postal-code'
45+
},
46+
casaErrors: formErrors
1947
}) }}
2048
```
2149

app/views/casa/components/postal-address-object/template.njk

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,38 @@
1414
{% if fieldAddress1Errors %}
1515
{% set fieldAttributes = mergeObjects(fieldAttributes, {'data-validation': {fn: params.name + '[address1]', va: fieldAddress1Errors[0].validator} | dump}) %}
1616
{% endif %}
17-
{{ govukInput({
17+
{{ govukInput(mergeObjectsDeep({
1818
label: {
1919
html: t('macros:postalAddressObject.address1')
2020
},
21-
id: 'f-' + params.name + '[address1]',
22-
name: params.name + '[address1]',
23-
value: params.value.address1,
2421
attributes: fieldAttributes,
2522
errorMessage: {
2623
text: t(fieldAddress1Errors[0].inline)
2724
} if fieldAddress1Errors else null
28-
}) }}
25+
}, params.address1 if params.address1 else {}, {
26+
id: 'f-' + params.name + '[address1]',
27+
name: params.name + '[address1]',
28+
value: params.value.address1
29+
})) }}
2930

3031
{# Line 2 #}
3132
{% set fieldAttributes = {} %}
3233
{% if fieldAddress2Errors %}
3334
{% set fieldAttributes = mergeObjects(fieldAttributes, {'data-validation': {fn: params.name + '[address2]', va: fieldAddress2Errors[0].validator} | dump}) %}
3435
{% endif %}
35-
{{ govukInput({
36+
{{ govukInput(mergeObjectsDeep({
3637
label: {
3738
html: t('macros:postalAddressObject.address2')
3839
},
39-
id: 'f-' + params.name + '[address2]',
40-
name: params.name + '[address2]',
41-
value: params.value.address2,
4240
attributes: fieldAttributes,
4341
errorMessage: {
4442
text: t(fieldAddress2Errors[0].inline)
4543
} if fieldAddress2Errors else null
46-
}) }}
44+
}, params.address2 if params.address2 else {}, {
45+
id: 'f-' + params.name + '[address2]',
46+
name: params.name + '[address2]',
47+
value: params.value.address2
48+
})) }}
4749
</div>
4850

4951
{# Town #}
@@ -52,57 +54,60 @@
5254
{% if fieldErrors %}
5355
{% set fieldAttributes = mergeObjects(fieldAttributes, {'data-validation': {fn: params.name + '[address3]', va: fieldErrors[0].validator} | dump}) %}
5456
{% endif %}
55-
{{ govukInput({
57+
{{ govukInput(mergeObjectsDeep({
58+
classes: "govuk-!-width-two-thirds",
5659
label: {
5760
html: t('macros:postalAddressObject.address3')
5861
},
59-
id: 'f-' + params.name + '[address3]',
60-
name: params.name + '[address3]',
61-
value: params.value.address3,
62-
classes: "govuk-!-width-two-thirds",
6362
attributes: fieldAttributes,
6463
errorMessage: {
6564
text: t(fieldErrors[0].inline)
6665
} if fieldErrors else null
67-
}) }}
66+
}, params.address3 if params.address3 else {}, {
67+
id: 'f-' + params.name + '[address3]',
68+
name: params.name + '[address3]',
69+
value: params.value.address3
70+
})) }}
6871

6972
{# County #}
7073
{% set fieldErrors = params.casaErrors[params.name+"[address4]"] %}
7174
{% set fieldAttributes = {} %}
7275
{% if fieldErrors %}
7376
{% set fieldAttributes = mergeObjects(fieldAttributes, {'data-validation': {fn: params.name + '[address4]', va: fieldErrors[0].validator} | dump}) %}
7477
{% endif %}
75-
{{ govukInput({
78+
{{ govukInput(mergeObjectsDeep({
79+
classes: "govuk-!-width-two-thirds",
7680
label: {
7781
html: t('macros:postalAddressObject.address4')
7882
},
79-
id: 'f-' + params.name + '[address4]',
80-
name: params.name + '[address4]',
81-
value: params.value.address4,
82-
classes: "govuk-!-width-two-thirds",
8383
attributes: fieldAttributes,
8484
errorMessage: {
8585
text: t(fieldErrors[0].inline)
8686
} if fieldErrors else null
87-
}) }}
87+
}, params.address4 if params.address4 else {}, {
88+
id: 'f-' + params.name + '[address4]',
89+
name: params.name + '[address4]',
90+
value: params.value.address4
91+
})) }}
8892

8993
{# Postcode #}
9094
{% set fieldErrors = params.casaErrors[params.name+"[postcode]"] %}
9195
{% set fieldAttributes = {} %}
9296
{% if fieldErrors %}
9397
{% set fieldAttributes = mergeObjects(fieldAttributes, {'data-validation': {fn: params.name + '[postcode]', va: fieldErrors[0].validator} | dump}) %}
9498
{% endif %}
95-
{{ govukInput({
99+
{{ govukInput(mergeObjectsDeep({
100+
classes: "govuk-input--width-10",
96101
label: {
97102
html: t('macros:postalAddressObject.postcode')
98103
},
99-
id: 'f-' + params.name + '[postcode]',
100-
name: params.name + '[postcode]',
101-
value: params.value.postcode,
102-
classes: "govuk-input--width-10",
103104
attributes: fieldAttributes,
104105
errorMessage: {
105106
text: t(fieldErrors[0].inline)
106107
} if fieldErrors else null
107-
}) }}
108+
}, params.postcode if params.postcode else {}, {
109+
id: 'f-' + params.name + '[postcode]',
110+
name: params.name + '[postcode]',
111+
value: params.value.postcode
112+
})) }}
108113
{% endcall %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% from "casa/components/date-input/macro.njk" import casaGovukDateInput %}
2+
3+
{{ casaGovukDateInput(params) }}

0 commit comments

Comments
 (0)