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

unique status for close button removal #1029

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Toast: React.FC<ToastProps> = props => {
type,
hideProgressBar,
closeToast,
closeToastByButton,
transition: Transition,
position,
className,
Expand Down Expand Up @@ -65,7 +66,7 @@ export const Toast: React.FC<ToastProps> = props => {
const icon = getIcon(props);
const isProgressControlled = !!progress || !autoClose;

const closeButtonProps = { closeToast, type, theme };
const closeButtonProps = { closeToast: closeToastByButton, type, theme };
let Close: React.ReactNode = null;

if (closeButton === false) {
Expand Down
15 changes: 14 additions & 1 deletion src/core/containerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function createContainerObserver(
let activeToasts: Id[] = [];
let snapshot: Toast[] = [];
let props = containerProps;
let closedByButton = false;
const toasts = new Map<Id, Toast>();
const listeners = new Set<Notify>();

Expand Down Expand Up @@ -113,6 +114,11 @@ export function createContainerObserver(
removeToast(toastId);
};

const closeToastByButton = () => {
removeToast(toastId);
closedByButton = true;
};

const isNotAnUpdate = updateId == null;

if (isNotAnUpdate) toastCount++;
Expand All @@ -128,6 +134,7 @@ export function createContainerObserver(
updateId,
data,
closeToast,
closeToastByButton,
isIn: false,
className: parseClassName(options.className || props.toastClassName),
bodyClassName: parseClassName(
Expand All @@ -144,9 +151,15 @@ export function createContainerObserver(
const { onClose, children } = toastToRemove.props;
if (isFn(onClose)) onClose(isValidElement(children) && children.props);

dispatchChanges(toToastItem(toastToRemove, 'removed'));
dispatchChanges(
toToastItem(
toastToRemove,
!closedByButton ? 'removed' : 'removed by button'
)
);
toasts.delete(toastId);

closedByButton = false;
toastCount--;
if (toastCount < 0) toastCount = 0;

Expand Down
18 changes: 18 additions & 0 deletions src/core/toast.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { ToastContainer } from '../components';
import { toast } from './toast';
import { Default } from '../utils';

beforeEach(() => {
cy.viewport('macbook-15');
Expand Down Expand Up @@ -84,6 +85,23 @@ describe('with container', () => {
});
});

it('remove toast by button', () => {
toast.onChange(cy.stub().as('onChange'));

toast('msg', { data: 'xxxx' });

cy.resolveEntranceAnimation();

cy.findByText('msg').should('exist');

cy.get(`.${Default.CSS_NAMESPACE}__close-button`).click();

cy.get('@onChange').should('have.been.calledWithMatch', {
status: 'removed by button'
});
cy.findByText('msg').should('not.exist');
});

it('unsubscribe from change event', () => {
const unsub = toast.onChange(cy.stub().as('onChange'));
unsub();
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export interface ToastProps extends ToastOptions {
key: Id;
transition: ToastTransition;
closeToast: () => void;
closeToastByButton: () => void;
position: ToastPosition;
children?: ToastContent;
draggablePercent: number;
Expand Down Expand Up @@ -336,7 +337,11 @@ export interface Toast {
toggle?: (v: boolean) => void;
}

export type ToastItemStatus = 'added' | 'removed' | 'updated';
export type ToastItemStatus =
| 'added'
| 'removed'
| 'updated'
| 'removed by button';

export interface ToastItem<Data = {}> {
content: ToastContent<Data>;
Expand Down
Loading