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: event handlers on toast container #1060

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/core/containerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ export function createContainerObserver(
notify();
dispatchChanges(toToastItem(toast, isNew ? 'added' : 'updated'));

if (isNew && isFn(onOpen))
onOpen(isValidElement(children) && children.props);
if (isNew) {
if (isFn(containerProps.onOpen))
containerProps.onOpen(isValidElement(children) && children.props);
if (isFn(onOpen)) onOpen(isValidElement(children) && children.props);
}
};

const buildToast = <TData = unknown>(
Expand All @@ -119,6 +122,9 @@ export function createContainerObserver(

const toastProps = {
...props,
// dont propagate the handlers called above for the container to the toast
onOpen: undefined,
onClose: undefined,
style: props.toastStyle,
key: toastKey++,
...Object.fromEntries(
Expand All @@ -142,6 +148,8 @@ export function createContainerObserver(
deleteToast() {
const toastToRemove = toasts.get(toastId)!;
const { onClose, children } = toastToRemove.props;
if (isFn(containerProps.onClose))
containerProps.onClose(isValidElement(children) && children.props);
if (isFn(onClose)) onClose(isValidElement(children) && children.props);

dispatchChanges(toToastItem(toastToRemove, 'removed'));
Expand Down
43 changes: 43 additions & 0 deletions src/core/toast.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,49 @@ describe('without container', () => {
});
});

describe('with container event handlers', () => {
it('calls container open and close', () => {
const onOpen = cy.stub().as('onContainerOpen');
const onClose = cy.stub().as('onContainerClose');
cy.mount(
<ToastContainer
autoClose={false}
closeOnClick
onClose={onClose}
onOpen={onOpen}
/>
);
toast('msg');
cy.resolveEntranceAnimation();
cy.findByText('msg').should('exist').click().should('not.exist');
cy.get('@onContainerOpen').should('have.been.calledOnce');
cy.get('@onContainerClose').should('have.been.calledOnce');
});

it('calls container open and close and toast open and close', () => {
const onOpen = cy.stub().as('onContainerOpen');
const onClose = cy.stub().as('onContainerClose');
cy.mount(
<ToastContainer
autoClose={false}
closeOnClick
onClose={onClose}
onOpen={onOpen}
/>
);
toast('msg', {
onOpen: cy.stub().as('onToastOpen'),
onClose: cy.stub().as('onToastClose')
});
cy.resolveEntranceAnimation();
cy.findByText('msg').should('exist').click().should('not.exist');
cy.get('@onContainerOpen').should('have.been.calledOnce');
cy.get('@onContainerClose').should('have.been.calledOnce');
cy.get('@onToastOpen').should('have.been.calledOnce');
cy.get('@onToastClose').should('have.been.calledOnce');
});
});

describe('with container', () => {
beforeEach(() => {
cy.mount(<ToastContainer autoClose={false} closeOnClick />);
Expand Down
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,6 @@ interface CommonOptions {
* `Default: 'light'`
*/
theme?: Theme;
}

export interface ToastOptions<Data = unknown> extends CommonOptions {
/**
* An optional css class to set.
*/
className?: ToastClassName;

/**
* Called when toast is mounted.
Expand All @@ -200,6 +193,13 @@ export interface ToastOptions<Data = unknown> extends CommonOptions {
* Called when toast is unmounted.
*/
onClose?: <T = {}>(props: T) => void;
}

export interface ToastOptions<Data = unknown> extends CommonOptions {
/**
* An optional css class to set.
*/
className?: ToastClassName;

/**
* An optional inline style to apply.
Expand Down