From 53f8a73b2f24a348c87e16bf5eb41d015ace5ad3 Mon Sep 17 00:00:00 2001 From: Fadi Khadra Date: Fri, 3 Nov 2023 14:36:20 +0100 Subject: [PATCH] add isLoading to icon #1003 --- src/components/Icons.cy.tsx | 20 -------------------- src/components/Icons.tsx | 7 +++---- src/components/Toast.tsx | 7 ++++--- src/core/containerObserver.ts | 3 --- src/types.ts | 8 ++------ 5 files changed, 9 insertions(+), 36 deletions(-) diff --git a/src/components/Icons.cy.tsx b/src/components/Icons.cy.tsx index b0e0aef3..2861fc84 100644 --- a/src/components/Icons.cy.tsx +++ b/src/components/Icons.cy.tsx @@ -29,26 +29,6 @@ describe('Icons', () => { cy.findByText('icon').should('exist'); }); - it('handle string', () => { - const C = getIcon({ - ...props, - icon: 'icon' - }); - - cy.mount(C); - cy.findByText('icon').should('exist'); - }); - - it('handle number', () => { - const C = getIcon({ - ...props, - icon: 123 - }); - - cy.mount(C); - cy.findByText('123').should('exist'); - }); - it('handle loader', () => { const C = getIcon({ ...props, diff --git a/src/components/Icons.tsx b/src/components/Icons.tsx index 29134369..1ee41091 100644 --- a/src/components/Icons.tsx +++ b/src/components/Icons.tsx @@ -1,7 +1,7 @@ import React, { cloneElement, isValidElement } from 'react'; import { Theme, ToastProps, TypeOptions } from '../types'; -import { Default, isFn, isNum, isStr } from '../utils'; +import { Default, isFn } from '../utils'; /** * Used when providing custom icon @@ -9,6 +9,7 @@ import { Default, isFn, isNum, isStr } from '../utils'; export interface IconProps { theme: Theme; type: TypeOptions; + isLoading?: boolean; } export type BuiltInIconProps = React.SVGProps & IconProps; @@ -80,7 +81,7 @@ export type IconParams = Pick< export function getIcon({ theme, type, isLoading, icon }: IconParams) { let Icon: React.ReactNode = null; - const iconProps = { theme, type }; + const iconProps = { theme, type, isLoading }; if (icon === false) { // hide @@ -88,8 +89,6 @@ export function getIcon({ theme, type, isLoading, icon }: IconParams) { Icon = icon(iconProps); } else if (isValidElement(icon)) { Icon = cloneElement(icon, iconProps); - } else if (isStr(icon) || isNum(icon)) { - Icon = icon; } else if (isLoading) { Icon = Icons.spinner(); } else if (maybeIcon(type)) { diff --git a/src/components/Toast.tsx b/src/components/Toast.tsx index d1d63d6d..3cfa6f4d 100644 --- a/src/components/Toast.tsx +++ b/src/components/Toast.tsx @@ -6,6 +6,7 @@ import { ToastProps } from '../types'; import { Default, isFn } from '../utils'; import { CloseButton } from './CloseButton'; import { ProgressBar } from './ProgressBar'; +import { getIcon } from './Icons'; export const Toast: React.FC = props => { const { @@ -39,7 +40,6 @@ export const Toast: React.FC = props => { deleteToast, isIn, isLoading, - iconOut, closeOnClick, theme } = props; @@ -62,6 +62,7 @@ export const Toast: React.FC = props => { defaultClassName }) : cx(defaultClassName, className); + const icon = getIcon(props); const isProgressControlled = !!progress || !autoClose; const closeButtonProps = { closeToast, type, theme }; @@ -104,14 +105,14 @@ export const Toast: React.FC = props => { } style={bodyStyle} > - {iconOut != null && ( + {icon != null && (
- {iconOut} + {icon}
)}
{children as ReactNode}
diff --git a/src/core/containerObserver.ts b/src/core/containerObserver.ts index 8e0b7fd1..c04e0634 100644 --- a/src/core/containerObserver.ts +++ b/src/core/containerObserver.ts @@ -1,5 +1,4 @@ import { ReactElement, cloneElement, isValidElement } from 'react'; -import { getIcon } from '../components/Icons'; import { Id, NotValidatedToastProps, @@ -165,8 +164,6 @@ export function createContainerObserver({ } } as ToastProps; - toastProps.iconOut = getIcon(toastProps); - toastProps.closeButton = props.closeButton; if (options.closeButton === false || canBeRendered(options.closeButton)) { diff --git a/src/types.ts b/src/types.ts index b464d24b..237b1748 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,12 +28,9 @@ export type ToastContent = | ((props: ToastContentProps) => React.ReactNode); export type ToastIcon = - | boolean + | false | ((props: IconProps) => React.ReactNode) - | React.ReactElement - | string - | number - | React.ReactNode; + | React.ReactElement; export type Id = number | string; @@ -319,7 +316,6 @@ export interface ToastProps extends ToastOptions { deleteToast: () => void; theme: Theme; type: TypeOptions; - iconOut?: React.ReactNode; collapseAll: () => void; stacked?: boolean; }