Skip to content

Commit

Permalink
fix: issue 70
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchao committed Dec 18, 2024
1 parent be803e7 commit 6788e46
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/docs/usage/icons.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const ResolveCustomIcon = (props: IconProps) => {
case 'default':
return '👌';
case 'loading':
return '...';
return '';
case 'info':
return '🎈';
case 'success':
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/usage/pass-props-to-custom-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defineProps({
```
:::

- disable globally
- enable globally

```ts
app.use(
Expand Down
1 change: 0 additions & 1 deletion playground/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const onOptionsChange = (opts: ToastOptions) => {
function showToast() {
toast(constomCompo, {
type: 'info',
...options.value,
contentProps: {
title: 'narges1',
Expand Down
16 changes: 11 additions & 5 deletions src/components/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,28 @@ const maybeIcon = (type: string): type is keyof typeof Icons => type in Icons;

export function getIcon({ theme, type, isLoading, icon }: ToastOptions) {
let Icon: undefined | IconType;

const loading = !!isLoading || type === 'loading';

const iconProps = {
theme,
type,
};

if (isLoading) {
Icon = Icons.spinner();
} else if (icon === false) {
Icon = undefined;
} else if (isComponent(icon as any)) {
if (loading && (icon === undefined || typeof icon === 'boolean')) return Icons.spinner();

if (icon === false) return undefined;

if (isComponent(icon as any)) {
Icon = toRaw(icon);
} else if (isFn(icon)) {
// @ts-ignore
const iconCreator: (props?: BuiltInIconProps) => VNode = icon;

iconProps.type = loading ? 'loading' : type;

Icon = iconCreator(iconProps as BuiltInIconProps);
Icon = !Icon && loading ? Icons.spinner() : Icon;
} else if (isVNode(icon)) {
Icon = cloneVNode(icon, iconProps);
} else if (isStr(icon) || isNum(icon)) {
Expand Down
19 changes: 11 additions & 8 deletions src/core/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ type OmitLoadingOptsToastOption = Omit<ToastOptions, 'isLoading' | 'draggable'>;

let inThrottle = false;

const loadingProps = {
isLoading: true,
autoClose: false,
closeOnClick: false,
closeButton: false,
draggable: false,
} as ToastOptions;

function getAllActiveToast() {
const result: ToastOptions[] = [];
const items = getAllToast();
Expand Down Expand Up @@ -59,13 +67,14 @@ function openToast(content: Content, type: ToastType, options = {} as ToastOptio

options = {
...options,
...options.type === 'loading' ? loadingProps : {},
content,
containerId: options.containerId || String(options.position),
} as ToastOptions;

const progress = Number(options?.progress);

if (progress < 0) {
if (!isNaN(progress) && progress < 0) {
options.progress = 0;
}
if (progress > 1) {
Expand Down Expand Up @@ -136,13 +145,7 @@ toast.success =
toast.loading = (content: Content, options?: OmitLoadingOptsToastOption) => openToast(
content,
TYPE.DEFAULT,
mergeOptions(options, {
isLoading: true,
autoClose: false,
closeOnClick: false,
closeButton: false,
draggable: false,
} as ToastOptions),
mergeOptions(options, loadingProps as ToastOptions),
);

/** dark toast */
Expand Down

0 comments on commit 6788e46

Please sign in to comment.