@commercetools-frontend/ui-kit 15.13.2 → 15.14.1

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.
@@ -0,0 +1,270 @@
1
+ /* eslint-disable jsx-a11y/anchor-is-valid */
2
+ /* eslint-disable jsx-a11y/anchor-has-content */
3
+ import styled from '@emotion/styled';
4
+ import PropTypes from 'prop-types';
5
+ import upperFirst from 'lodash/upperFirst';
6
+ import deprecatedTokens from '../deprecated-tokens';
7
+
8
+ const getIsDeprecated = (token) => deprecatedTokens.includes(token);
9
+
10
+ const filterGroupItemsValues = (groupItems, searchText) =>
11
+ Object.entries(groupItems).filter(
12
+ ([key, value]) =>
13
+ key.toLowerCase().includes(searchText.toLowerCase()) ||
14
+ value?.toLowerCase?.().includes(searchText.toLowerCase()) ||
15
+ value?.description?.toLowerCase().includes(searchText.toLowerCase())
16
+ );
17
+
18
+ const Table = styled.table`
19
+ border: 1px solid #ccc;
20
+ border-collapse: collapse;
21
+ & tr td {
22
+ border: 1px solid #ccc;
23
+ padding: 15px;
24
+ text-align: left;
25
+ }
26
+ & thead td {
27
+ background-color: gray;
28
+ color: white;
29
+ font-weight: bold;
30
+ }
31
+ `;
32
+
33
+ const GroupStyle = styled.div`
34
+ padding: 10px;
35
+ display: ${(props) => (props.isVisible ? 'block' : 'none')};
36
+ `;
37
+
38
+ export const Token = styled.p`
39
+ font-family: monospace;
40
+ `;
41
+
42
+ const DeprecationBadge = () => <b style={{ color: 'orange' }}>DEPRECATED</b>;
43
+ DeprecationBadge.displayName = 'DeprecationBadge';
44
+
45
+ export const TokenBodyCell = (props) => (
46
+ <>
47
+ <Token>{props.tokenName}</Token>
48
+ {getIsDeprecated(props.tokenName) && <DeprecationBadge />}
49
+ </>
50
+ );
51
+ TokenBodyCell.propTypes = {
52
+ tokenName: PropTypes.string.isRequired,
53
+ };
54
+
55
+ export const TokenGroupLinks = (props) => {
56
+ return (
57
+ <>
58
+ <a
59
+ href={`#${props.id}`}
60
+ onClick={(event) => {
61
+ event.preventDefault();
62
+ window.scrollTo({
63
+ top: document.getElementById(props.id).offsetTop,
64
+ behavior: 'smooth',
65
+ });
66
+ }}
67
+ >
68
+ {props.children}
69
+ </a>
70
+ {Boolean(props.config) && (
71
+ <ul>
72
+ {Object.entries(props.config).map(
73
+ ([key, configGroup]) =>
74
+ filterGroupItemsValues(
75
+ configGroup.choices || configGroup.decisions,
76
+ props.filterText
77
+ ).length > 0 && (
78
+ <li key={key}>
79
+ <a
80
+ href={`#${props.id}-${configGroup.prefix}`}
81
+ onClick={(event) => {
82
+ event.preventDefault();
83
+ window.scrollTo({
84
+ top: document.getElementById(
85
+ `${props.id}-${configGroup.prefix}`
86
+ ).offsetTop,
87
+ behavior: 'smooth',
88
+ });
89
+ }}
90
+ >
91
+ {configGroup.label}
92
+ </a>
93
+ </li>
94
+ )
95
+ )}
96
+ </ul>
97
+ )}
98
+ </>
99
+ );
100
+ };
101
+ TokenGroupLinks.propTypes = {
102
+ id: PropTypes.string.isRequired,
103
+ config: PropTypes.object,
104
+ filterText: PropTypes.string,
105
+ children: PropTypes.node.isRequired,
106
+ };
107
+
108
+ const TableBody = (props) => {
109
+ return (
110
+ <tbody>
111
+ {props.groupItems.map(([tokenName, tokenData]) => {
112
+ return (
113
+ <tr key={`${props.groupItemsPrefix}-${tokenName}-body-row-key`}>
114
+ {props.columnsConfig.map((columnConfig) => (
115
+ <td
116
+ key={`${props.groupItemsPrefix}-${tokenName}-${columnConfig.key}-body-cell-key`}
117
+ >
118
+ {props.cellRenderer({
119
+ columnKey: columnConfig.key,
120
+ groupItemsPrefix: props.groupItemsPrefix,
121
+ tokenName,
122
+ tokenData,
123
+ themeName: props.themeName,
124
+ })}
125
+ </td>
126
+ ))}
127
+ </tr>
128
+ );
129
+ })}
130
+ </tbody>
131
+ );
132
+ };
133
+ TableBody.propTypes = {
134
+ groupItems: PropTypes.array.isRequired,
135
+ groupItemsPrefix: PropTypes.string,
136
+ themeName: PropTypes.string.isRequired,
137
+ columnsConfig: PropTypes.arrayOf(
138
+ PropTypes.shape({
139
+ key: PropTypes.string.isRequired,
140
+ label: PropTypes.string,
141
+ })
142
+ ).isRequired,
143
+ cellRenderer: PropTypes.func.isRequired,
144
+ };
145
+
146
+ const TokensDetailsTable = (props) => (
147
+ <Table>
148
+ <thead>
149
+ <tr>
150
+ {props.columnsConfig.map((columnsConfig, index) => (
151
+ <td
152
+ key={columnsConfig.key}
153
+ style={{
154
+ minWidth: index === 0 ? 400 : 'auto',
155
+ }}
156
+ >
157
+ {columnsConfig.label || upperFirst(columnsConfig.key)}
158
+ </td>
159
+ ))}
160
+ </tr>
161
+ </thead>
162
+ <TableBody
163
+ groupItems={props.tokensGroupData}
164
+ groupItemsPrefix={props.tokensGroupPrefix}
165
+ themeName={props.themeName}
166
+ columnsConfig={props.columnsConfig}
167
+ cellRenderer={props.cellRenderer}
168
+ />
169
+ </Table>
170
+ );
171
+ TokensDetailsTable.propTypes = {
172
+ columnsConfig: PropTypes.arrayOf(
173
+ PropTypes.shape({
174
+ key: PropTypes.string.isRequired,
175
+ label: PropTypes.string,
176
+ })
177
+ ).isRequired,
178
+ cellRenderer: PropTypes.func.isRequired,
179
+ themeName: PropTypes.string.isRequired,
180
+ tokensGroupPrefix: PropTypes.string,
181
+ tokensGroupData: PropTypes.array.isRequired,
182
+ };
183
+
184
+ export function SingleTokensGroupDetails(props) {
185
+ const filteredGroupItems = filterGroupItemsValues(
186
+ props.groupItems,
187
+ props.filterText
188
+ );
189
+
190
+ return (
191
+ <section>
192
+ <h2 id={props.id}>{props.title || upperFirst(props.id)}</h2>
193
+ {props.subtitle && <p>{props.subtitle}</p>}
194
+ <GroupStyle isVisible={filteredGroupItems.length > 0}>
195
+ <TokensDetailsTable
196
+ columnsConfig={props.columnsConfig}
197
+ cellRenderer={props.cellRenderer}
198
+ themeName={props.themeName}
199
+ tokensGroupPrefix=""
200
+ tokensGroupData={filteredGroupItems}
201
+ />
202
+ </GroupStyle>
203
+ </section>
204
+ );
205
+ }
206
+ SingleTokensGroupDetails.propTypes = {
207
+ id: PropTypes.string.isRequired,
208
+ title: PropTypes.string,
209
+ subtitle: PropTypes.string,
210
+ filterText: PropTypes.string.isRequired,
211
+ groupItems: PropTypes.object.isRequired,
212
+ columnsConfig: PropTypes.arrayOf(
213
+ PropTypes.shape({
214
+ key: PropTypes.string.isRequired,
215
+ label: PropTypes.string,
216
+ })
217
+ ).isRequired,
218
+ cellRenderer: PropTypes.func.isRequired,
219
+ themeName: PropTypes.string.isRequired,
220
+ };
221
+
222
+ export const MultiTokensGroupDetails = (props) => (
223
+ <section>
224
+ <h2 id={props.id}>{props.title || upperFirst(props.id)}</h2>
225
+ {props.subtitle && <p>{props.subtitle}</p>}
226
+ {Object.entries(props.tokensGroupData).map(
227
+ ([tokenGroupKey, tokenGroupConfig]) => {
228
+ const filteredTokensGroups = filterGroupItemsValues(
229
+ tokenGroupConfig[props.id],
230
+ props.filterText
231
+ );
232
+
233
+ return (
234
+ <GroupStyle
235
+ key={`tokens-group_${tokenGroupKey}`}
236
+ isVisible={filteredTokensGroups.length > 0}
237
+ >
238
+ <a id={`${props.id}-${tokenGroupConfig.prefix}`} />
239
+ <h3>{tokenGroupConfig.label}</h3>
240
+ {Boolean(tokenGroupConfig.description) && (
241
+ <p>{tokenGroupConfig.description}</p>
242
+ )}
243
+ <TokensDetailsTable
244
+ columnsConfig={props.columnsConfig}
245
+ cellRenderer={props.cellRenderer}
246
+ themeName={props.themeName}
247
+ tokensGroupPrefix={tokenGroupConfig.prefix}
248
+ tokensGroupData={filteredTokensGroups}
249
+ />
250
+ </GroupStyle>
251
+ );
252
+ }
253
+ )}
254
+ </section>
255
+ );
256
+ MultiTokensGroupDetails.propTypes = {
257
+ id: PropTypes.string.isRequired,
258
+ title: PropTypes.string,
259
+ subtitle: PropTypes.string,
260
+ filterText: PropTypes.string.isRequired,
261
+ tokensGroupData: PropTypes.object.isRequired,
262
+ columnsConfig: PropTypes.arrayOf(
263
+ PropTypes.shape({
264
+ key: PropTypes.string.isRequired,
265
+ label: PropTypes.string,
266
+ })
267
+ ).isRequired,
268
+ cellRenderer: PropTypes.func.isRequired,
269
+ themeName: PropTypes.string.isRequired,
270
+ };