From 8e2382da580060824bb643a97cf2f5be0335c2f2 Mon Sep 17 00:00:00 2001 From: Fadi Date: Thu, 28 Jan 2021 20:15:51 +0100 Subject: [PATCH] fix #560 --- src/hooks/toastContainerReducer.ts | 4 +-- src/hooks/useToastContainer.ts | 51 ++++++++++++++---------------- src/utils/propValidator.ts | 2 +- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/hooks/toastContainerReducer.ts b/src/hooks/toastContainerReducer.ts index 2c0ea1af..b4991a8c 100644 --- a/src/hooks/toastContainerReducer.ts +++ b/src/hooks/toastContainerReducer.ts @@ -1,6 +1,6 @@ import { Id } from '../types'; -import { hasToastId } from '../utils'; +import { isToastIdValid } from '../utils'; export const enum ActionType { ADD, @@ -16,7 +16,7 @@ export function reducer(state: State, action: Action) { case ActionType.ADD: return [...state, action.toastId].filter(id => id !== action.staleId); case ActionType.REMOVE: - return hasToastId(action.toastId) + return isToastIdValid(action.toastId) ? state.filter(id => id !== action.toastId) : []; } diff --git a/src/hooks/useToastContainer.ts b/src/hooks/useToastContainer.ts index 651e9eda..c0a582ca 100644 --- a/src/hooks/useToastContainer.ts +++ b/src/hooks/useToastContainer.ts @@ -12,7 +12,7 @@ import { isFn, isNum, isStr, - hasToastId, + isToastIdValid, getAutoCloseDelay, Direction, Default @@ -106,36 +106,12 @@ export function useToastContainer(props: ToastContainerProps) { } function removeToast(toastId?: Id) { - const queueLen = queue.length; - toastCount = hasToastId(toastId) - ? toastCount - 1 - : toastCount - instance.displayedToast; - - if (toastCount < 0) toastCount = 0; - - if (queueLen > 0) { - const freeSlot = hasToastId(toastId) ? 1 : instance.props.limit!; - - if (queueLen === 1 || freeSlot === 1) { - instance.displayedToast++; - dequeueToast(); - } else { - const toDequeue = freeSlot > queueLen ? queueLen : freeSlot; - instance.displayedToast = toDequeue; - - for (let i = 0; i < toDequeue; i++) dequeueToast(); - } - } dispatch({ type: ActionType.REMOVE, toastId }); } function dequeueToast() { const { toastContent, toastProps, staleId } = queue.shift() as QueuedToast; - - // ensure that exit transition has been completed, hence the timeout - setTimeout(() => { - appendToast(toastContent, toastProps, staleId); - }, 500); + appendToast(toastContent, toastProps, staleId); } /** @@ -291,7 +267,28 @@ export function useToastContainer(props: ToastContainerProps) { function removeFromCollection(toastId: Id) { delete collection[toastId]; - forceUpdate(); + const queueLen = queue.length; + toastCount = isToastIdValid(toastId) + ? toastCount - 1 + : toastCount - instance.displayedToast; + + if (toastCount < 0) toastCount = 0; + + if (queueLen > 0) { + const freeSlot = isToastIdValid(toastId) ? 1 : instance.props.limit!; + + if (queueLen === 1 || freeSlot === 1) { + instance.displayedToast++; + dequeueToast(); + } else { + const toDequeue = freeSlot > queueLen ? queueLen : freeSlot; + instance.displayedToast = toDequeue; + + for (let i = 0; i < toDequeue; i++) dequeueToast(); + } + } else { + forceUpdate(); + } } function getToastToRender( diff --git a/src/utils/propValidator.ts b/src/utils/propValidator.ts index 58c2f64a..5ed69b3a 100644 --- a/src/utils/propValidator.ts +++ b/src/utils/propValidator.ts @@ -22,7 +22,7 @@ export function parseClassName(v: any) { return isStr(v) || isFn(v) ? v : null; } -export function hasToastId(toastId?: Id) { +export function isToastIdValid(toastId?: Id) { return toastId === 0 || toastId; }