An opinionated toast component for React.
feat: updates toast action/cancel property type/handlers (#398)
Phát7d28d42b368f
diff --git a/src/index.tsx b/src/index.tsxindex 9a2adc5..fa76ce3 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -424,8 +424,8 @@ const Toast = (props: ToastProps) => {// We need to check twice because typescriptif (!isAction(toast.cancel)) return;if (!dismissible) return;+ toast.cancel.onClick?.(event);deleteToast();- toast.cancel.onClick(event);}}className={cn(classNames?.cancelButton, toast?.classNames?.cancelButton)}>@@ -436,13 +436,14 @@ const Toast = (props: ToastProps) => {toast.action) : toast.action && isAction(toast.action) ? (<button- data-button=""+ data-button+ data-actionstyle={toast.actionButtonStyle || actionButtonStyle}onClick={(event) => {// We need to check twice because typescriptif (!isAction(toast.action)) return;- toast.action.onClick(event);if (event.defaultPrevented) return;+ toast.action.onClick?.(event);deleteToast();}}className={cn(classNames?.actionButton, toast?.classNames?.actionButton)}diff --git a/src/types.ts b/src/types.tsindex a4ac7a0..4d7d216 100644--- a/src/types.ts+++ b/src/types.ts@@ -41,7 +41,7 @@ export interface ToastIcons {}interface Action {- label: string;+ label: React.ReactNode;onClick: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;actionButtonStyle?: React.CSSProperties;}@@ -76,7 +76,7 @@ export interface ToastT {}export function isAction(action: Action | React.ReactNode): action is Action {- return (action as Action).label !== undefined && typeof (action as Action).onClick === 'function';+ return (action as Action).label !== undefined;}export type Position = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'top-center' | 'bottom-center';