@thecb/components 9.2.0-beta.5 → 9.2.0-beta.7

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.5",
3
+ "version": "9.2.0-beta.7",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -19,15 +19,15 @@ const VARIANTS = {
19
19
  };
20
20
 
21
21
  const ToastNotification = ({
22
+ variant = VARIANTS.SUCCESS,
22
23
  message = "",
24
+ toastOpen,
25
+ closeToastNotification,
23
26
  extraStyles,
24
27
  minWidth = "112px",
25
28
  maxWidth = "350px",
26
29
  height = "56px",
27
30
  childGap = "1rem",
28
- closeToastNotification,
29
- toastOpen,
30
- variant = VARIANTS.SUCCESS,
31
31
  backgroundColor
32
32
  }) => (
33
33
  <Box
@@ -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;
@@ -2,7 +2,17 @@ import React from "react";
2
2
  import Expand from "../../../util/expand";
3
3
 
4
4
  export interface ToastNotificationProps {
5
+ variant?: string;
6
+ message: string;
7
+ toastOpen: boolean;
8
+ closeToastNotification: (event?: React.MouseEvent<HTMLElement>) => void;
9
+ extraStyles?: string;
10
+ minWidth?: string;
11
+ maxWidth?: string;
12
+ height?: string;
13
+ childGap?: string;
14
+ backgroundColor?: string;
5
15
  }
6
16
 
7
17
  export const ToastNotification: React.FC<Expand<ToastNotificationProps> &
8
- React.HTMLAttributes<HTMLElement>>;
18
+ React.HTMLAttributes<HTMLElement>>;
@@ -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";
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 "./useToastNotification";
6
7
 
7
- export { formats, general, theme, useFocusInvalidInput, useOutsideClick };
8
+ export {
9
+ formats,
10
+ general,
11
+ theme,
12
+ useFocusInvalidInput,
13
+ useOutsideClick,
14
+ useToastNotification
15
+ };
@@ -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 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;