From 3afe7efcb3c8bba8776934ee1ba193d4b1b1ce3b Mon Sep 17 00:00:00 2001 From: Fadi Khadra Date: Tue, 1 Nov 2022 08:51:07 +0100 Subject: [PATCH] remove isNil util --- src/components/Toast.tsx | 4 ++-- src/core/toast.ts | 6 +++--- src/hooks/useToastContainer.ts | 20 ++++++++++---------- src/utils/propValidator.ts | 2 -- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/components/Toast.tsx b/src/components/Toast.tsx index 2310da00..cfecf945 100644 --- a/src/components/Toast.tsx +++ b/src/components/Toast.tsx @@ -4,7 +4,7 @@ import cx from 'clsx'; import { ProgressBar } from './ProgressBar'; import { CloseButton } from './CloseButton'; import { ToastProps } from '../types'; -import { Default, isFn, isNil } from '../utils'; +import { Default, isFn } from '../utils'; import { useToast } from '../hooks/useToast'; export const Toast: React.FC = props => { @@ -97,7 +97,7 @@ export const Toast: React.FC = props => { } style={bodyStyle} > - {!isNil(iconOut) && ( + {iconOut != null && (
( ) => { // Remove the toast if the input has not been provided. This prevents the toast from hanging // in the pending state if a success/error toast has not been provided. - if (isNil(input)) { + if (input == null) { toast.dismiss(id); return; } @@ -208,7 +208,7 @@ toast.dismiss = (id?: Id) => { if (containers.size > 0) { eventManager.emit(Event.Clear, id); } else { - queue = queue.filter(t => !isNil(id) && t.options.toastId !== id); + queue = queue.filter(t => id != null && t.options.toastId !== id); } }; diff --git a/src/hooks/useToastContainer.ts b/src/hooks/useToastContainer.ts index 0d566fc7..52c19ca6 100644 --- a/src/hooks/useToastContainer.ts +++ b/src/hooks/useToastContainer.ts @@ -14,8 +14,7 @@ import { isNum, isStr, getAutoCloseDelay, - toToastItem, - isNil + toToastItem } from '../utils'; import { eventManager, Event } from '../core/eventManager'; @@ -97,7 +96,7 @@ export function useToastContainer(props: ToastContainerProps) { function removeToast(toastId?: Id) { setToastIds(state => - isNil(toastId) ? [] : state.filter(id => id !== toastId) + toastId == null ? [] : state.filter(id => id !== toastId) ); } @@ -117,7 +116,7 @@ export function useToastContainer(props: ToastContainerProps) { !containerRef.current || (instance.props.enableMultiContainer && options.containerId !== instance.props.containerId) || - (toastToRender.has(options.toastId) && isNil(options.updateId)) + (toastToRender.has(options.toastId) && options.updateId == null) ); } @@ -131,7 +130,7 @@ export function useToastContainer(props: ToastContainerProps) { const { toastId, updateId, data } = options; const { props } = instance; const closeToast = () => removeToast(toastId); - const isNotAnUpdate = isNil(updateId); + const isNotAnUpdate = updateId == null; if (isNotAnUpdate) instance.count++; @@ -161,14 +160,15 @@ export function useToastContainer(props: ToastContainerProps) { eventManager.emit(Event.Change, removed); const queueLen = instance.queue.length; - instance.count = isNil(toastId) - ? instance.count - instance.displayedToast - : instance.count - 1; + instance.count = + toastId == null + ? instance.count - instance.displayedToast + : instance.count - 1; if (instance.count < 0) instance.count = 0; if (queueLen > 0) { - const freeSlot = isNil(toastId) ? instance.props.limit! : 1; + const freeSlot = toastId == null ? instance.props.limit! : 1; if (queueLen === 1 || freeSlot === 1) { instance.displayedToast++; @@ -247,7 +247,7 @@ export function useToastContainer(props: ToastContainerProps) { setToastIds(state => [...state, toastId].filter(id => id !== staleId)); eventManager.emit( Event.Change, - toToastItem(toast, isNil(toast.props.updateId) ? 'added' : 'updated') + toToastItem(toast, toast.props.updateId == null ? 'added' : 'updated') ); } diff --git a/src/utils/propValidator.ts b/src/utils/propValidator.ts index c81de53a..aaec82d2 100644 --- a/src/utils/propValidator.ts +++ b/src/utils/propValidator.ts @@ -9,8 +9,6 @@ export const isFn = (v: any): v is Function => typeof v === 'function'; export const parseClassName = (v: any) => (isStr(v) || isFn(v) ? v : null); -export const isNil = (v: any) => v == null; - export const getAutoCloseDelay = ( toastAutoClose?: false | number, containerAutoClose?: false | number