Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates toast action/cancel property type/handlers #398

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,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 @@ -434,13 +434,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 @@ -75,7 +75,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