Skip to content

Commit

Permalink
add isLoading to icon #1003
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed Nov 3, 2023
1 parent a9d3256 commit 53f8a73
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 36 deletions.
20 changes: 0 additions & 20 deletions src/components/Icons.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/components/Icons.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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
*/
export interface IconProps {
theme: Theme;
type: TypeOptions;
isLoading?: boolean;
}

export type BuiltInIconProps = React.SVGProps<SVGSVGElement> & IconProps;
Expand Down Expand Up @@ -80,16 +81,14 @@ 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
} else if (isFn(icon)) {
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)) {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToastProps> = props => {
const {
Expand Down Expand Up @@ -39,7 +40,6 @@ export const Toast: React.FC<ToastProps> = props => {
deleteToast,
isIn,
isLoading,
iconOut,
closeOnClick,
theme
} = props;
Expand All @@ -62,6 +62,7 @@ export const Toast: React.FC<ToastProps> = props => {
defaultClassName
})
: cx(defaultClassName, className);
const icon = getIcon(props);
const isProgressControlled = !!progress || !autoClose;

const closeButtonProps = { closeToast, type, theme };
Expand Down Expand Up @@ -104,14 +105,14 @@ export const Toast: React.FC<ToastProps> = props => {
}
style={bodyStyle}
>
{iconOut != null && (
{icon != null && (
<div
className={cx(`${Default.CSS_NAMESPACE}__toast-icon`, {
[`${Default.CSS_NAMESPACE}--animate-icon ${Default.CSS_NAMESPACE}__zoom-enter`]:
!isLoading
})}
>
{iconOut}
{icon}
</div>
)}
<div>{children as ReactNode}</div>
Expand Down
3 changes: 0 additions & 3 deletions src/core/containerObserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReactElement, cloneElement, isValidElement } from 'react';
import { getIcon } from '../components/Icons';
import {
Id,
NotValidatedToastProps,
Expand Down Expand Up @@ -165,8 +164,6 @@ export function createContainerObserver({
}
} as ToastProps;

toastProps.iconOut = getIcon(toastProps);

toastProps.closeButton = props.closeButton;

if (options.closeButton === false || canBeRendered(options.closeButton)) {
Expand Down
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ export type ToastContent<T = unknown> =
| ((props: ToastContentProps<T>) => React.ReactNode);

export type ToastIcon =
| boolean
| false
| ((props: IconProps) => React.ReactNode)
| React.ReactElement<IconProps>
| string
| number
| React.ReactNode;
| React.ReactElement<IconProps>;

export type Id = number | string;

Expand Down Expand Up @@ -319,7 +316,6 @@ export interface ToastProps extends ToastOptions {
deleteToast: () => void;
theme: Theme;
type: TypeOptions;
iconOut?: React.ReactNode;
collapseAll: () => void;
stacked?: boolean;
}
Expand Down

0 comments on commit 53f8a73

Please sign in to comment.