@canonical/react-components 2.12.0 → 2.14.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.
@@ -1,3 +1,3 @@
1
1
  export { NotificationConsumer, NotificationProvider, useNotify, } from "./NotificationProvider";
2
- export { info, success, failure, queue } from "./messageBuilder";
2
+ export { info, success, failure, caution, queue } from "./messageBuilder";
3
3
  export type { NotificationAction, NotificationType, QueuedNotification, NotificationHelper, } from "./types";
@@ -15,6 +15,12 @@ Object.defineProperty(exports, "NotificationProvider", {
15
15
  return _NotificationProvider.NotificationProvider;
16
16
  }
17
17
  });
18
+ Object.defineProperty(exports, "caution", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _messageBuilder.caution;
22
+ }
23
+ });
18
24
  Object.defineProperty(exports, "failure", {
19
25
  enumerable: true,
20
26
  get: function () {
@@ -1,6 +1,8 @@
1
1
  import { NotificationAction, NotificationType, QueuedNotification } from "./types";
2
- import { ReactNode } from "react";
2
+ import React, { ReactNode } from "react";
3
3
  export declare const queue: (notification: NotificationType) => QueuedNotification;
4
- export declare const info: (message: ReactNode, title?: string) => NotificationType;
5
- export declare const success: (message: ReactNode, title?: string) => NotificationType;
4
+ export declare const info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
5
+ export declare const success: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
6
6
  export declare const failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => NotificationType;
7
+ export declare const caution: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
8
+ export declare const formatErrorMessage: (message?: ReactNode, error?: unknown) => string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactPortal | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode>> | React.JSX.Element;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.success = exports.queue = exports.info = exports.failure = void 0;
6
+ exports.success = exports.queue = exports.info = exports.formatErrorMessage = exports.failure = exports.caution = void 0;
7
7
  var _react = _interopRequireDefault(require("react"));
8
8
  var _Notifications = require("../Notifications");
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -15,18 +15,20 @@ const queue = notification => {
15
15
  };
16
16
  };
17
17
  exports.queue = queue;
18
- const info = (message, title) => {
18
+ const info = (message, title, actions) => {
19
19
  return {
20
20
  message,
21
21
  title,
22
+ actions,
22
23
  type: _Notifications.NotificationSeverity.INFORMATION
23
24
  };
24
25
  };
25
26
  exports.info = info;
26
- const success = (message, title) => {
27
+ const success = (message, title, actions) => {
27
28
  return {
28
29
  message,
29
30
  title,
31
+ actions,
30
32
  type: _Notifications.NotificationSeverity.POSITIVE
31
33
  };
32
34
  };
@@ -34,9 +36,26 @@ exports.success = success;
34
36
  const failure = (title, error, message, actions) => {
35
37
  return {
36
38
  actions,
37
- message: error && error instanceof Error ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, message, " ", error.message) : message,
39
+ message: formatErrorMessage(message, error),
38
40
  title,
39
41
  type: _Notifications.NotificationSeverity.NEGATIVE
40
42
  };
41
43
  };
42
- exports.failure = failure;
44
+ exports.failure = failure;
45
+ const caution = (message, title, actions) => {
46
+ return {
47
+ message,
48
+ actions,
49
+ title,
50
+ type: _Notifications.NotificationSeverity.CAUTION
51
+ };
52
+ };
53
+ exports.caution = caution;
54
+ const formatErrorMessage = (message, error) => {
55
+ if (error && error instanceof Error) {
56
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, message, " ", error.message);
57
+ } else {
58
+ return message;
59
+ }
60
+ };
61
+ exports.formatErrorMessage = formatErrorMessage;
@@ -60,6 +60,8 @@ const PreloadedList = () => {
60
60
  onClick: () => console.log("Retry clicked")
61
61
  }])
62
62
  }, "Add Error"), /*#__PURE__*/_react.default.createElement(_Button.default, {
63
+ onClick: () => toastNotify.caution("You have unsaved changes")
64
+ }, "Add Warning"), /*#__PURE__*/_react.default.createElement(_Button.default, {
63
65
  onClick: toastNotify.toggleListView
64
66
  }, "Toggle List View"));
65
67
  };
@@ -80,7 +80,7 @@ const ToastNotificationList = _ref => {
80
80
  setFilters(new Set());
81
81
  return;
82
82
  }
83
- onDismiss();
83
+ onDismiss(notifications);
84
84
  };
85
85
  const getSeverityFilters = () => {
86
86
  const filterButtons = severityOrder.map(severity => {
@@ -6,12 +6,15 @@ export type ToastNotificationType = NotificationType & {
6
6
  timestamp?: ReactNode;
7
7
  id: string;
8
8
  };
9
+ interface Props {
10
+ onDismiss?: (notifications?: ToastNotificationType[]) => void;
11
+ }
9
12
  interface ToastNotificationHelper {
10
13
  notifications: ToastNotificationType[];
11
- success: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
12
- info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => ToastNotificationType;
13
- failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => ToastNotificationType;
14
- caution: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
14
+ success: (message: ReactNode, actions?: NotificationAction[], title?: string, id?: ToastNotificationType["id"]) => ToastNotificationType;
15
+ info: (message: ReactNode, title?: string, actions?: NotificationAction[], id?: ToastNotificationType["id"]) => ToastNotificationType;
16
+ failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[], id?: ToastNotificationType["id"]) => ToastNotificationType;
17
+ caution: (message: ReactNode, actions?: NotificationAction[], title?: string, id?: ToastNotificationType["id"]) => ToastNotificationType;
15
18
  clear: (notification?: ToastNotificationType[]) => void;
16
19
  toggleListView: () => void;
17
20
  isListView: boolean;
@@ -81,6 +84,6 @@ console.log(count.positive);
81
84
 
82
85
  Alternatively, you can use the `ToastNotification` and `ToastNotificationList` components directly, without using the provider.
83
86
  */
84
- declare const ToastNotificationProvider: FC<PropsWithChildren>;
87
+ declare const ToastNotificationProvider: FC<PropsWithChildren<Props>>;
85
88
  export default ToastNotificationProvider;
86
89
  export declare const useToastNotification: () => ToastNotificationHelper;
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.useToastNotification = exports.default = void 0;
7
7
  var _NotificationProvider = require("../../NotificationProvider");
8
- var _ = require("./..");
9
8
  var _ToastNotification = _interopRequireDefault(require("./ToastNotification"));
10
9
  var _ToastNotificationList = _interopRequireDefault(require("./ToastNotificationList"));
11
10
  var _react = _interopRequireWildcard(require("react"));
@@ -103,7 +102,8 @@ Alternatively, you can use the `ToastNotification` and `ToastNotificationList` c
103
102
 
104
103
  const ToastNotificationProvider = _ref => {
105
104
  let {
106
- children
105
+ children,
106
+ onDismiss
107
107
  } = _ref;
108
108
  const [notifications, setNotifications] = (0, _react.useState)([]);
109
109
  const [showList, setShowList] = (0, _react.useState)(false);
@@ -140,10 +140,11 @@ const ToastNotificationProvider = _ref => {
140
140
  });
141
141
  };
142
142
  const addNotification = notification => {
143
+ var _notification$id;
143
144
  const notificationToAdd = {
144
145
  ...notification,
145
146
  timestamp: new Date().toLocaleString(),
146
- id: Date.now().toString() + (Math.random() + 1).toString(36).substring(7)
147
+ id: (_notification$id = notification.id) !== null && _notification$id !== void 0 ? _notification$id : Date.now().toString() + (Math.random() + 1).toString(36).substring(7)
147
148
  };
148
149
  setNotifications(prev => {
149
150
  return [...prev, notificationToAdd];
@@ -152,6 +153,9 @@ const ToastNotificationProvider = _ref => {
152
153
  return notificationToAdd;
153
154
  };
154
155
  const clear = notifications => {
156
+ if (onDismiss) {
157
+ onDismiss(notifications);
158
+ }
155
159
  if (!notifications) {
156
160
  setNotifications([]);
157
161
  setShowList(false);
@@ -186,24 +190,21 @@ const ToastNotificationProvider = _ref => {
186
190
  });
187
191
  const helper = {
188
192
  notifications,
189
- failure: (title, error, message, actions) => addNotification((0, _NotificationProvider.failure)(title, error, message, actions)),
190
- info: (message, title, actions) => addNotification({
191
- message,
192
- actions,
193
- title,
194
- type: _.NotificationSeverity.INFORMATION
193
+ failure: (title, error, message, actions, id) => addNotification({
194
+ ...(0, _NotificationProvider.failure)(title, error, message, actions),
195
+ id
196
+ }),
197
+ info: (message, title, actions, id) => addNotification({
198
+ ...(0, _NotificationProvider.info)(message, title, actions),
199
+ id
195
200
  }),
196
- success: (message, actions, title) => addNotification({
197
- message,
198
- actions,
199
- title,
200
- type: _.NotificationSeverity.POSITIVE
201
+ success: (message, actions, title, id) => addNotification({
202
+ ...(0, _NotificationProvider.success)(message, title, actions),
203
+ id
201
204
  }),
202
- caution: (message, actions, title) => addNotification({
203
- message,
204
- actions,
205
- title,
206
- type: _.NotificationSeverity.CAUTION
205
+ caution: (message, actions, title, id) => addNotification({
206
+ ...(0, _NotificationProvider.caution)(message, title, actions),
207
+ id
207
208
  }),
208
209
  clear,
209
210
  toggleListView,
@@ -1,3 +1,3 @@
1
1
  export { NotificationConsumer, NotificationProvider, useNotify, } from "./NotificationProvider";
2
- export { info, success, failure, queue } from "./messageBuilder";
2
+ export { info, success, failure, caution, queue } from "./messageBuilder";
3
3
  export type { NotificationAction, NotificationType, QueuedNotification, NotificationHelper, } from "./types";
@@ -1,2 +1,2 @@
1
1
  export { NotificationConsumer, NotificationProvider, useNotify } from "./NotificationProvider";
2
- export { info, success, failure, queue } from "./messageBuilder";
2
+ export { info, success, failure, caution, queue } from "./messageBuilder";
@@ -1,6 +1,8 @@
1
1
  import { NotificationAction, NotificationType, QueuedNotification } from "./types";
2
- import { ReactNode } from "react";
2
+ import React, { ReactNode } from "react";
3
3
  export declare const queue: (notification: NotificationType) => QueuedNotification;
4
- export declare const info: (message: ReactNode, title?: string) => NotificationType;
5
- export declare const success: (message: ReactNode, title?: string) => NotificationType;
4
+ export declare const info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
5
+ export declare const success: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
6
6
  export declare const failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => NotificationType;
7
+ export declare const caution: (message: ReactNode, title?: string, actions?: NotificationAction[]) => NotificationType;
8
+ export declare const formatErrorMessage: (message?: ReactNode, error?: unknown) => string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactPortal | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode>> | React.JSX.Element;
@@ -7,25 +7,42 @@ export var queue = notification => {
7
7
  }
8
8
  };
9
9
  };
10
- export var info = (message, title) => {
10
+ export var info = (message, title, actions) => {
11
11
  return {
12
12
  message,
13
13
  title,
14
+ actions,
14
15
  type: NotificationSeverity.INFORMATION
15
16
  };
16
17
  };
17
- export var success = (message, title) => {
18
+ export var success = (message, title, actions) => {
18
19
  return {
19
20
  message,
20
21
  title,
22
+ actions,
21
23
  type: NotificationSeverity.POSITIVE
22
24
  };
23
25
  };
24
26
  export var failure = (title, error, message, actions) => {
25
27
  return {
26
28
  actions,
27
- message: error && error instanceof Error ? /*#__PURE__*/React.createElement(React.Fragment, null, message, " ", error.message) : message,
29
+ message: formatErrorMessage(message, error),
28
30
  title,
29
31
  type: NotificationSeverity.NEGATIVE
30
32
  };
33
+ };
34
+ export var caution = (message, title, actions) => {
35
+ return {
36
+ message,
37
+ actions,
38
+ title,
39
+ type: NotificationSeverity.CAUTION
40
+ };
41
+ };
42
+ export var formatErrorMessage = (message, error) => {
43
+ if (error && error instanceof Error) {
44
+ return /*#__PURE__*/React.createElement(React.Fragment, null, message, " ", error.message);
45
+ } else {
46
+ return message;
47
+ }
31
48
  };
@@ -51,6 +51,8 @@ var PreloadedList = () => {
51
51
  onClick: () => console.log("Retry clicked")
52
52
  }])
53
53
  }, "Add Error"), /*#__PURE__*/React.createElement(Button, {
54
+ onClick: () => toastNotify.caution("You have unsaved changes")
55
+ }, "Add Warning"), /*#__PURE__*/React.createElement(Button, {
54
56
  onClick: toastNotify.toggleListView
55
57
  }, "Toggle List View"));
56
58
  };
@@ -73,7 +73,7 @@ var ToastNotificationList = _ref => {
73
73
  setFilters(new Set());
74
74
  return;
75
75
  }
76
- onDismiss();
76
+ onDismiss(notifications);
77
77
  };
78
78
  var getSeverityFilters = () => {
79
79
  var filterButtons = severityOrder.map(severity => {
@@ -6,12 +6,15 @@ export type ToastNotificationType = NotificationType & {
6
6
  timestamp?: ReactNode;
7
7
  id: string;
8
8
  };
9
+ interface Props {
10
+ onDismiss?: (notifications?: ToastNotificationType[]) => void;
11
+ }
9
12
  interface ToastNotificationHelper {
10
13
  notifications: ToastNotificationType[];
11
- success: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
12
- info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => ToastNotificationType;
13
- failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => ToastNotificationType;
14
- caution: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
14
+ success: (message: ReactNode, actions?: NotificationAction[], title?: string, id?: ToastNotificationType["id"]) => ToastNotificationType;
15
+ info: (message: ReactNode, title?: string, actions?: NotificationAction[], id?: ToastNotificationType["id"]) => ToastNotificationType;
16
+ failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[], id?: ToastNotificationType["id"]) => ToastNotificationType;
17
+ caution: (message: ReactNode, actions?: NotificationAction[], title?: string, id?: ToastNotificationType["id"]) => ToastNotificationType;
15
18
  clear: (notification?: ToastNotificationType[]) => void;
16
19
  toggleListView: () => void;
17
20
  isListView: boolean;
@@ -81,6 +84,6 @@ console.log(count.positive);
81
84
 
82
85
  Alternatively, you can use the `ToastNotification` and `ToastNotificationList` components directly, without using the provider.
83
86
  */
84
- declare const ToastNotificationProvider: FC<PropsWithChildren>;
87
+ declare const ToastNotificationProvider: FC<PropsWithChildren<Props>>;
85
88
  export default ToastNotificationProvider;
86
89
  export declare const useToastNotification: () => ToastNotificationHelper;
@@ -3,8 +3,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
3
3
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
4
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
5
5
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6
- import { failure as _failure } from "../../NotificationProvider";
7
- import { NotificationSeverity } from "./..";
6
+ import { failure as _failure, info as _info, caution as _caution, success as _success } from "../../NotificationProvider";
8
7
  import ToastNotification from "./ToastNotification";
9
8
  import ToastNotificationList from "./ToastNotificationList";
10
9
  import { createContext, useContext, useEffect, useState } from "react";
@@ -100,7 +99,8 @@ Alternatively, you can use the `ToastNotification` and `ToastNotificationList` c
100
99
 
101
100
  var ToastNotificationProvider = _ref => {
102
101
  var {
103
- children
102
+ children,
103
+ onDismiss
104
104
  } = _ref;
105
105
  var [notifications, setNotifications] = useState([]);
106
106
  var [showList, setShowList] = useState(false);
@@ -137,9 +137,10 @@ var ToastNotificationProvider = _ref => {
137
137
  });
138
138
  };
139
139
  var addNotification = notification => {
140
+ var _notification$id;
140
141
  var notificationToAdd = _objectSpread(_objectSpread({}, notification), {}, {
141
142
  timestamp: new Date().toLocaleString(),
142
- id: Date.now().toString() + (Math.random() + 1).toString(36).substring(7)
143
+ id: (_notification$id = notification.id) !== null && _notification$id !== void 0 ? _notification$id : Date.now().toString() + (Math.random() + 1).toString(36).substring(7)
143
144
  });
144
145
  setNotifications(prev => {
145
146
  return [...prev, notificationToAdd];
@@ -148,6 +149,9 @@ var ToastNotificationProvider = _ref => {
148
149
  return notificationToAdd;
149
150
  };
150
151
  var clear = notifications => {
152
+ if (onDismiss) {
153
+ onDismiss(notifications);
154
+ }
151
155
  if (!notifications) {
152
156
  setNotifications([]);
153
157
  setShowList(false);
@@ -182,25 +186,18 @@ var ToastNotificationProvider = _ref => {
182
186
  });
183
187
  var helper = {
184
188
  notifications,
185
- failure: (title, error, message, actions) => addNotification(_failure(title, error, message, actions)),
186
- info: (message, title, actions) => addNotification({
187
- message,
188
- actions,
189
- title,
190
- type: NotificationSeverity.INFORMATION
191
- }),
192
- success: (message, actions, title) => addNotification({
193
- message,
194
- actions,
195
- title,
196
- type: NotificationSeverity.POSITIVE
197
- }),
198
- caution: (message, actions, title) => addNotification({
199
- message,
200
- actions,
201
- title,
202
- type: NotificationSeverity.CAUTION
203
- }),
189
+ failure: (title, error, message, actions, id) => addNotification(_objectSpread(_objectSpread({}, _failure(title, error, message, actions)), {}, {
190
+ id
191
+ })),
192
+ info: (message, title, actions, id) => addNotification(_objectSpread(_objectSpread({}, _info(message, title, actions)), {}, {
193
+ id
194
+ })),
195
+ success: (message, actions, title, id) => addNotification(_objectSpread(_objectSpread({}, _success(message, title, actions)), {}, {
196
+ id
197
+ })),
198
+ caution: (message, actions, title, id) => addNotification(_objectSpread(_objectSpread({}, _caution(message, title, actions)), {}, {
199
+ id
200
+ })),
204
201
  clear,
205
202
  toggleListView,
206
203
  isListView: showList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "2.12.0",
3
+ "version": "2.14.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {
@@ -93,7 +93,7 @@
93
93
  "tsc-alias": "1.8.10",
94
94
  "typescript": "5.7.3",
95
95
  "typescript-eslint": "8.24.1",
96
- "vanilla-framework": "4.26.1",
96
+ "vanilla-framework": "4.28.0",
97
97
  "wait-on": "8.0.2",
98
98
  "webpack": "5.98.0"
99
99
  },