@thecb/components 9.2.0-beta.6 → 9.2.0-beta.8
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.
- package/dist/index.cjs.js +49 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +96 -60
- package/dist/index.esm.js +49 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/molecules/toast-notification/ToastNotification.stories.js +43 -12
- package/src/components/molecules/toast-notification/index.d.ts +1 -1
- package/src/index.d.ts +2 -1
- package/src/types/common/ToastVariants.ts +6 -0
- package/src/types/common/index.ts +1 -0
- package/src/util/hooks/index.js +1 -0
- package/src/util/hooks/use-toast-notification/index.d.ts +23 -0
- package/src/util/hooks/use-toast-notification/index.js +38 -0
- package/src/util/index.js +9 -1
package/package.json
CHANGED
|
@@ -1,28 +1,59 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, { useEffect } from "react";
|
|
2
2
|
import ToastNotification from "./ToastNotification";
|
|
3
3
|
import page from "../../../../.storybook/page";
|
|
4
|
+
import { useToastNotification } from "../../../util";
|
|
5
|
+
import { ToastVariants } from "../../../types/common";
|
|
4
6
|
|
|
5
7
|
export const toastNotificationSuccess = () => {
|
|
6
|
-
const
|
|
8
|
+
const {
|
|
9
|
+
isToastOpen,
|
|
10
|
+
toastVariant,
|
|
11
|
+
toastMessage,
|
|
12
|
+
showToast,
|
|
13
|
+
hideToast
|
|
14
|
+
} = useToastNotification();
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
showToast({
|
|
18
|
+
message: "Success!",
|
|
19
|
+
variant: ToastVariants.SUCCESS
|
|
20
|
+
});
|
|
21
|
+
}, []);
|
|
22
|
+
|
|
7
23
|
return (
|
|
8
24
|
<ToastNotification
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
25
|
+
variant={toastVariant}
|
|
26
|
+
message={toastMessage}
|
|
27
|
+
toastOpen={isToastOpen}
|
|
28
|
+
closeToastNotification={() => hideToast()}
|
|
13
29
|
/>
|
|
14
30
|
);
|
|
15
31
|
};
|
|
32
|
+
|
|
16
33
|
toastNotificationSuccess.storyName = "Success Toast";
|
|
17
34
|
|
|
18
35
|
export const toastNotificationError = () => {
|
|
19
|
-
const
|
|
36
|
+
const {
|
|
37
|
+
isToastOpen,
|
|
38
|
+
toastVariant,
|
|
39
|
+
toastMessage,
|
|
40
|
+
showToast,
|
|
41
|
+
hideToast,
|
|
42
|
+
} = useToastNotification();
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
showToast({
|
|
46
|
+
message: "An error occurred",
|
|
47
|
+
variant: ToastVariants.ERROR,
|
|
48
|
+
});
|
|
49
|
+
}, []);
|
|
50
|
+
|
|
20
51
|
return (
|
|
21
52
|
<ToastNotification
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
53
|
+
variant={toastVariant}
|
|
54
|
+
message={toastMessage}
|
|
55
|
+
toastOpen={isToastOpen}
|
|
56
|
+
closeToastNotification={() => hideToast()}
|
|
26
57
|
/>
|
|
27
58
|
);
|
|
28
59
|
};
|
|
@@ -30,7 +61,7 @@ toastNotificationError.storyName = "Error Toast";
|
|
|
30
61
|
|
|
31
62
|
const story = page({
|
|
32
63
|
title: "Components|Molecules/ToastNotification",
|
|
33
|
-
Component: ToastNotification
|
|
64
|
+
Component: ToastNotification,
|
|
34
65
|
});
|
|
35
66
|
|
|
36
67
|
export default story;
|
package/src/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { default as FieldActions } from "./FieldActions";
|
|
|
4
4
|
export { default as FormSelectOption } from "./FormSelectOption";
|
|
5
5
|
export { default as SearchableSelectOption } from "./SearchableSelectOption";
|
|
6
6
|
export { default as ErrorMessageDictionary } from "./ErrorMessageDictionary";
|
|
7
|
+
export { default as ToastVariants } from "./ToastVariants";
|
|
7
8
|
export * from "./FieldActions";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useToastNotification } from "./use-toast-notification";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ToastVariants } from "../../../types/common";
|
|
2
|
+
|
|
3
|
+
export interface UseToastProps {
|
|
4
|
+
timeout?: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface UseToastResult {
|
|
8
|
+
isToastOpen: boolean;
|
|
9
|
+
toastVariant: "" | ToastVariants;
|
|
10
|
+
toastMessage: string;
|
|
11
|
+
showToast: ({
|
|
12
|
+
message,
|
|
13
|
+
variant,
|
|
14
|
+
}: {
|
|
15
|
+
message: string;
|
|
16
|
+
variant: ToastVariants;
|
|
17
|
+
}) => void;
|
|
18
|
+
hideToast: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function useToastNotification({
|
|
22
|
+
timeout,
|
|
23
|
+
}: UseToastProps): UseToastResult;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
const initialToastState = {
|
|
4
|
+
isOpen: false,
|
|
5
|
+
variant: "",
|
|
6
|
+
message: ""
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const useToastNotification = ({ timeout = 5000 } = {}) => {
|
|
10
|
+
const [toastState, setToastState] = useState(initialToastState);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (toastState.isOpen) {
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
setToastState(initialToastState);
|
|
16
|
+
}, timeout);
|
|
17
|
+
}
|
|
18
|
+
}, [timeout, toastState.isOpen]);
|
|
19
|
+
|
|
20
|
+
const showToast = ({ message, variant }) =>
|
|
21
|
+
setToastState({
|
|
22
|
+
isOpen: true,
|
|
23
|
+
variant,
|
|
24
|
+
message
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const hideToast = () => setToastState(initialToastState);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
isToastOpen: toastState.isOpen,
|
|
31
|
+
toastVariant: toastState.variant,
|
|
32
|
+
toastMessage: toastState.message,
|
|
33
|
+
showToast,
|
|
34
|
+
hideToast
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default useToastNotification;
|
package/src/util/index.js
CHANGED
|
@@ -3,5 +3,13 @@ import * as general from "./general";
|
|
|
3
3
|
import * as theme from "./themeUtils";
|
|
4
4
|
import useFocusInvalidInput from "./focusFirstInvalidInputHook";
|
|
5
5
|
import useOutsideClick from "./useOutsideClick";
|
|
6
|
+
import useToastNotification from "./hooks/use-toast-notification";
|
|
6
7
|
|
|
7
|
-
export {
|
|
8
|
+
export {
|
|
9
|
+
formats,
|
|
10
|
+
general,
|
|
11
|
+
theme,
|
|
12
|
+
useFocusInvalidInput,
|
|
13
|
+
useOutsideClick,
|
|
14
|
+
useToastNotification
|
|
15
|
+
};
|