nhsuk-decorated-components 0.0.2
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.
- package/LICENSE.txt +21 -0
- package/README.md +57 -0
- package/lib/decorate.js +143 -0
- package/package.json +53 -0
- package/x-nhsuk/decorated/button/macro.njk +4 -0
- package/x-nhsuk/decorated/character-count/macro.njk +4 -0
- package/x-nhsuk/decorated/checkboxes/macro.njk +4 -0
- package/x-nhsuk/decorated/date-input/macro.njk +4 -0
- package/x-nhsuk/decorated/file-upload/macro.njk +4 -0
- package/x-nhsuk/decorated/input/macro.njk +4 -0
- package/x-nhsuk/decorated/radios/macro.njk +4 -0
- package/x-nhsuk/decorated/select/macro.njk +4 -0
- package/x-nhsuk/decorated/textarea/macro.njk +4 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 X-GOVUK
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# NHS.UK Decorated Components · [](https://github.com/x-govuk/nhsuk-decorated-components/actions/workflows/test.yml)
|
|
2
|
+
|
|
3
|
+
Form components for the NHS.UK Design System that require less parameters to collect data. [Replace the multiple parameters needed for saving data with a single `decorate` parameter](https://x-govuk.github.io/nhsuk-prototype-rig/using-data/form-components/).
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
Node.js v16 or later.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
> **Note** These components are included by default with the [NHS.UK Prototype Rig](https://x-govuk.github.io/nhsuk-prototype-rig/).
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm install nhsuk-decorated-components --save
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
To add them to the NHS.UK Prototype Kit, follow these steps:
|
|
20
|
+
|
|
21
|
+
1. Add `/node_modules/nhsuk-decorated-components` to your application’s views (`appViews`) in `server.js`:
|
|
22
|
+
|
|
23
|
+
```diff
|
|
24
|
+
// Set up App
|
|
25
|
+
var appViews = extensions.getAppViews([
|
|
26
|
+
+ path.join(projectDir, '/node_modules/nhsuk-decorated-components'),
|
|
27
|
+
path.join(projectDir, '/app/views/'),
|
|
28
|
+
path.join(projectDir, '/lib/')
|
|
29
|
+
])
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
2. Add the `decorate` global function to your Nunjucks environment (`nunjucksAppEnv`) in `server.js`:
|
|
33
|
+
|
|
34
|
+
```diff
|
|
35
|
+
var nunjucksAppEnv = nunjucks.configure(appViews, nunjucksConfig)
|
|
36
|
+
|
|
37
|
+
// Add Nunjucks Globals
|
|
38
|
+
+ const { decorate } = require('nhsuk-decorated-components')
|
|
39
|
+
+ nunjucksAppEnv.addGlobal('decorate', decorate)
|
|
40
|
+
|
|
41
|
+
// Add Nunjucks filters
|
|
42
|
+
utils.addNunjucksFilters(nunjucksAppEnv)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
3. Replace imported NHS.UK Frontend macros with those provided by this package:
|
|
46
|
+
|
|
47
|
+
```diff
|
|
48
|
+
+ {% from "x-nhsuk/decorated/button/macro.njk" import button with context %}
|
|
49
|
+
+ {% from "x-nhsuk/decorated/character-count/macro.njk" import characterCount with context %}
|
|
50
|
+
+ {% from "x-nhsuk/decorated/checkboxes/macro.njk" import checkboxes with context %}
|
|
51
|
+
+ {% from "x-nhsuk/decorated/date-input/macro.njk" import dateInput with context %}
|
|
52
|
+
+ {% from "x-nhsuk/decorated/file-upload/macro.njk" import fileUpload with context %}
|
|
53
|
+
+ {% from "x-nhsuk/decorated/input/macro.njk" import input with context %}
|
|
54
|
+
+ {% from "x-nhsuk/decorated/radios/macro.njk" import radios with context %}
|
|
55
|
+
+ {% from "x-nhsuk/decorated/select/macro.njk" import select with context %}
|
|
56
|
+
+ {% from "x-nhsuk/decorated/textarea/macro.njk" import textarea with context %}
|
|
57
|
+
```
|
package/lib/decorate.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Add `name`, `value`, `id`, `idPrefix` and `checked`/`selected` attributes
|
|
5
|
+
* to NHS.UK form inputs. Generates attributes based on where they are stored
|
|
6
|
+
* in the session data object.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} params - Component parameters
|
|
9
|
+
* @param {string} keyPath - Path to key (using dot/bracket notation)
|
|
10
|
+
* @param {string} [componentName] - Name of component calling decorate
|
|
11
|
+
* @returns {Object} Updated component parameters
|
|
12
|
+
*/
|
|
13
|
+
exports.decorate = function (params, keyPath, componentName) {
|
|
14
|
+
if (typeof keyPath === 'undefined') {
|
|
15
|
+
return params
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
keyPath = _.toPath(keyPath)
|
|
19
|
+
|
|
20
|
+
const { data, errors } = this.ctx
|
|
21
|
+
if (!data) {
|
|
22
|
+
return ''
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Strip data from key path as auto store data middleware adds it
|
|
26
|
+
if (keyPath[0] === 'data') {
|
|
27
|
+
keyPath.shift(1)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const storedValue = _.get(data, keyPath)
|
|
31
|
+
|
|
32
|
+
params.id = (params.id) ? params.id : keyPath.join('-')
|
|
33
|
+
params.name = (params.name) ? params.name : keyPath.map(s => `[${s}]`).join('')
|
|
34
|
+
|
|
35
|
+
// Add field validations to data
|
|
36
|
+
if (params.validate) {
|
|
37
|
+
data.validations = data.validations || {}
|
|
38
|
+
data.validations[params.name] = params.validate
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (componentName === 'dateInput' && !params.items) {
|
|
42
|
+
params.items = [
|
|
43
|
+
{ decorate: 'day' },
|
|
44
|
+
{ decorate: 'month' },
|
|
45
|
+
{ decorate: 'year' }
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (params.items) {
|
|
50
|
+
params.idPrefix = params.id
|
|
51
|
+
params.items = params.items.map(item => {
|
|
52
|
+
// Ignore dividers or empty items
|
|
53
|
+
if (item.divider || item === '') {
|
|
54
|
+
return item
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Validate dates
|
|
58
|
+
// ==============
|
|
59
|
+
// We want to validate a combined date, but validate.js validates
|
|
60
|
+
// individual fields. The custom `date` validator hooks into the year
|
|
61
|
+
// value and gets day and month values via its `attributes` object.
|
|
62
|
+
//
|
|
63
|
+
// 1. Delete `validations` for all date fields
|
|
64
|
+
// 2. Add `validations` for year field
|
|
65
|
+
|
|
66
|
+
if (item.decorate && data.validations) {
|
|
67
|
+
delete data.validations[params.name] // 1.
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Update date input items based on `decorate` value
|
|
71
|
+
switch (item.decorate) {
|
|
72
|
+
case 'day':
|
|
73
|
+
item.classes = item.classes || 'nhsuk-input--width-2'
|
|
74
|
+
item.id = `${params.id}-day`
|
|
75
|
+
item.name = `${params.name}[day]`
|
|
76
|
+
item.label = item.label || 'Day'
|
|
77
|
+
item.value = storedValue?.day
|
|
78
|
+
break
|
|
79
|
+
case 'month':
|
|
80
|
+
item.classes = item.classes || 'nhsuk-input--width-2'
|
|
81
|
+
item.id = `${params.id}-month`
|
|
82
|
+
item.name = `${params.name}[month]`
|
|
83
|
+
item.label = item.label || 'Month'
|
|
84
|
+
item.value = storedValue?.month
|
|
85
|
+
break
|
|
86
|
+
case 'year':
|
|
87
|
+
item.classes = item.classes || 'nhsuk-input--width-4'
|
|
88
|
+
item.id = `${params.id}-year`
|
|
89
|
+
item.name = `${params.name}[year]`
|
|
90
|
+
item.label = item.label || 'Year'
|
|
91
|
+
item.value = storedValue?.year
|
|
92
|
+
|
|
93
|
+
if (params.validate && data.validations) {
|
|
94
|
+
data.validations[item.name] = params.validate // 2.
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
break
|
|
98
|
+
default:
|
|
99
|
+
// Use label text if no value given for option
|
|
100
|
+
if (typeof item.value === 'undefined') {
|
|
101
|
+
item.value = item.text
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let checkedValue = ''
|
|
106
|
+
let selectedValue = ''
|
|
107
|
+
|
|
108
|
+
if (Array.isArray(storedValue)) {
|
|
109
|
+
// Stored value is an array, check it exists in the array
|
|
110
|
+
if (storedValue.indexOf(item.value) !== -1) {
|
|
111
|
+
checkedValue = 'checked'
|
|
112
|
+
selectedValue = 'selected'
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
// Stored value is a simple value, check it matches
|
|
116
|
+
if (storedValue === item.value) {
|
|
117
|
+
checkedValue = 'checked'
|
|
118
|
+
selectedValue = 'selected'
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
item.checked = checkedValue
|
|
123
|
+
item.selected = selectedValue
|
|
124
|
+
|
|
125
|
+
return item
|
|
126
|
+
})
|
|
127
|
+
} else {
|
|
128
|
+
// Check for undefined because value may exist and be intentionally blank
|
|
129
|
+
if (typeof params.value === 'undefined') {
|
|
130
|
+
params.value = storedValue
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Add message if error
|
|
135
|
+
const errorKeyPath = keyPath.join('.')
|
|
136
|
+
if (errors && errors[errorKeyPath]) {
|
|
137
|
+
params.errorMessage = {
|
|
138
|
+
text: errors[errorKeyPath].msg
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return params
|
|
143
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nhsuk-decorated-components",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Form components for the NHS.UK Design System that require less parameters to collect data",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"govuk",
|
|
7
|
+
"design system",
|
|
8
|
+
"prototyping",
|
|
9
|
+
"components"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/x-govuk/nhsuk-decorated-components#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/x-govuk/nhsuk-decorated-components/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"files": [
|
|
17
|
+
"lib",
|
|
18
|
+
"x-nhsuk"
|
|
19
|
+
],
|
|
20
|
+
"main": "lib/decorate.js",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/x-govuk/nhsuk-decorated-components.git"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"lint": "standard",
|
|
27
|
+
"test": "ava",
|
|
28
|
+
"test:watch": "ava --watch",
|
|
29
|
+
"coverage": "c8 ava"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"lodash": "^4.17.21"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"ava": "^4.0.1",
|
|
39
|
+
"c8": "^7.11.0",
|
|
40
|
+
"nhsuk-frontend": "^6.1.2",
|
|
41
|
+
"nunjucks": "^3.2.3",
|
|
42
|
+
"standard": "^17.0.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=16.0.0"
|
|
46
|
+
},
|
|
47
|
+
"c8": {
|
|
48
|
+
"reporter": [
|
|
49
|
+
"text",
|
|
50
|
+
"lcovonly"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|