Skip to content

Commit

Permalink
feat: updates toast action/cancel property type/handlers (#398)
Browse files Browse the repository at this point in the history
* feat: update action `type` and helper function

* feat: handle action/cancel optional onClick property
  • Loading branch information
6thpath committed May 23, 2024
1 parent 35610be commit 7d28d42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ const Toast = (props: ToastProps) => {
// We need to check twice because typescript
if (!isAction(toast.cancel)) return;
if (!dismissible) return;
toast.cancel.onClick?.(event);
deleteToast();
toast.cancel.onClick(event);
}}
className={cn(classNames?.cancelButton, toast?.classNames?.cancelButton)}
>
Expand All @@ -436,13 +436,14 @@ const Toast = (props: ToastProps) => {
toast.action
) : toast.action && isAction(toast.action) ? (
<button
data-button=""
data-button
data-action
style={toast.actionButtonStyle || actionButtonStyle}
onClick={(event) => {
// We need to check twice because typescript
if (!isAction(toast.action)) return;
toast.action.onClick(event);
if (event.defaultPrevented) return;
toast.action.onClick?.(event);
deleteToast();
}}
className={cn(classNames?.actionButton, toast?.classNames?.actionButton)}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface ToastIcons {
}

interface Action {
label: string;
label: React.ReactNode;
onClick: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
actionButtonStyle?: React.CSSProperties;
}
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 7d28d42

Please sign in to comment.