@pie-lib/config-ui 13.0.4-next.31 → 13.0.4-next.34

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.
Files changed (190) hide show
  1. package/CHANGELOG.json +32 -0
  2. package/CHANGELOG.md +2419 -0
  3. package/LICENSE.md +5 -0
  4. package/lib/alert-dialog.js +68 -0
  5. package/lib/alert-dialog.js.map +1 -0
  6. package/lib/checkbox.js +84 -0
  7. package/lib/checkbox.js.map +1 -0
  8. package/lib/choice-configuration/feedback-menu.js +129 -0
  9. package/lib/choice-configuration/feedback-menu.js.map +1 -0
  10. package/lib/choice-configuration/index.js +381 -0
  11. package/lib/choice-configuration/index.js.map +1 -0
  12. package/lib/choice-utils.js +42 -0
  13. package/lib/choice-utils.js.map +1 -0
  14. package/lib/feedback-config/feedback-selector.js +155 -0
  15. package/lib/feedback-config/feedback-selector.js.map +1 -0
  16. package/lib/feedback-config/group.js +61 -0
  17. package/lib/feedback-config/group.js.map +1 -0
  18. package/lib/feedback-config/index.js +146 -0
  19. package/lib/feedback-config/index.js.map +1 -0
  20. package/lib/form-section.js +44 -0
  21. package/lib/form-section.js.map +1 -0
  22. package/lib/help.js +106 -0
  23. package/lib/help.js.map +1 -0
  24. package/lib/index.js +186 -0
  25. package/lib/index.js.map +1 -0
  26. package/lib/input.js +106 -0
  27. package/lib/input.js.map +1 -0
  28. package/lib/inputs.js +105 -0
  29. package/lib/inputs.js.map +1 -0
  30. package/lib/langs.js +136 -0
  31. package/lib/langs.js.map +1 -0
  32. package/lib/layout/config-layout.js +137 -0
  33. package/lib/layout/config-layout.js.map +1 -0
  34. package/lib/layout/index.js +21 -0
  35. package/lib/layout/index.js.map +1 -0
  36. package/lib/layout/layout-contents.js +160 -0
  37. package/lib/layout/layout-contents.js.map +1 -0
  38. package/lib/layout/settings-box.js +57 -0
  39. package/lib/layout/settings-box.js.map +1 -0
  40. package/lib/mui-box/index.js +63 -0
  41. package/lib/mui-box/index.js.map +1 -0
  42. package/lib/number-text-field-custom.js +376 -0
  43. package/lib/number-text-field-custom.js.map +1 -0
  44. package/lib/number-text-field.js +229 -0
  45. package/lib/number-text-field.js.map +1 -0
  46. package/lib/radio-with-label.js +48 -0
  47. package/lib/radio-with-label.js.map +1 -0
  48. package/lib/settings/display-size.js +61 -0
  49. package/lib/settings/display-size.js.map +1 -0
  50. package/lib/settings/index.js +110 -0
  51. package/lib/settings/index.js.map +1 -0
  52. package/lib/settings/panel.js +392 -0
  53. package/lib/settings/panel.js.map +1 -0
  54. package/lib/settings/settings-radio-label.js +51 -0
  55. package/lib/settings/settings-radio-label.js.map +1 -0
  56. package/lib/settings/toggle.js +63 -0
  57. package/lib/settings/toggle.js.map +1 -0
  58. package/lib/tabs/index.js +75 -0
  59. package/lib/tabs/index.js.map +1 -0
  60. package/lib/tags-input/index.js +149 -0
  61. package/lib/tags-input/index.js.map +1 -0
  62. package/lib/two-choice.js +136 -0
  63. package/lib/two-choice.js.map +1 -0
  64. package/lib/with-stateful-model.js +61 -0
  65. package/lib/with-stateful-model.js.map +1 -0
  66. package/package.json +19 -33
  67. package/src/__tests__/alert-dialog.test.jsx +183 -0
  68. package/src/__tests__/checkbox.test.jsx +152 -0
  69. package/src/__tests__/choice-utils.test.js +12 -0
  70. package/src/__tests__/form-section.test.jsx +328 -0
  71. package/src/__tests__/help.test.jsx +184 -0
  72. package/src/__tests__/input.test.jsx +156 -0
  73. package/src/__tests__/langs.test.jsx +376 -0
  74. package/src/__tests__/number-text-field-custom.test.jsx +255 -0
  75. package/src/__tests__/number-text-field.test.jsx +263 -0
  76. package/src/__tests__/radio-with-label.test.jsx +155 -0
  77. package/src/__tests__/settings-panel.test.js +187 -0
  78. package/src/__tests__/settings.test.jsx +452 -0
  79. package/src/__tests__/tabs.test.jsx +188 -0
  80. package/src/__tests__/two-choice.test.js +110 -0
  81. package/src/__tests__/with-stateful-model.test.jsx +139 -0
  82. package/src/alert-dialog.jsx +75 -0
  83. package/src/checkbox.jsx +61 -0
  84. package/src/choice-configuration/__tests__/feedback-menu.test.jsx +151 -0
  85. package/src/choice-configuration/__tests__/index.test.jsx +234 -0
  86. package/src/choice-configuration/feedback-menu.jsx +96 -0
  87. package/src/choice-configuration/index.jsx +357 -0
  88. package/src/choice-utils.js +30 -0
  89. package/src/feedback-config/__tests__/feedback-config.test.jsx +141 -0
  90. package/src/feedback-config/__tests__/feedback-selector.test.jsx +97 -0
  91. package/src/feedback-config/feedback-selector.jsx +112 -0
  92. package/src/feedback-config/group.jsx +51 -0
  93. package/src/feedback-config/index.jsx +111 -0
  94. package/src/form-section.jsx +31 -0
  95. package/src/help.jsx +79 -0
  96. package/src/index.js +55 -0
  97. package/src/input.jsx +72 -0
  98. package/src/inputs.jsx +69 -0
  99. package/src/langs.jsx +111 -0
  100. package/src/layout/__tests__/config.layout.test.jsx +59 -0
  101. package/src/layout/__tests__/layout-content.test.jsx +3 -0
  102. package/src/layout/config-layout.jsx +103 -0
  103. package/src/layout/index.js +4 -0
  104. package/src/layout/layout-contents.jsx +117 -0
  105. package/src/layout/settings-box.jsx +32 -0
  106. package/src/mui-box/index.jsx +56 -0
  107. package/src/number-text-field-custom.jsx +333 -0
  108. package/src/number-text-field.jsx +215 -0
  109. package/src/radio-with-label.jsx +30 -0
  110. package/src/settings/display-size.jsx +53 -0
  111. package/src/settings/index.js +83 -0
  112. package/src/settings/panel.jsx +333 -0
  113. package/src/settings/settings-radio-label.jsx +32 -0
  114. package/src/settings/toggle.jsx +46 -0
  115. package/src/tabs/index.jsx +47 -0
  116. package/src/tags-input/__tests__/index.test.jsx +113 -0
  117. package/src/tags-input/index.jsx +116 -0
  118. package/src/two-choice.jsx +90 -0
  119. package/src/with-stateful-model.jsx +36 -0
  120. package/dist/_virtual/_rolldown/runtime.js +0 -11
  121. package/dist/alert-dialog.d.ts +0 -44
  122. package/dist/alert-dialog.js +0 -47
  123. package/dist/checkbox.d.ts +0 -34
  124. package/dist/checkbox.js +0 -57
  125. package/dist/choice-configuration/feedback-menu.d.ts +0 -32
  126. package/dist/choice-configuration/feedback-menu.js +0 -85
  127. package/dist/choice-configuration/index.d.ts +0 -62
  128. package/dist/choice-configuration/index.js +0 -240
  129. package/dist/choice-utils.d.ts +0 -21
  130. package/dist/choice-utils.js +0 -15
  131. package/dist/feedback-config/feedback-selector.d.ts +0 -33
  132. package/dist/feedback-config/feedback-selector.js +0 -92
  133. package/dist/feedback-config/group.d.ts +0 -21
  134. package/dist/feedback-config/group.js +0 -33
  135. package/dist/feedback-config/index.d.ts +0 -48
  136. package/dist/feedback-config/index.js +0 -96
  137. package/dist/form-section.d.ts +0 -25
  138. package/dist/form-section.js +0 -25
  139. package/dist/fraction-to-number.d.ts +0 -7
  140. package/dist/fraction-to-number.js +0 -9
  141. package/dist/help.d.ts +0 -41
  142. package/dist/help.js +0 -61
  143. package/dist/index.d.ts +0 -31
  144. package/dist/index.js +0 -34
  145. package/dist/input.d.ts +0 -29
  146. package/dist/input.js +0 -65
  147. package/dist/inputs.d.ts +0 -63
  148. package/dist/inputs.js +0 -70
  149. package/dist/langs.d.ts +0 -41
  150. package/dist/langs.js +0 -76
  151. package/dist/layout/config-layout.d.ts +0 -10
  152. package/dist/layout/config-layout.js +0 -75
  153. package/dist/layout/index.d.ts +0 -11
  154. package/dist/layout/index.js +0 -10
  155. package/dist/layout/layout-contents.d.ts +0 -21
  156. package/dist/layout/layout-contents.js +0 -70
  157. package/dist/layout/settings-box.d.ts +0 -19
  158. package/dist/layout/settings-box.js +0 -31
  159. package/dist/mui-box/index.d.ts +0 -21
  160. package/dist/mui-box/index.js +0 -47
  161. package/dist/node_modules/.bun/@babel_runtime@7.28.6/node_modules/@babel/runtime/helpers/esm/extends.js +0 -12
  162. package/dist/node_modules/.bun/@babel_runtime@7.28.6/node_modules/@babel/runtime/helpers/esm/inheritsLoose.js +0 -7
  163. package/dist/node_modules/.bun/@babel_runtime@7.28.6/node_modules/@babel/runtime/helpers/esm/objectWithoutPropertiesLoose.js +0 -12
  164. package/dist/node_modules/.bun/@babel_runtime@7.28.6/node_modules/@babel/runtime/helpers/esm/setPrototypeOf.js +0 -8
  165. package/dist/node_modules/.bun/react-measure@2.5.2_6dbf9a050bc9aadb/node_modules/react-measure/dist/index.esm.js +0 -122
  166. package/dist/node_modules/.bun/resize-observer-polyfill@1.5.1/node_modules/resize-observer-polyfill/dist/ResizeObserver.es.js +0 -276
  167. package/dist/number-text-field-custom.d.ts +0 -51
  168. package/dist/number-text-field-custom.js +0 -192
  169. package/dist/number-text-field.d.ts +0 -47
  170. package/dist/number-text-field.js +0 -122
  171. package/dist/radio-with-label.d.ts +0 -25
  172. package/dist/radio-with-label.js +0 -27
  173. package/dist/settings/display-size.d.ts +0 -26
  174. package/dist/settings/display-size.js +0 -45
  175. package/dist/settings/index.d.ts +0 -45
  176. package/dist/settings/index.js +0 -63
  177. package/dist/settings/panel.d.ts +0 -27
  178. package/dist/settings/panel.js +0 -201
  179. package/dist/settings/settings-radio-label.d.ts +0 -25
  180. package/dist/settings/settings-radio-label.js +0 -29
  181. package/dist/settings/toggle.d.ts +0 -25
  182. package/dist/settings/toggle.js +0 -33
  183. package/dist/tabs/index.d.ts +0 -22
  184. package/dist/tabs/index.js +0 -39
  185. package/dist/tags-input/index.d.ts +0 -21
  186. package/dist/tags-input/index.js +0 -83
  187. package/dist/two-choice.d.ts +0 -43
  188. package/dist/two-choice.js +0 -79
  189. package/dist/with-stateful-model.d.ts +0 -42
  190. package/dist/with-stateful-model.js +0 -32
@@ -0,0 +1,333 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { get, set } from 'lodash-es';
4
+ import { styled } from '@mui/material/styles';
5
+ import Select from '@mui/material/Select';
6
+ import Input from '@mui/material/Input';
7
+ import MenuItem from '@mui/material/MenuItem';
8
+ import Typography from '@mui/material/Typography';
9
+ import debug from 'debug';
10
+
11
+ import Toggle from './toggle';
12
+ import { NChoice } from '../two-choice';
13
+ import SettingsRadioLabel from './settings-radio-label';
14
+ import NumberTextField from '../number-text-field';
15
+ import Checkbox from '../checkbox';
16
+
17
+ const log = debug('pie-lib:config-ui:settings:panel');
18
+
19
+ const labelValue = {
20
+ label: PropTypes.string,
21
+ value: PropTypes.string,
22
+ };
23
+
24
+ const baseTypes = {
25
+ label: PropTypes.string,
26
+ value: PropTypes.string,
27
+ onChange: PropTypes.func,
28
+ };
29
+
30
+ const CheckboxChoice = ({ label, value, onChange }) => {
31
+ return (
32
+ <Checkbox
33
+ checked={value}
34
+ label={label}
35
+ onChange={(event) => {
36
+ onChange(event.target.checked);
37
+ }}
38
+ />
39
+ );
40
+ };
41
+
42
+ CheckboxChoice.propTypes = {
43
+ label: PropTypes.string,
44
+ value: PropTypes.bool,
45
+ onChange: PropTypes.func,
46
+ };
47
+
48
+ const StyledNChoice = styled(NChoice)(({ theme }) => ({
49
+ marginTop: theme.spacing(0.5),
50
+ paddingBottom: theme.spacing(0.5),
51
+ width: '100%',
52
+ '& > label': {
53
+ color: 'rgba(0, 0, 0, 0.89)',
54
+ transform: 'translate(0, 10px) scale(1)',
55
+ fontSize: '18px',
56
+ marginTop: theme.spacing(2.5),
57
+ },
58
+ '& > div': {
59
+ marginTop: theme.spacing(2.5),
60
+ },
61
+ }));
62
+
63
+ const Radio = ({ label, value, onChange, choices }) => {
64
+ return (
65
+ <StyledNChoice
66
+ direction="horizontal"
67
+ customLabel={SettingsRadioLabel}
68
+ value={value}
69
+ header={label}
70
+ opts={choices}
71
+ onChange={onChange}
72
+ />
73
+ );
74
+ };
75
+
76
+ Radio.propTypes = { ...baseTypes, choices: PropTypes.arrayOf(PropTypes.shape(labelValue)) };
77
+
78
+ const StyledRadio = Radio;
79
+
80
+ const StyledLabel = styled('p')(({ theme }) => ({
81
+ margin: 0,
82
+ fontSize: theme.typography.fontSize,
83
+ }));
84
+
85
+ const StyledSelect = styled(Select)(({ theme }) => ({
86
+ marginTop: theme.spacing(0.5),
87
+ border: '2px solid lightgrey',
88
+ padding: `0 ${theme.spacing(1)}`,
89
+ borderRadius: '4px',
90
+ }));
91
+
92
+ const Dropdown = ({ label, value, onChange, choices = [] }) => {
93
+ const getItemLabel = (l) => (typeof l === 'string' ? l : l.label);
94
+ const getItemValue = (l) => (typeof l === 'string' ? l : l.value);
95
+ return (
96
+ <div>
97
+ {label && <StyledLabel>{label}</StyledLabel>}
98
+ <StyledSelect
99
+ value={value || (choices && choices[0])}
100
+ onChange={({ target }) => onChange(target.value)}
101
+ input={<Input id={`dropdown-${label}`} />}
102
+ disableUnderline
103
+ MenuProps={{ transitionDuration: { enter: 225, exit: 195 } }}
104
+ >
105
+ {choices.map((l, index) => (
106
+ <MenuItem key={index} value={getItemValue(l)}>
107
+ {getItemLabel(l)}
108
+ </MenuItem>
109
+ ))}
110
+ </StyledSelect>
111
+ </div>
112
+ );
113
+ };
114
+
115
+ Dropdown.propTypes = { ...baseTypes, choices: PropTypes.arrayOf(PropTypes.string) };
116
+
117
+ const StyledTypography = styled(Typography)(({ theme }) => ({
118
+ marginRight: theme.spacing(3),
119
+ marginTop: theme.spacing(1),
120
+ }));
121
+
122
+ const TextField = ({ label }) => {
123
+ return <StyledTypography>{label}</StyledTypography>;
124
+ };
125
+
126
+ const StyledNumberTextField = styled(NumberTextField)(({ theme }) => ({
127
+ width: '35%',
128
+ marginRight: theme.spacing(3),
129
+ marginTop: theme.spacing(1),
130
+ '& .MuiInputBase-root': {
131
+ marginTop: theme.spacing(0.5),
132
+ border: '2px solid lightgrey',
133
+ borderRadius: '4px',
134
+ padding: `0 ${theme.spacing(1)}`,
135
+ backgroundColor: 'transparent',
136
+ },
137
+ }));
138
+
139
+ const NumberField = ({ label, value, onChange = () => {}, suffix, min, max }) => {
140
+ return (
141
+ <StyledNumberTextField
142
+ variant={'standard'}
143
+ label={label || 'Label'}
144
+ value={value}
145
+ max={max}
146
+ min={min}
147
+ onChange={(ev, value) => onChange(value)}
148
+ suffix={suffix}
149
+ showErrorWhenOutsideRange
150
+ disableUnderline
151
+ />
152
+ );
153
+ };
154
+
155
+ NumberField.propTypes = {
156
+ ...baseTypes,
157
+ suffix: PropTypes.string,
158
+ min: PropTypes.number,
159
+ max: PropTypes.number,
160
+ value: PropTypes.number,
161
+ };
162
+
163
+ TextField.propTypes = {
164
+ ...baseTypes,
165
+ };
166
+
167
+ const ToggleWrapper = ({ disabled, label, value, onChange }) => (
168
+ <Toggle label={label} checked={!!value} disabled={!!disabled} toggle={onChange} />
169
+ );
170
+
171
+ ToggleWrapper.propTypes = { ...baseTypes, value: PropTypes.bool };
172
+
173
+ const tagMap = {
174
+ toggle: ToggleWrapper,
175
+ radio: StyledRadio,
176
+ dropdown: Dropdown,
177
+ numberField: NumberField,
178
+ checkbox: CheckboxChoice,
179
+ textField: TextField,
180
+ };
181
+
182
+ const StyledGroup = styled('div')(({ theme }) => ({
183
+ margin: `0 0 ${theme.spacing(2)} 0`,
184
+ }));
185
+
186
+ const StyledGroupHeader = styled('div')(({ theme }) => ({
187
+ color: '#495B8F',
188
+ fontSize: theme.typography.fontSize + 2,
189
+ fontWeight: 600,
190
+ marginBottom: theme.spacing(1),
191
+ }));
192
+
193
+ const StyledNumberFields = styled('p')(({ theme }) => ({
194
+ fontSize: theme.typography.fontSize,
195
+ marginBottom: 0,
196
+ }));
197
+
198
+ const Group = (props) => {
199
+ const { model, label, group, configuration, onChange } = props;
200
+
201
+ /**
202
+ * @param group - the group of settings
203
+ * @param key - the key(or path) to be used to set or get from model or configuration
204
+ * @param innerKey - the key(or path) to be used to get from the group (used only for numberField type)
205
+ * @returns tag that corresponds to element type */
206
+ const getTag = (group, key, innerKey) => {
207
+ const { isConfigProperty, ...properties } = get(group, innerKey || key);
208
+ const value = isConfigProperty ? get(configuration, key) : get(model, key);
209
+ const tagProps = { ...properties, key, value };
210
+ const Tag = tagMap[tagProps.type];
211
+
212
+ return <Tag key={key} {...tagProps} onChange={(v) => onChange(key, v, isConfigProperty)} />;
213
+ };
214
+
215
+ const content = (group, key) => {
216
+ const currentGroup = group[key];
217
+
218
+ if (!currentGroup) {
219
+ return null;
220
+ }
221
+
222
+ const { type, label, fields, choices } = currentGroup;
223
+
224
+ if (type === 'numberFields') {
225
+ return (
226
+ <div key={`numberField-${label}`}>
227
+ <StyledNumberFields>{label}</StyledNumberFields>
228
+ {Object.keys(fields).map((fieldKey) => {
229
+ return getTag(group, `${key}.${fieldKey}`, `${key}.fields.${fieldKey}`);
230
+ })}
231
+ </div>
232
+ );
233
+ }
234
+
235
+ if (type === 'checkboxes') {
236
+ return (
237
+ <div key={`checkbox-${label}`}>
238
+ <p>{label}</p>
239
+ {Object.keys(choices).map((choiceKey) => {
240
+ return getTag(group, `${key}.${choiceKey}`, `${key}.choices.${choiceKey}`);
241
+ })}
242
+ </div>
243
+ );
244
+ }
245
+
246
+ // if type is toggle, radio, dropdown, numberField or numberText
247
+ return getTag(group, key);
248
+ };
249
+
250
+ return (
251
+ <StyledGroup>
252
+ <StyledGroupHeader>{label}</StyledGroupHeader>
253
+
254
+ {Object.keys(group).map((key) => {
255
+ return content(group, key);
256
+ })}
257
+ </StyledGroup>
258
+ );
259
+ };
260
+
261
+ Group.propTypes = {
262
+ model: PropTypes.object,
263
+ label: PropTypes.string,
264
+ group: PropTypes.object,
265
+ configuration: PropTypes.object,
266
+ onChange: PropTypes.func,
267
+ };
268
+
269
+ export class Panel extends React.Component {
270
+ static propTypes = {
271
+ model: PropTypes.object,
272
+ configuration: PropTypes.object,
273
+ groups: PropTypes.object,
274
+ onChangeModel: PropTypes.func,
275
+ onChangeConfiguration: PropTypes.func,
276
+ modal: PropTypes.object,
277
+ };
278
+
279
+ static defaultProps = {
280
+ onChangeModel: () => {},
281
+ onChangeConfiguration: () => {},
282
+ };
283
+
284
+ change = (key, value, isConfigProperty = false) => {
285
+ log('[changeModel]', key, value);
286
+
287
+ const { onChangeModel, onChangeConfiguration } = this.props;
288
+ const model = { ...this.props.model };
289
+ const configuration = { ...this.props.configuration };
290
+
291
+ if (isConfigProperty) {
292
+ set(configuration, key, value);
293
+ onChangeConfiguration(configuration, key);
294
+ } else {
295
+ set(model, key, value);
296
+ onChangeModel(model, key);
297
+ }
298
+ };
299
+
300
+ render() {
301
+ const { groups, model, configuration, modal } = this.props;
302
+
303
+ log('render:', model);
304
+
305
+ const renderedGroups = Object.keys(groups || {}).map((group) => {
306
+ const showGroup = Object.entries(groups[group]).some(([, propVal]) => !!propVal);
307
+
308
+ if (showGroup) {
309
+ return (
310
+ <Group
311
+ label={group}
312
+ key={group}
313
+ model={model}
314
+ configuration={configuration}
315
+ group={groups[group]}
316
+ onChange={this.change}
317
+ />
318
+ );
319
+ }
320
+
321
+ return null;
322
+ });
323
+
324
+ return (
325
+ <div>
326
+ {renderedGroups}
327
+ {modal}
328
+ </div>
329
+ );
330
+ }
331
+ }
332
+
333
+ export default Panel;
@@ -0,0 +1,32 @@
1
+ import FormControlLabel from '@mui/material/FormControlLabel';
2
+ import Radio from '@mui/material/Radio';
3
+ import React from 'react';
4
+ import PropTypes from 'prop-types';
5
+ import { styled } from '@mui/material/styles';
6
+ import { color } from '@pie-lib/render-ui';
7
+
8
+ const StyledFormControlLabel = styled(FormControlLabel)(({ theme }) => ({
9
+ '& .MuiFormControlLabel-label': {
10
+ color: 'rgba(0, 0, 0, 0.89)',
11
+ fontSize: theme.typography.fontSize - 2,
12
+ left: '-5px',
13
+ position: 'relative',
14
+ },
15
+ }));
16
+
17
+ const StyledRadio = styled(Radio)(() => ({
18
+ color: `${color.tertiary()} !important`,
19
+ }));
20
+
21
+ const SettingsRadioLabel = ({ label, value, checked, onChange }) => (
22
+ <StyledFormControlLabel value={value} control={<StyledRadio checked={checked} onChange={onChange} />} label={label} />
23
+ );
24
+
25
+ SettingsRadioLabel.propTypes = {
26
+ label: PropTypes.string,
27
+ value: PropTypes.string,
28
+ checked: PropTypes.bool,
29
+ onChange: PropTypes.func,
30
+ };
31
+
32
+ export default SettingsRadioLabel;
@@ -0,0 +1,46 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import InputLabel from '@mui/material/InputLabel';
4
+ import { styled } from '@mui/material/styles';
5
+ import Switch from '@mui/material/Switch';
6
+ import { color } from '@pie-lib/render-ui';
7
+
8
+ const StyledToggle = styled('div')(() => ({
9
+ display: 'flex',
10
+ width: '100%',
11
+ justifyContent: 'space-between',
12
+ }));
13
+
14
+ const StyledInputLabel = styled(InputLabel)(({ theme }) => ({
15
+ color: 'rgba(0, 0, 0, 0.89)',
16
+ fontSize: theme.typography.fontSize,
17
+ paddingTop: theme.spacing(2),
18
+ }));
19
+
20
+ const StyledSwitch = styled(Switch)(({ checked }) => ({
21
+ '&.Mui-checked .MuiSwitch-thumb': {
22
+ color: `${color.tertiary()} !important`,
23
+ },
24
+ '&.Mui-checked .MuiSwitch-track': {
25
+ backgroundColor: `${color.tertiaryLight()} !important`,
26
+ },
27
+ '& .MuiSwitch-track': {
28
+ backgroundColor: checked ? `${color.tertiaryLight()} !important` : undefined,
29
+ },
30
+ }));
31
+
32
+ const Toggle = ({ checked, disabled, label, toggle }) => (
33
+ <StyledToggle>
34
+ <StyledInputLabel>{label}</StyledInputLabel>
35
+ <StyledSwitch checked={checked} disabled={disabled} onChange={(e) => toggle(e.target.checked)} />
36
+ </StyledToggle>
37
+ );
38
+
39
+ Toggle.propTypes = {
40
+ checked: PropTypes.bool,
41
+ disabled: PropTypes.bool,
42
+ label: PropTypes.string.isRequired,
43
+ toggle: PropTypes.func.isRequired,
44
+ };
45
+
46
+ export default Toggle;
@@ -0,0 +1,47 @@
1
+ import React from 'react';
2
+
3
+ import MuiTabs from '@mui/material/Tabs';
4
+ import MuiTab from '@mui/material/Tab';
5
+ import PropTypes from 'prop-types';
6
+ import { styled } from '@mui/material/styles';
7
+
8
+ const StyledMuiTab = styled(MuiTab)(() => ({}));
9
+
10
+ export class Tabs extends React.Component {
11
+ static propTypes = {
12
+ className: PropTypes.string,
13
+ contentClassName: PropTypes.string,
14
+ contentStyle: PropTypes.object,
15
+ children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
16
+ };
17
+
18
+ constructor(props) {
19
+ super(props);
20
+ this.state = { value: 0 };
21
+ }
22
+
23
+ handleChange = (event, value) => {
24
+ this.setState({ value });
25
+ };
26
+
27
+ render() {
28
+ const { value } = this.state;
29
+ const { children, className, contentClassName, contentStyle = {} } = this.props;
30
+
31
+ return (
32
+ <div className={className}>
33
+ <MuiTabs indicatorColor="primary" value={value} onChange={this.handleChange}>
34
+ {React.Children.map(children, (c, index) =>
35
+ c && c.props.title ? <StyledMuiTab key={index} label={c.props.title} /> : null,
36
+ )}
37
+ </MuiTabs>
38
+
39
+ <div className={contentClassName} style={contentStyle}>
40
+ {children[value]}
41
+ </div>
42
+ </div>
43
+ );
44
+ }
45
+ }
46
+
47
+ export default Tabs;
@@ -0,0 +1,113 @@
1
+ import { TagsInput } from '../index';
2
+ import { Keys, pressKey, render, screen, userEvent } from '@pie-lib/test-utils';
3
+ import React from 'react';
4
+
5
+ describe('TagsInput', () => {
6
+ describe('rendering', () => {
7
+ it('renders existing tags as chips', () => {
8
+ render(<TagsInput tags={['foo', 'bar']} onChange={jest.fn()} />);
9
+
10
+ expect(screen.getByText('foo')).toBeInTheDocument();
11
+ expect(screen.getByText('bar')).toBeInTheDocument();
12
+ });
13
+
14
+ it('renders input field', () => {
15
+ render(<TagsInput tags={['foo']} onChange={jest.fn()} />);
16
+
17
+ const input = screen.getByRole('textbox');
18
+ expect(input).toBeInTheDocument();
19
+ });
20
+ });
21
+
22
+ describe('user interactions', () => {
23
+ let onChange;
24
+ const renderComponent = (tags = ['foo']) => {
25
+ onChange = jest.fn();
26
+ return render(<TagsInput onChange={onChange} tags={tags} />);
27
+ };
28
+
29
+ describe('focus behavior', () => {
30
+ it('allows user to focus the input', async () => {
31
+ const user = userEvent.setup();
32
+ renderComponent();
33
+
34
+ const input = screen.getByRole('textbox');
35
+ await user.click(input);
36
+
37
+ expect(input).toHaveFocus();
38
+ });
39
+
40
+ it('allows user to blur the input', async () => {
41
+ const user = userEvent.setup();
42
+ renderComponent();
43
+
44
+ const input = screen.getByRole('textbox');
45
+ await user.click(input);
46
+ expect(input).toHaveFocus();
47
+
48
+ await user.tab();
49
+ expect(input).not.toHaveFocus();
50
+ });
51
+ });
52
+
53
+ describe('typing in input', () => {
54
+ it('updates input value when user types', async () => {
55
+ const user = userEvent.setup();
56
+ renderComponent();
57
+
58
+ const input = screen.getByRole('textbox');
59
+ await user.type(input, 'boo');
60
+
61
+ expect(input).toHaveValue('boo');
62
+ });
63
+ });
64
+
65
+ describe('adding tags', () => {
66
+ it('adds new tag when user presses Enter', async () => {
67
+ const user = userEvent.setup();
68
+ renderComponent();
69
+
70
+ const input = screen.getByRole('textbox');
71
+ await user.type(input, 'banana');
72
+ pressKey(input, Keys.ENTER);
73
+
74
+ expect(onChange).toHaveBeenCalledWith(['foo', 'banana']);
75
+ });
76
+
77
+ it('does not add duplicate tags', async () => {
78
+ const user = userEvent.setup();
79
+ renderComponent();
80
+
81
+ const input = screen.getByRole('textbox');
82
+ await user.type(input, 'foo');
83
+ pressKey(input, Keys.ENTER);
84
+
85
+ expect(onChange).not.toHaveBeenCalled();
86
+ });
87
+
88
+ it('clears input after adding tag', async () => {
89
+ const user = userEvent.setup();
90
+ renderComponent();
91
+
92
+ const input = screen.getByRole('textbox');
93
+ await user.type(input, 'banana');
94
+ pressKey(input, Keys.ENTER);
95
+
96
+ expect(input).toHaveValue('');
97
+ });
98
+ });
99
+
100
+ describe('deleting tags', () => {
101
+ it('removes tag when user clicks delete button', async () => {
102
+ const user = userEvent.setup();
103
+ renderComponent(['foo', 'bar']);
104
+
105
+ // Find the delete button for 'foo' tag
106
+ const deleteButtons = screen.getAllByTestId('CancelIcon');
107
+ await user.click(deleteButtons[0]);
108
+
109
+ expect(onChange).toHaveBeenCalledWith(['bar']);
110
+ });
111
+ });
112
+ });
113
+ });
@@ -0,0 +1,116 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { styled } from '@mui/material/styles';
4
+ import { uniq } from 'lodash-es';
5
+ import Chip from '@mui/material/Chip';
6
+ import MuiBox from '../mui-box';
7
+
8
+ const ENTER = 13;
9
+
10
+ const StyledChip = styled(Chip)(() => ({
11
+ padding: '0px',
12
+ margin: '1px',
13
+ }));
14
+
15
+ const Tag = ({ label, onDelete }) => <StyledChip label={label} onDelete={onDelete} />;
16
+
17
+ Tag.propTypes = {
18
+ label: PropTypes.string.isRequired,
19
+ onDelete: PropTypes.func.isRequired,
20
+ };
21
+
22
+ const StyledTagsInput = styled('div')(({ theme }) => ({
23
+ border: `0px solid ${theme.palette.background.paper}`,
24
+ display: 'flex',
25
+ flexWrap: 'wrap',
26
+ }));
27
+
28
+ const StyledInput = styled('input')(({ theme }) => ({
29
+ padding: '2px',
30
+ margin: '1px',
31
+ minWidth: '30px',
32
+ width: '100%',
33
+ flex: '1',
34
+ border: `0px solid ${theme.palette.background.paper}`,
35
+ height: '28px',
36
+ fontSize: theme.typography.fontSize,
37
+ fontFamily: theme.typography.fontFamily,
38
+ outline: 'none',
39
+ '&:focus': {
40
+ outline: 'none',
41
+ },
42
+ }));
43
+
44
+ export class TagsInput extends React.Component {
45
+ static propTypes = {
46
+ tags: PropTypes.arrayOf(PropTypes.string).isRequired,
47
+ onChange: PropTypes.func.isRequired,
48
+ };
49
+
50
+ constructor(props) {
51
+ super(props);
52
+ this.state = {
53
+ value: '',
54
+ focused: false,
55
+ };
56
+
57
+ this.onKeyDown = (event) => {
58
+ if (event.keyCode === ENTER && this.state.value !== '') {
59
+ const tag = this.state.value.trim();
60
+ const newTags = uniq(this.props.tags.concat([tag]));
61
+
62
+ if (newTags.length !== this.props.tags.length) {
63
+ this.props.onChange(newTags);
64
+ this.setState({ value: '' });
65
+ }
66
+ }
67
+ };
68
+
69
+ this.onChange = (event) => {
70
+ this.setState({ value: event.target.value });
71
+ };
72
+
73
+ this.deleteTag = (tag) => {
74
+ const { tags } = this.props;
75
+
76
+ const tagIndex = tags.indexOf(tag);
77
+ if (tagIndex !== -1) {
78
+ tags.splice(tagIndex, 1);
79
+ this.props.onChange(tags);
80
+ this.input.focus();
81
+ }
82
+ };
83
+ }
84
+
85
+ onFocus = () => {
86
+ this.setState({ focused: true });
87
+ };
88
+
89
+ onBlur = () => {
90
+ this.setState({ focused: false });
91
+ };
92
+
93
+ render() {
94
+ const { tags } = this.props;
95
+ return (
96
+ <MuiBox focused={this.state.focused}>
97
+ <StyledTagsInput>
98
+ {(tags || []).map((t, index) => (
99
+ <Tag key={index} label={t} onDelete={() => this.deleteTag(t)} />
100
+ ))}
101
+ <StyledInput
102
+ ref={(r) => (this.input = r)}
103
+ onKeyDown={this.onKeyDown}
104
+ onChange={this.onChange}
105
+ value={this.state.value}
106
+ onFocus={this.onFocus}
107
+ onBlur={this.onBlur}
108
+ type="text"
109
+ />
110
+ </StyledTagsInput>
111
+ </MuiBox>
112
+ );
113
+ }
114
+ }
115
+
116
+ export default TagsInput;