Skip to content

An opinionated toast component for React.

feat: `description` support function in promise (#279)

kaho3ac23860babd
diff --git a/src/state.ts b/src/state.ts
index 89e8df1..03f7915 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -116,6 +116,7 @@ class Observer {
promise,
type: 'loading',
message: data.loading,
+ description: typeof data.description !== 'function' ? data.description : undefined,
});
}
@@ -131,18 +132,25 @@ class Observer {
const message =
// @ts-expect-error
typeof data.error === 'function' ? data.error(`HTTP error! status: ${response.status}`) : data.error;
- this.create({ id, type: 'error', message });
+ const description =
+ typeof data.description === 'function'
+ ? // @ts-expect-error
+ data.description(`HTTP error! status: ${response.status}`)
+ : data.description;
+ this.create({ id, type: 'error', message, description });
} else if (data.success !== undefined) {
shouldDismiss = false;
const message = typeof data.success === 'function' ? data.success(response) : data.success;
- this.create({ id, type: 'success', message });
+ const description = typeof data.description === 'function' ? data.description(response) : data.description;
+ this.create({ id, type: 'success', message, description });
}
})
.catch((error) => {
if (data.error !== undefined) {
shouldDismiss = false;
const message = typeof data.error === 'function' ? data.error(error) : data.error;
- this.create({ id, type: 'error', message });
+ const description = typeof data.description === 'function' ? data.description(error) : data.description;
+ this.create({ id, type: 'error', message, description });
}
})
.finally(() => {
diff --git a/src/types.ts b/src/types.ts
index 407a1e5..489b369 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -4,10 +4,13 @@ export type ToastTypes = 'normal' | 'action' | 'success' | 'info' | 'warning' |
export type PromiseT<Data = any> = Promise<Data> | (() => Promise<Data>);
-export type PromiseData<ToastData = any> = ExternalToast & {
+export type PromiseExternalToast = Omit<ExternalToast, 'description'>;
+
+export type PromiseData<ToastData = any> = PromiseExternalToast & {
loading?: string | React.ReactNode;
success?: string | React.ReactNode | ((data: ToastData) => React.ReactNode | string);
error?: string | React.ReactNode | ((error: any) => React.ReactNode | string);
+ description?: string | React.ReactNode | ((data: any) => React.ReactNode | string);
finally?: () => void | Promise<void>;
};