@codacy/ui-components 0.61.7 → 0.61.9

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.
@@ -4,6 +4,8 @@ import { StyledToastContainer } from './styles';
4
4
  import { ErrorIcon, WarningIcon, InfoIcon, SuccessIcon } from '../Icons';
5
5
  import { toast as launchToast, ToastContainer as ToastifyContainer } from 'react-toastify';
6
6
  import { Subheader, Paragraph } from '../Typography';
7
+ import { hash } from '../utils/hash';
8
+ import { reactNodeToText } from '../utils/reactNodeToText';
7
9
  var DEFAULT_TOAST_TYPE = 'success';
8
10
  var TypeIcons = {
9
11
  "default": /*#__PURE__*/React.createElement(SuccessIcon, null),
@@ -13,10 +15,33 @@ var TypeIcons = {
13
15
  warning: /*#__PURE__*/React.createElement(WarningIcon, null)
14
16
  };
15
17
 
18
+ var getInnerToastText = function getInnerToastText(content) {
19
+ if (!content) return '';
20
+
21
+ switch (typeof content) {
22
+ case 'string':
23
+ return content;
24
+
25
+ case 'object':
26
+ if ('title' in content || 'description' in content) {
27
+ if (typeof content.description === 'object') {
28
+ return "".concat(content.title || '', " ").concat(reactNodeToText(content.description));
29
+ } else {
30
+ return "".concat(content.title || '', " ").concat(content.description);
31
+ }
32
+ }
33
+
34
+ return reactNodeToText(content);
35
+
36
+ default:
37
+ return '';
38
+ }
39
+ };
40
+
16
41
  var ToastContentWrapper = function ToastContentWrapper(_ref) {
17
42
  var content = _ref.content;
18
43
 
19
- if (typeof content === 'object' && 'description' in content) {
44
+ if (content && typeof content === 'object' && 'description' in content) {
20
45
  return /*#__PURE__*/React.createElement(React.Fragment, null, content.title && /*#__PURE__*/React.createElement(Subheader, {
21
46
  size: "lg",
22
47
  mb: 2
@@ -40,6 +65,7 @@ var useToast = function useToast(defaultOptions) {
40
65
  launchToast( /*#__PURE__*/React.createElement(ToastContentWrapper, {
41
66
  content: content
42
67
  }), _objectSpread(_objectSpread({}, ops), {}, {
68
+ toastId: hash(getInnerToastText(content)),
43
69
  icon: TypeIcons[(ops === null || ops === void 0 ? void 0 : ops.type) || DEFAULT_TOAST_TYPE],
44
70
  autoClose: (ops === null || ops === void 0 ? void 0 : ops.duration) === null ? false : ops === null || ops === void 0 ? void 0 : ops.duration
45
71
  }));
@@ -1,8 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { ToastOptions } from 'react-toastify';
3
- export declare type ToastContent = React.ReactChild | {
3
+ export declare type ToastContent = string | React.ReactNode | {
4
4
  title?: string;
5
- description: React.ReactChild;
5
+ description: React.ReactNode;
6
6
  };
7
7
  export interface ToastConfig extends Pick<ToastOptions, 'onClose' | 'onOpen' | 'toastId' | 'type' | 'closeOnClick'> {
8
8
  duration?: number | null;
@@ -0,0 +1 @@
1
+ export declare const hash: (s: string) => number;
@@ -0,0 +1,9 @@
1
+ export var hash = function hash(s) {
2
+ var h = 9;
3
+
4
+ for (var i = 0; i < s.length;) {
5
+ h = Math.imul(h ^ s.charCodeAt(i++), Math.pow(9, 9));
6
+ }
7
+
8
+ return h ^ h >>> 9;
9
+ };
@@ -0,0 +1,2 @@
1
+ import { ReactNode } from 'react';
2
+ export declare const reactNodeToText: (children: ReactNode | ReactNode[]) => string;
@@ -0,0 +1,38 @@
1
+ // From https://github.com/fernandopasik/react-children-utilities
2
+ import { Children, isValidElement } from 'react';
3
+
4
+ var hasChildren = function hasChildren(element) {
5
+ return isValidElement(element) && Boolean(element.props.children);
6
+ };
7
+
8
+ var childToString = function childToString(child) {
9
+ if (typeof child === 'undefined' || child === null || typeof child === 'boolean') {
10
+ return '';
11
+ }
12
+
13
+ if (JSON.stringify(child) === '{}') {
14
+ return '';
15
+ }
16
+
17
+ return child.toString();
18
+ };
19
+
20
+ export var reactNodeToText = function reactNodeToText(children) {
21
+ if (!(children instanceof Array) && !isValidElement(children)) {
22
+ return childToString(children);
23
+ }
24
+
25
+ return Children.toArray(children).reduce(function (text, child) {
26
+ var newText = '';
27
+
28
+ if (isValidElement(child) && hasChildren(child)) {
29
+ newText = reactNodeToText(child.props.children);
30
+ } else if (isValidElement(child) && !hasChildren(child)) {
31
+ newText = '';
32
+ } else {
33
+ newText = childToString(child);
34
+ }
35
+
36
+ return text.concat(newText);
37
+ }, '');
38
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codacy/ui-components",
3
- "version": "0.61.7",
3
+ "version": "0.61.9",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
@@ -94,7 +94,7 @@
94
94
  "react": "^18.2.0",
95
95
  "react-docgen-typescript-loader": "^3.7.2",
96
96
  "react-dom": "^18.2.0",
97
- "react-toastify": "^8.0.2",
97
+ "react-toastify": "8.0.2",
98
98
  "reakit": "^1.3.11",
99
99
  "rimraf": "^3.0.2",
100
100
  "styled-system": "^5.1.5",
@@ -108,9 +108,9 @@
108
108
  "@popperjs/core": "^2.5.4",
109
109
  "lodash": "^4.17.20",
110
110
  "numeral": "^2.0.6",
111
- "react": "=>17.0.2",
112
- "react-dom": "=>17.0.2",
113
- "react-toastify": "^8.0.2",
111
+ "react": "^17.0.0 || ^18.0.0",
112
+ "react-dom": "^17.0.0 || ^18.0.0",
113
+ "react-toastify": "8.0.2",
114
114
  "reakit": "^1.3.11",
115
115
  "styled-system": "^5.1.5",
116
116
  "typeface-clear-sans": "0.0.44"
@@ -132,5 +132,6 @@
132
132
  "*.md": [
133
133
  "prettier --write"
134
134
  ]
135
- }
135
+ },
136
+ "packageManager": "yarn@1.22.19"
136
137
  }