@canonical/react-components 2.13.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
  };
@@ -11,10 +11,10 @@ interface Props {
11
11
  }
12
12
  interface ToastNotificationHelper {
13
13
  notifications: ToastNotificationType[];
14
- success: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
15
- info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => ToastNotificationType;
16
- failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => ToastNotificationType;
17
- 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;
18
18
  clear: (notification?: ToastNotificationType[]) => void;
19
19
  toggleListView: () => void;
20
20
  isListView: boolean;
@@ -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"));
@@ -141,10 +140,11 @@ const ToastNotificationProvider = _ref => {
141
140
  });
142
141
  };
143
142
  const addNotification = notification => {
143
+ var _notification$id;
144
144
  const notificationToAdd = {
145
145
  ...notification,
146
146
  timestamp: new Date().toLocaleString(),
147
- 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)
148
148
  };
149
149
  setNotifications(prev => {
150
150
  return [...prev, notificationToAdd];
@@ -190,24 +190,21 @@ const ToastNotificationProvider = _ref => {
190
190
  });
191
191
  const helper = {
192
192
  notifications,
193
- failure: (title, error, message, actions) => addNotification((0, _NotificationProvider.failure)(title, error, message, actions)),
194
- info: (message, title, actions) => addNotification({
195
- message,
196
- actions,
197
- title,
198
- type: _.NotificationSeverity.INFORMATION
193
+ failure: (title, error, message, actions, id) => addNotification({
194
+ ...(0, _NotificationProvider.failure)(title, error, message, actions),
195
+ id
199
196
  }),
200
- success: (message, actions, title) => addNotification({
201
- message,
202
- actions,
203
- title,
204
- type: _.NotificationSeverity.POSITIVE
197
+ info: (message, title, actions, id) => addNotification({
198
+ ...(0, _NotificationProvider.info)(message, title, actions),
199
+ id
205
200
  }),
206
- caution: (message, actions, title) => addNotification({
207
- message,
208
- actions,
209
- title,
210
- type: _.NotificationSeverity.CAUTION
201
+ success: (message, actions, title, id) => addNotification({
202
+ ...(0, _NotificationProvider.success)(message, title, actions),
203
+ id
204
+ }),
205
+ caution: (message, actions, title, id) => addNotification({
206
+ ...(0, _NotificationProvider.caution)(message, title, actions),
207
+ id
211
208
  }),
212
209
  clear,
213
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
  };
@@ -11,10 +11,10 @@ interface Props {
11
11
  }
12
12
  interface ToastNotificationHelper {
13
13
  notifications: ToastNotificationType[];
14
- success: (message: ReactNode, actions?: NotificationAction[], title?: string) => ToastNotificationType;
15
- info: (message: ReactNode, title?: string, actions?: NotificationAction[]) => ToastNotificationType;
16
- failure: (title: string, error: unknown, message?: ReactNode, actions?: NotificationAction[]) => ToastNotificationType;
17
- 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;
18
18
  clear: (notification?: ToastNotificationType[]) => void;
19
19
  toggleListView: () => void;
20
20
  isListView: boolean;
@@ -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";
@@ -138,9 +137,10 @@ var ToastNotificationProvider = _ref => {
138
137
  });
139
138
  };
140
139
  var addNotification = notification => {
140
+ var _notification$id;
141
141
  var notificationToAdd = _objectSpread(_objectSpread({}, notification), {}, {
142
142
  timestamp: new Date().toLocaleString(),
143
- 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)
144
144
  });
145
145
  setNotifications(prev => {
146
146
  return [...prev, notificationToAdd];
@@ -186,25 +186,18 @@ var ToastNotificationProvider = _ref => {
186
186
  });
187
187
  var helper = {
188
188
  notifications,
189
- failure: (title, error, message, actions) => addNotification(_failure(title, error, message, actions)),
190
- info: (message, title, actions) => addNotification({
191
- message,
192
- actions,
193
- title,
194
- type: NotificationSeverity.INFORMATION
195
- }),
196
- success: (message, actions, title) => addNotification({
197
- message,
198
- actions,
199
- title,
200
- type: NotificationSeverity.POSITIVE
201
- }),
202
- caution: (message, actions, title) => addNotification({
203
- message,
204
- actions,
205
- title,
206
- type: NotificationSeverity.CAUTION
207
- }),
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
+ })),
208
201
  clear,
209
202
  toggleListView,
210
203
  isListView: showList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "2.13.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
  },