forked from UsmanJ/govuk-react-jsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessExampleData.js
158 lines (138 loc) · 4.52 KB
/
processExampleData.js
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 deepIterator from 'deep-iterator';
import ReactHtmlParser from 'react-html-parser';
const propReplacements = {
classes: 'className',
describedBy: 'aria-describedby',
containerClasses: 'containerClassName',
navigationClasses: 'navigationClassName',
autocomplete: 'autoComplete',
for: 'htmlFor',
captionClasses: 'captionClassName',
colspan: 'colSpan',
rowspan: 'rowSpan',
summaryText: 'summaryChildren',
summaryHtml: 'summaryChildren',
descriptionText: 'descriptionChildren',
descriptionHtml: 'descriptionChildren',
titleText: 'titleChildren',
titleHtml: 'titleChildren',
inputmode: 'inputMode',
serviceUrl: 'serviceUrlHref',
homepageUrl: 'homepageUrlHref',
spellcheck: 'spellCheck',
tabindex: 'tabIndex',
ariaLabel: 'aria-label',
headingText: 'headingChildren',
};
export default function processExampleData(data, componentName) {
for (const { parent, value, key } of deepIterator(data)) {
// Replace html and text props with children
// Turn any html strings into jsx
if (key && key.toString().toLowerCase().slice(-4) === 'html' && value) {
let replacementKey;
if (key === 'html') {
replacementKey = 'children';
} else {
// Replacements like headingHtml becomes headingChildren
replacementKey = key.replace('Html', 'Children');
}
parent[replacementKey] = ReactHtmlParser(value);
delete parent[key];
}
if (key === 'text' && value) {
parent.children = value;
delete parent[key];
}
// Various replacements of govuk specific params to react syntax. E.g. classes -> className
if (Object.keys(propReplacements).includes(key)) {
parent[propReplacements[key]] = value;
delete parent[key];
}
// Spread attributes out into the object above them in a more React-like fashion
if (key === 'attributes') {
Object.keys(value).forEach((attributeName) => {
if (Object.keys(propReplacements).includes(attributeName)) {
parent[propReplacements[attributeName]] =
value[attributeName].toString();
} else {
parent[attributeName] = value[attributeName].toString();
}
});
delete parent.attributes;
}
if (componentName === 'table') {
if (key === 'rows' && !('cells' in parent[key][0])) {
parent[key] = parent[key].map((row) => ({
cells: row,
}));
}
}
}
if (componentName === 'radios') {
for (const { parent, value, key } of deepIterator(data)) {
// Replace 'checked' value on radio items with a top level 'value' prop for compatibility with react form libraries
if (key === 'items') {
// Work out which one is checked
const checked = value
.filter((item) => item)
.find((item) => item.checked);
// Remove the checked value from each item
parent.items = value.map((item) => {
if (item && Object.keys(item).length > 0) {
const modifiedItem = { ...item };
delete modifiedItem.checked;
return modifiedItem;
}
return item;
});
if (checked) {
parent.value = checked.value;
}
}
}
}
if (componentName === 'select') {
for (const { parent, value, key } of deepIterator(data)) {
// Replace 'selected' value on select box items with a top level 'value' prop for compatibility with react
if (key === 'items') {
// Work out which one is checked
const selected = value.find((item) => (item ? item.selected : null));
// Remove the selected value from each item
parent.items = value.map((item) => {
if (item && Object.keys(item).length > 0) {
const modifiedItem = { ...item };
delete modifiedItem.selected;
return modifiedItem;
}
return item;
});
if (selected) {
parent.value = selected.value;
}
}
}
}
if (componentName === 'cookie-banner') {
for (const { parent, value, key } of deepIterator(data)) {
if (key === 'messages') {
if (value.length > 0) {
parent[key] = value.map((item, index) => ({
...item,
key: index,
}));
}
}
}
for (const { parent, value, key } of deepIterator(data)) {
if (key === 'actions') {
if (value.length > 0) {
parent[key] = value.map((item, index) => ({
...item,
key: index,
}));
}
}
}
}
return data;
}