From 6e0dca766921ab0b410a257997f199b50e20f59f Mon Sep 17 00:00:00 2001 From: Vinicius CZB Date: Sat, 11 Nov 2023 10:47:20 +0100 Subject: [PATCH] Minor improvements on Toaster --- src/packages/Toaster/index.tsx | 20 ++++++++++++-------- src/packages/Toaster/stories.tsx | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/packages/Toaster/index.tsx b/src/packages/Toaster/index.tsx index 4ca8377..a586609 100644 --- a/src/packages/Toaster/index.tsx +++ b/src/packages/Toaster/index.tsx @@ -10,39 +10,41 @@ export type ToasterProps = { duration?: number | null closable?: boolean initialVisible?: boolean + showProgress?: boolean } & Omit // Omit the 'header' prop from AlertProps const Toaster = ({ duration, closable = true, initialVisible = true, + showProgress = true, severity = 'success', onClose, ...props }: ToasterProps) => { const [isVisible, setIsVisible] = useState(initialVisible) + const handleClose = useCallback(() => { + setIsVisible(false) + onClose && onClose() + }, [onClose]) + useEffect(() => { if (typeof duration === 'number') { const timer = setTimeout(() => { - setIsVisible(false) + handleClose() }, duration) return () => { clearTimeout(timer) } } - }, [duration]) + }, [duration, handleClose]) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const theme = useContext(ThemeContext) - const handleClose = useCallback(() => { - setIsVisible(false) - onClose && onClose() - }, [onClose]) - if (!isVisible) { return null } @@ -55,7 +57,9 @@ const Toaster = ({ severity={severity} onClose={() => handleClose()} /> - + {showProgress && ( + + )} ) } diff --git a/src/packages/Toaster/stories.tsx b/src/packages/Toaster/stories.tsx index 57d6b52..bcfd9f1 100644 --- a/src/packages/Toaster/stories.tsx +++ b/src/packages/Toaster/stories.tsx @@ -9,7 +9,8 @@ export default { children: 'This is a toaster message', severity: 'info', showIcon: true, - closable: true + closable: true, + onClose: () => console.log('closed') } } as Meta