@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "9.2.0-beta.6",
3
+ "version": "9.2.0-beta.8",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,28 +1,59 @@
1
- import React, { useState } from "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 [toastOpen, setToastOpen] = useState(true);
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
- toastOpen={toastOpen}
10
- closeToastNotification={() => setToastOpen(false)}
11
- message="Successful"
12
- variant="success"
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 [toastOpen, setToastOpen] = useState(true);
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
- toastOpen={toastOpen}
23
- closeToastNotification={() => setToastOpen(false)}
24
- message="Errored"
25
- variant="error"
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;
@@ -15,4 +15,4 @@ export interface ToastNotificationProps {
15
15
  }
16
16
 
17
17
  export const ToastNotification: React.FC<Expand<ToastNotificationProps> &
18
- React.HTMLAttributes<HTMLElement>>;
18
+ React.HTMLAttributes<HTMLElement>>;
package/src/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as constants from "./constants";
2
+ import * as hooks from "./util/hooks";
2
3
  export * from "./components";
3
4
  export * from "./types/common";
4
- export { constants };
5
+ export { constants, hooks };
@@ -0,0 +1,6 @@
1
+ enum ToastVariants {
2
+ ERROR = "error",
3
+ SUCCESS = "success",
4
+ }
5
+
6
+ export default ToastVariants;
@@ -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 { formats, general, theme, useFocusInvalidInput, useOutsideClick };
8
+ export {
9
+ formats,
10
+ general,
11
+ theme,
12
+ useFocusInvalidInput,
13
+ useOutsideClick,
14
+ useToastNotification
15
+ };