@onewelcome/react-lib-components 1.8.3 → 1.9.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.
Files changed (35) hide show
  1. package/dist/Button/BaseButton.d.ts +2 -1
  2. package/dist/Button/Button.d.ts +1 -1
  3. package/dist/Button/IconButton.d.ts +1 -1
  4. package/dist/Button/Spinner.d.ts +2 -0
  5. package/dist/Notifications/BaseModal/BaseModal.d.ts +1 -1
  6. package/dist/_BaseStyling_/BaseStyling.d.ts +1 -1
  7. package/dist/hooks/useGetDomRoot.d.ts +3 -0
  8. package/dist/react-lib-components.cjs.development.js +93 -32
  9. package/dist/react-lib-components.cjs.development.js.map +1 -1
  10. package/dist/react-lib-components.cjs.production.min.js +1 -1
  11. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  12. package/dist/react-lib-components.esm.js +94 -33
  13. package/dist/react-lib-components.esm.js.map +1 -1
  14. package/dist/util/helper.d.ts +1 -0
  15. package/package.json +14 -14
  16. package/src/Button/BaseButton.module.scss +24 -0
  17. package/src/Button/BaseButton.test.tsx +12 -0
  18. package/src/Button/BaseButton.tsx +17 -5
  19. package/src/Button/Spinner.tsx +33 -0
  20. package/src/ContextMenu/ContextMenu.tsx +18 -3
  21. package/src/DataGrid/DataGridBody/__snapshots__/DataGridBody.test.tsx.snap +4 -4
  22. package/src/Form/FileUpload/FileUpload.module.scss +9 -2
  23. package/src/Form/FileUpload/FileUpload.tsx +35 -33
  24. package/src/Form/Wrapper/InputWrapper/InputWrapper.module.scss +1 -0
  25. package/src/Notifications/BaseModal/BaseModal.tsx +48 -41
  26. package/src/Notifications/BaseModal/BaseModalActions/BaseModalActions.module.scss +1 -1
  27. package/src/Notifications/Snackbar/SnackbarProvider/SnackbarProvider.tsx +7 -4
  28. package/src/Tooltip/Tooltip.tsx +9 -3
  29. package/src/_BaseStyling_/BaseStyling.tsx +14 -6
  30. package/src/hooks/useGetDomRoot.ts +40 -0
  31. package/src/hooks/usePosition.ts +2 -1
  32. package/src/hooks/useUploadFile.test.ts +2 -2
  33. package/src/hooks/useUploadFile.tsx +8 -5
  34. package/src/util/helper.test.tsx +16 -1
  35. package/src/util/helper.tsx +9 -0
@@ -19,7 +19,7 @@
19
19
  * and make sure to add it to the shouldBeColorPicker array!
20
20
  */
21
21
 
22
- import React, { Fragment, HTMLAttributes, ReactChild, useEffect, useState } from "react";
22
+ import React, { HTMLAttributes, ReactChild, useEffect, useRef, useState } from "react";
23
23
 
24
24
  interface CSSProperties {
25
25
  colorFocus?: string;
@@ -193,23 +193,31 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
193
193
 
194
194
  /** We need a loading state, because otherwise you see the colors flash from the default to the possible overridden ones. */
195
195
  const [isLoading, setIsLoading] = useState(true);
196
+ const baseStylingWrapper = useRef(null);
196
197
 
197
198
  const setCSSProperties = (CSSPropertiesObject: CSSProperties) => {
198
199
  for (const [key, value] of Object.entries(CSSPropertiesObject)) {
199
200
  const formattedPropertyName = key.replace(/([A-Z])/g, val => `-${val.toLowerCase()}`);
200
- document.documentElement.style.setProperty(`--${formattedPropertyName}`, value);
201
+ (baseStylingWrapper.current! as HTMLElement).style.setProperty(
202
+ `--${formattedPropertyName}`,
203
+ value
204
+ );
201
205
  }
202
206
  };
203
207
 
204
208
  useEffect(() => {
205
- if (Object.keys(properties).length !== 0) {
209
+ if (Object.keys(properties).length !== 0 && baseStylingWrapper.current) {
206
210
  const mergedState = { ...defaultProperties, ...properties };
207
211
  setCSSProperties(mergedState);
208
- } else {
212
+ } else if (baseStylingWrapper.current) {
209
213
  setCSSProperties(defaultProperties);
210
214
  }
211
215
  setIsLoading(false);
212
- }, [properties]);
216
+ }, [properties, baseStylingWrapper.current]);
213
217
 
214
- return !isLoading ? <Fragment>{children}</Fragment> : null;
218
+ return (
219
+ <div className="basestyling-wrapper" ref={baseStylingWrapper}>
220
+ {!isLoading ? children : null}
221
+ </div>
222
+ );
215
223
  };
@@ -0,0 +1,40 @@
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 { useEffect, useState } from "react";
18
+
19
+ export const useGetDomRoot = (
20
+ passedDomRoot: HTMLElement | undefined,
21
+ relativeElement: React.RefObject<HTMLDivElement> | null
22
+ ) => {
23
+ const [root, setRoot] = useState<HTMLElement>(document.body);
24
+
25
+ useEffect(() => {
26
+ if (relativeElement && relativeElement.current && !passedDomRoot) {
27
+ const closestBaseStylingWrapper = relativeElement.current.closest(".basestyling-wrapper");
28
+
29
+ if (closestBaseStylingWrapper) {
30
+ setRoot(closestBaseStylingWrapper as HTMLElement);
31
+ return;
32
+ }
33
+ } else if (passedDomRoot) {
34
+ setRoot(passedDomRoot);
35
+ return;
36
+ }
37
+ }, [relativeElement]);
38
+
39
+ return { root };
40
+ };
@@ -330,7 +330,8 @@ export const usePosition = (providedConfigObject: ConfigObject = defaultConfigOb
330
330
  };
331
331
 
332
332
  const calculatePosition = useDebouncedCallback(() => {
333
- if (!configObject.relativeElement?.current) return;
333
+ if (!configObject.relativeElement?.current || !configObject.elementToBePositioned?.current)
334
+ return;
334
335
  const relativeElRect = (configObject.relativeElement!
335
336
  .current as HTMLElement)!.getBoundingClientRect();
336
337
  const elementToBePositionedDimensions: Dimensions = {
@@ -101,7 +101,7 @@ describe("it should perform upload", () => {
101
101
 
102
102
  it("should contain a file with status of error", async () => {
103
103
  const response = { code: 404, body: { message: "Error test" } };
104
- const mock = setupXhrEnvironment([404, DONE, response]);
104
+ const mock = setupXhrEnvironment([404, DONE, JSON.stringify(response)]);
105
105
  const files3 = [
106
106
  {
107
107
  name: "test3.txt",
@@ -115,7 +115,7 @@ describe("it should perform upload", () => {
115
115
 
116
116
  it("should contain a file with status of server error", async () => {
117
117
  const response = { code: 500, body: { message: "Error test" } };
118
- const mock = setupXhrEnvironment([500, DONE, response]);
118
+ const mock = setupXhrEnvironment([500, DONE, JSON.stringify(response)]);
119
119
  const files6 = [
120
120
  {
121
121
  name: "test6.txt",
@@ -16,7 +16,7 @@
16
16
 
17
17
  import { FileType } from "../Form/FileUpload/FileUpload";
18
18
  import { useEffect, useState } from "react";
19
- import { getValueByPath } from "../util/helper";
19
+ import { getValueByPath, isJsonString } from "../util/helper";
20
20
 
21
21
  export interface UploadResponseType {
22
22
  fileList: FileType[];
@@ -76,6 +76,7 @@ export const useUploadFile = (
76
76
  ) => {
77
77
  let fileStatus: FileType["status"] = undefined;
78
78
  let error = "";
79
+ const response = responseText && isJsonString(responseText) && JSON.parse(responseText);
79
80
  if (requestStatus >= 200 && requestStatus < 400) {
80
81
  fileStatus = "completed";
81
82
  } else if (requestStatus === 0) {
@@ -83,13 +84,15 @@ export const useUploadFile = (
83
84
  error =
84
85
  networkErrorText || "Network error. Check internet connection and retry uploading the file";
85
86
  } else if (requestStatus >= 400 && requestStatus < 500) {
86
- const response = responseText && JSON.parse(JSON.stringify(responseText));
87
87
  fileStatus = "error";
88
- error = responseErrorPath ? getValueByPath(response, responseErrorPath) : "Bad request";
88
+ error =
89
+ responseErrorPath && response ? getValueByPath(response, responseErrorPath) : "Bad request";
89
90
  } else if (requestStatus >= 500) {
90
- const response = responseText && JSON.parse(JSON.stringify(responseText));
91
91
  fileStatus = "error";
92
- error = responseErrorPath ? getValueByPath(response, responseErrorPath) : "Server Error";
92
+ error =
93
+ responseErrorPath && response
94
+ ? getValueByPath(response, responseErrorPath)
95
+ : "Server Error";
93
96
  }
94
97
  return { fileStatus, error };
95
98
  };
@@ -24,7 +24,8 @@ import {
24
24
  throttle,
25
25
  areArraysDifferent,
26
26
  getValueByPath,
27
- isEqual
27
+ isEqual,
28
+ isJsonString
28
29
  } from "./helper";
29
30
  import { render } from "@testing-library/react";
30
31
 
@@ -337,3 +338,17 @@ describe("pixel to rem function works", () => {
337
338
  expect(result).toBe(px);
338
339
  });
339
340
  });
341
+
342
+ describe("isJsonString should work", () => {
343
+ it("should return true when parameter is a valid JSON string", () => {
344
+ const param = JSON.stringify({ test: 1 });
345
+
346
+ expect(isJsonString(param)).toEqual(true);
347
+ });
348
+
349
+ it("should return false when parameter is a valid JSON string", () => {
350
+ const param = { test: 2 };
351
+
352
+ expect(isJsonString(param)).toEqual(false);
353
+ });
354
+ });
@@ -181,3 +181,12 @@ export const getValueByPath = (obj: { [key: string]: any }, path: string): any =
181
181
  export const remToPx = (rem: number): number => {
182
182
  return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
183
183
  };
184
+
185
+ export const isJsonString = (str: any) => {
186
+ try {
187
+ JSON.parse(str);
188
+ } catch (e) {
189
+ return false;
190
+ }
191
+ return true;
192
+ };