@onewelcome/react-lib-components 1.5.0 → 1.7.0

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 (101) hide show
  1. package/README.md +4 -4
  2. package/dist/Button/Button.d.ts +0 -1
  3. package/dist/DataGrid/datagrid.interfaces.d.ts +1 -0
  4. package/dist/Form/Checkbox/Checkbox.d.ts +1 -1
  5. package/dist/Form/FileUpload/FileItem/FileItem.d.ts +17 -0
  6. package/dist/Form/FileUpload/FileUpload.d.ts +26 -0
  7. package/dist/Form/FormHelperText/FormHelperText.d.ts +1 -1
  8. package/dist/Form/FormSelectorWrapper/FormSelectorWrapper.d.ts +1 -1
  9. package/dist/Form/Input/Input.d.ts +2 -2
  10. package/dist/Form/Radio/Radio.d.ts +1 -1
  11. package/dist/Form/Select/Select.d.ts +1 -1
  12. package/dist/Form/Textarea/Textarea.d.ts +1 -6
  13. package/dist/Form/Toggle/Toggle.d.ts +1 -1
  14. package/dist/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.d.ts +1 -1
  15. package/dist/Form/Wrapper/InputWrapper/InputWrapper.d.ts +1 -1
  16. package/dist/Form/Wrapper/RadioWrapper/RadioWrapper.d.ts +1 -1
  17. package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
  18. package/dist/Form/Wrapper/TextareaWrapper/TextareaWrapper.d.ts +1 -1
  19. package/dist/Form/form.interfaces.d.ts +1 -0
  20. package/dist/Icon/Icon.d.ts +4 -1
  21. package/dist/Link/Link.d.ts +1 -2
  22. package/dist/Notifications/Banner/Banner.d.ts +11 -0
  23. package/dist/ProgressBar/ProgressBar.d.ts +2 -1
  24. package/dist/Tabs/TabButton.d.ts +0 -1
  25. package/dist/_BaseStyling_/BaseStyling.d.ts +5 -0
  26. package/dist/hooks/useDetermineStatusIcon.d.ts +3 -0
  27. package/dist/hooks/useUploadFile.d.ts +22 -0
  28. package/dist/index.d.ts +1 -0
  29. package/dist/react-lib-components.cjs.development.js +431 -326
  30. package/dist/react-lib-components.cjs.development.js.map +1 -1
  31. package/dist/react-lib-components.cjs.production.min.js +1 -1
  32. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  33. package/dist/react-lib-components.esm.js +431 -327
  34. package/dist/react-lib-components.esm.js.map +1 -1
  35. package/dist/util/helper.d.ts +5 -0
  36. package/package.json +28 -25
  37. package/src/Button/BaseButton.module.scss +2 -2
  38. package/src/Button/Button.module.scss +4 -5
  39. package/src/Button/Button.tsx +0 -1
  40. package/src/Button/IconButton.module.scss +4 -5
  41. package/src/DataGrid/DataGrid.tsx +3 -2
  42. package/src/DataGrid/DataGridActions/DataGridActions.tsx +16 -9
  43. package/src/DataGrid/DataGridBody/DataGridCell.module.scss +2 -2
  44. package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +8 -3
  45. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +3 -1
  46. package/src/DataGrid/datagrid.interfaces.ts +1 -0
  47. package/src/Form/FileUpload/FileItem/FileItem.modules.scss +75 -0
  48. package/src/Form/FileUpload/FileItem/FileItem.test.tsx +103 -0
  49. package/src/Form/FileUpload/FileItem/FileItem.tsx +141 -0
  50. package/src/Form/FileUpload/FileUpload.module.scss +106 -0
  51. package/src/Form/FileUpload/FileUpload.test.tsx +374 -0
  52. package/src/Form/FileUpload/FileUpload.tsx +251 -0
  53. package/src/Form/Input/Input.module.scss +36 -26
  54. package/src/Form/Input/Input.test.tsx +10 -0
  55. package/src/Form/Input/Input.tsx +7 -5
  56. package/src/Form/Select/Select.module.scss +9 -6
  57. package/src/Form/Select/Select.test.tsx +11 -0
  58. package/src/Form/Select/Select.tsx +5 -9
  59. package/src/Form/Select/SelectService.ts +2 -2
  60. package/src/Form/Textarea/Textarea.module.scss +21 -13
  61. package/src/Form/Textarea/Textarea.test.tsx +8 -0
  62. package/src/Form/Textarea/Textarea.tsx +6 -12
  63. package/src/Form/Toggle/Toggle.module.scss +3 -3
  64. package/src/Form/Wrapper/InputWrapper/InputWrapper.module.scss +7 -3
  65. package/src/Form/Wrapper/InputWrapper/InputWrapper.tsx +2 -0
  66. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +12 -1
  67. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.module.scss +15 -14
  68. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.tsx +2 -1
  69. package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +2 -2
  70. package/src/Form/form.interfaces.ts +1 -0
  71. package/src/Icon/Icon.module.scss +12 -0
  72. package/src/Icon/Icon.tsx +4 -1
  73. package/src/Link/Link.module.scss +5 -5
  74. package/src/Link/Link.tsx +14 -13
  75. package/src/Notifications/Banner/Banner.module.scss +76 -0
  76. package/src/Notifications/Banner/Banner.test.tsx +84 -0
  77. package/src/Notifications/Banner/Banner.tsx +78 -0
  78. package/src/Notifications/BaseModal/BaseModal.module.scss +2 -2
  79. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +2 -2
  80. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +4 -4
  81. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.tsx +3 -2
  82. package/src/Popover/Popover.module.scss +2 -2
  83. package/src/ProgressBar/ProgressBar.module.scss +11 -9
  84. package/src/ProgressBar/ProgressBar.test.tsx +21 -0
  85. package/src/ProgressBar/ProgressBar.tsx +7 -2
  86. package/src/Skeleton/Skeleton.module.scss +2 -2
  87. package/src/Tabs/TabButton.tsx +1 -2
  88. package/src/Tabs/Tabs.module.scss +2 -2
  89. package/src/Tabs/Tabs.tsx +13 -10
  90. package/src/Tiles/Tile.module.scss +4 -4
  91. package/src/Tooltip/Tooltip.module.scss +3 -3
  92. package/src/Typography/Typography.module.scss +2 -2
  93. package/src/_BaseStyling_/BaseStyling.tsx +13 -3
  94. package/src/hooks/useDetermineStatusIcon.test.ts +28 -0
  95. package/src/hooks/useDetermineStatusIcon.tsx +35 -0
  96. package/src/hooks/useUploadFile.test.ts +211 -0
  97. package/src/hooks/useUploadFile.tsx +136 -0
  98. package/src/index.ts +1 -0
  99. package/src/mixins.module.scss +24 -5
  100. package/src/util/helper.test.tsx +156 -1
  101. package/src/util/helper.tsx +33 -0
@@ -0,0 +1,106 @@
1
+ /*!
2
+ * Copyright 2022 OneWelcome B.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ @import "../../mixins.module.scss";
18
+
19
+ .file-upload-wrapper {
20
+ display: flex;
21
+ flex-direction: column;
22
+ }
23
+
24
+ .file-dropzone {
25
+ border-radius: var(--input-border-radius);
26
+ background-color: var(--input-background-color);
27
+ padding: 1.25rem;
28
+ position: relative;
29
+ @include transition(all, 0.2s, ease-in-out);
30
+ &.drag-active {
31
+ @include outline(
32
+ var(--vivid-violet),
33
+ var(--drag-border-style),
34
+ var(--input-border-width),
35
+ var(--input-border-radius),
36
+ var(--light-pink)
37
+ );
38
+ @include outlineStates;
39
+ }
40
+ &.error {
41
+ span[data-icon-status],
42
+ .file-upload-title,
43
+ .file-selector-sub-text {
44
+ color: var(--error);
45
+ }
46
+ }
47
+ &.success {
48
+ span[data-icon-status] {
49
+ color: var(--success);
50
+ }
51
+ }
52
+
53
+ &.disabled {
54
+ pointer-events: none;
55
+ background-color: var(--disabled);
56
+ color: var(--greyed-out);
57
+ }
58
+ span[data-icon-status] {
59
+ font-size: 1.25rem;
60
+ position: absolute;
61
+ top: 2.74rem;
62
+ right: 1.375rem;
63
+ }
64
+ }
65
+
66
+ .file-select {
67
+ display: flex;
68
+ align-items: center;
69
+ flex-direction: column;
70
+ [class*="file"] {
71
+ font-size: 1.25rem;
72
+ }
73
+ [class*="typography"] {
74
+ margin-bottom: 0.625rem;
75
+ }
76
+ }
77
+
78
+ .upload-input {
79
+ display: block;
80
+ width: 100%;
81
+ border: none;
82
+ text-transform: none;
83
+ position: absolute;
84
+ top: 0;
85
+ left: 0;
86
+ right: 0;
87
+ bottom: 0;
88
+ opacity: 0;
89
+ cursor: pointer;
90
+ &:focus {
91
+ outline: none;
92
+ }
93
+ }
94
+
95
+ .file-upload-btn {
96
+ position: relative;
97
+ }
98
+
99
+ .file-list {
100
+ list-style: none;
101
+ padding: 0;
102
+ margin: 0.75rem 0;
103
+ li:not(:last-child) {
104
+ margin-bottom: 0.5rem;
105
+ }
106
+ }
@@ -0,0 +1,374 @@
1
+ /*
2
+ * Copyright 2022 OneWelcome B.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import React, { useEffect, useRef } from "react";
18
+ import { FileType, FileUpload, Props } from "./FileUpload";
19
+ import { createEvent, fireEvent, render, waitFor } from "@testing-library/react";
20
+
21
+ import user from "@testing-library/user-event";
22
+ import { act } from "react-dom/test-utils";
23
+
24
+ const defaultParams: Props = {
25
+ accept: ".pdf, .jpg, .txt",
26
+ title: "File Upload test",
27
+ multiple: false,
28
+ fileList: []
29
+ };
30
+
31
+ const createComponent = (params?: (defaultParams: Props) => Props, dataTesId?: string) => {
32
+ let parameters: Props = defaultParams;
33
+ const id = dataTesId || "file-upload";
34
+ if (params) {
35
+ parameters = params(defaultParams);
36
+ }
37
+ const queries = render(<FileUpload {...parameters} data-testid={id} />);
38
+ const component = queries.getByTestId(id);
39
+ const container = queries.container;
40
+
41
+ return {
42
+ ...queries,
43
+ container,
44
+ component
45
+ };
46
+ };
47
+
48
+ describe("component should render", () => {
49
+ it("renders without crashing", () => {
50
+ const { component } = createComponent();
51
+
52
+ expect(component).toBeDefined();
53
+ });
54
+ });
55
+
56
+ describe("ref should work", () => {
57
+ it("should give back the proper data prop, this also checks if the component propagates ...rest properly", () => {
58
+ const ExampleComponent = ({
59
+ propagateRef
60
+ }: {
61
+ propagateRef?: (ref: React.RefObject<HTMLElement>) => void;
62
+ }) => {
63
+ const ref = useRef(null);
64
+
65
+ useEffect(() => {
66
+ if (ref.current) {
67
+ propagateRef && propagateRef(ref);
68
+ }
69
+ }, [ref]);
70
+
71
+ return <FileUpload {...defaultParams} data-ref="testing" ref={ref} />;
72
+ };
73
+
74
+ const refCheck = (ref: React.RefObject<HTMLElement>) => {
75
+ expect(ref.current).toHaveAttribute("data-ref", "testing");
76
+ };
77
+
78
+ render(<ExampleComponent propagateRef={refCheck} />);
79
+ });
80
+ });
81
+
82
+ describe("File upload properties", () => {
83
+ it("is disabled", () => {
84
+ const { container } = createComponent(
85
+ defaultParams => ({
86
+ ...defaultParams,
87
+ disabled: true
88
+ }),
89
+ "file-upload-1"
90
+ );
91
+ const dropZone = container.querySelector(".file-dropzone");
92
+ expect(dropZone).toHaveClass("disabled");
93
+ });
94
+
95
+ it("shows success", () => {
96
+ const { container } = createComponent(
97
+ defaultParams => ({
98
+ ...defaultParams,
99
+ success: true
100
+ }),
101
+ "file-upload-2"
102
+ );
103
+
104
+ const dropZone = container.querySelector(".file-dropzone");
105
+ expect(dropZone).toHaveClass("success");
106
+ const icon = container.querySelector("[data-icon-status='success']");
107
+ expect(icon).toBeDefined();
108
+ });
109
+
110
+ it("shows error", () => {
111
+ const { container } = createComponent(
112
+ defaultParams => ({
113
+ ...defaultParams,
114
+ error: true
115
+ }),
116
+ "file-upload-3"
117
+ );
118
+
119
+ const dropZone = container.querySelector(".file-dropzone");
120
+ expect(dropZone).toHaveClass("error");
121
+ const icon = container.querySelector("[data-icon-status='error']");
122
+ expect(icon).toBeDefined();
123
+ });
124
+
125
+ it("has multiple attribute setup on the input", () => {
126
+ const { component } = createComponent(
127
+ defaultParams => ({
128
+ ...defaultParams,
129
+ multiple: true
130
+ }),
131
+ "file-upload-4"
132
+ );
133
+
134
+ expect(component).toHaveAttribute("multiple");
135
+ });
136
+
137
+ it("has the correct drag and drop label", () => {
138
+ const text = "test drag and drop text";
139
+ const { container } = createComponent(
140
+ defaultParams => ({
141
+ ...defaultParams,
142
+ dragAndDropText: text
143
+ }),
144
+ "file-upload-5"
145
+ );
146
+
147
+ expect(container.querySelector(".drag-and-drop-text")).toHaveTextContent(text);
148
+ });
149
+
150
+ it("has the correct button label", () => {
151
+ const text = "Test";
152
+ const { container } = createComponent(
153
+ defaultParams => ({
154
+ ...defaultParams,
155
+ selectButtonText: text
156
+ }),
157
+ "file-upload-6"
158
+ );
159
+
160
+ expect(container.querySelector("button")).toHaveTextContent(text);
161
+ });
162
+
163
+ afterEach(() => {
164
+ jest.clearAllMocks();
165
+ });
166
+ });
167
+
168
+ describe("File Upload should display items based on file list", () => {
169
+ it("should display the file list with each status", () => {
170
+ const file: FileType = {
171
+ name: "test",
172
+ size: 2,
173
+ type: "text",
174
+ status: "completed"
175
+ };
176
+ const { container } = createComponent(
177
+ defaultParams => ({
178
+ ...defaultParams,
179
+ fileList: [file]
180
+ }),
181
+ "file-upload-10"
182
+ );
183
+
184
+ const fileEl = container.querySelector(`#${file.name}`);
185
+ expect(fileEl).toBeDefined();
186
+ expect(fileEl).toHaveClass(file.status as string);
187
+ });
188
+ });
189
+
190
+ describe("file drag and drop properties", () => {
191
+ it("should call all the drag and drop callback provided", async () => {
192
+ const onDrop = jest.fn();
193
+ const onDragOver = jest.fn();
194
+ const onDragLeave = jest.fn();
195
+ const { container } = createComponent(
196
+ defaultParams => ({
197
+ ...defaultParams,
198
+ accept: ".png",
199
+ onDrop,
200
+ onDragOver,
201
+ onDragLeave,
202
+ fileList: []
203
+ }),
204
+ "file-upload-6"
205
+ );
206
+
207
+ const file = new File([""], "test.png", {
208
+ type: "image/png"
209
+ });
210
+
211
+ const eventData = {
212
+ dataTransfer: {
213
+ files: [file]
214
+ }
215
+ };
216
+
217
+ const dropZone = container.querySelector(".file-dropzone") as Element;
218
+ const dragEnterEvent = createEvent.dragEnter(dropZone, eventData);
219
+ const dragOverEvent = createEvent.dragOver(dropZone, eventData);
220
+ const dropEvent = createEvent.drop(dropZone, eventData);
221
+ const dragLeaveEvent = createEvent.dragLeave(dropZone, eventData);
222
+
223
+ await waitFor(() => {
224
+ fireEvent(dropZone, dragEnterEvent);
225
+ });
226
+
227
+ await waitFor(() => {
228
+ fireEvent(dropZone, dragLeaveEvent);
229
+ });
230
+
231
+ expect(onDragLeave).toHaveBeenCalled();
232
+
233
+ await waitFor(() => {
234
+ fireEvent(dropZone, dragOverEvent);
235
+ });
236
+
237
+ expect(dropZone).toHaveClass("drag-active");
238
+
239
+ expect(onDragOver).toHaveBeenCalled();
240
+
241
+ await waitFor(() => {
242
+ fireEvent(dropZone, dropEvent);
243
+ });
244
+
245
+ expect(onDrop).toHaveBeenCalled();
246
+ });
247
+
248
+ afterEach(() => {
249
+ jest.clearAllMocks();
250
+ });
251
+ });
252
+
253
+ describe("upload action", () => {
254
+ it("shows accepts only files that follow the type rules", async () => {
255
+ const onChange = jest.fn();
256
+ const { component } = createComponent(
257
+ defaultParams => ({
258
+ ...defaultParams,
259
+ onChange,
260
+ fileList: [],
261
+ accept: ".pdf"
262
+ }),
263
+ "file-upload-7"
264
+ );
265
+ const str = "test";
266
+ const blob = new Blob([str]);
267
+ const file = new File([blob], "test.pdf", {
268
+ type: "application/pdf"
269
+ });
270
+ File.prototype.text = jest.fn().mockResolvedValueOnce(str);
271
+ await act(async () => {
272
+ await waitFor(() => {
273
+ user.upload(component, file);
274
+ });
275
+ });
276
+
277
+ const file2 = new File([blob], "test.jpg", {
278
+ type: "application/jpg"
279
+ });
280
+
281
+ await act(async () => {
282
+ await waitFor(() => {
283
+ user.upload(component, file2);
284
+ });
285
+ });
286
+ expect(onChange).toHaveBeenCalledTimes(1);
287
+ });
288
+
289
+ it("doesn't upload a file two times", async () => {
290
+ const onChange = jest.fn();
291
+ const { component } = createComponent(
292
+ defaultParams => ({
293
+ ...defaultParams,
294
+ maxFileSize: 1024 * 1024,
295
+ onChange,
296
+ fileList: []
297
+ }),
298
+ "file-upload-9"
299
+ );
300
+ const value = "test";
301
+ const blob = new Blob([value]);
302
+ const file = new File([blob], "test.pdf", {
303
+ type: "application/pdf"
304
+ });
305
+ Object.defineProperty(file, "size", { value: 1024 * 1024 * 2 });
306
+ File.prototype.text = jest.fn().mockResolvedValueOnce(value);
307
+ await act(async () => {
308
+ await waitFor(() => {
309
+ user.upload(component, file);
310
+ });
311
+ });
312
+ await act(async () => {
313
+ await waitFor(() => {
314
+ user.upload(component, file);
315
+ });
316
+ });
317
+ expect(onChange).toHaveBeenCalledTimes(1);
318
+ });
319
+
320
+ it("doesn't allows files to be dropped according to the accepted file types", async () => {
321
+ const onDrop = jest.fn();
322
+ const { container } = createComponent(
323
+ defaultParams => ({
324
+ ...defaultParams,
325
+ accept: ".jpg",
326
+ onDrop,
327
+ fileList: []
328
+ }),
329
+ "file-upload-10"
330
+ );
331
+
332
+ const file = new File([""], "test.png", {
333
+ type: "image/png"
334
+ });
335
+
336
+ const eventData = {
337
+ dataTransfer: {
338
+ files: [file]
339
+ }
340
+ };
341
+ const dropZone = container.querySelector(".file-dropzone") as Element;
342
+ const dropEvent = createEvent.drop(dropZone, eventData);
343
+ await waitFor(() => {
344
+ fireEvent(dropZone, dropEvent);
345
+ });
346
+
347
+ expect(onDrop).toHaveBeenCalledTimes(0);
348
+ });
349
+
350
+ afterEach(() => {
351
+ jest.clearAllMocks();
352
+ });
353
+ });
354
+
355
+ describe("FileUpload should validate files on first render", () => {
356
+ const onChange = jest.fn();
357
+ const { component } = createComponent(
358
+ defaultParams => ({
359
+ ...defaultParams,
360
+ onChange,
361
+ fileList: [
362
+ {
363
+ name: "test",
364
+ type: "text/txt",
365
+ size: 1024 * 1024
366
+ }
367
+ ]
368
+ }),
369
+ "file-upload-8"
370
+ );
371
+
372
+ expect(component).toBeDefined();
373
+ expect(onChange).toHaveBeenCalled();
374
+ });