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

fix(MessageBox - TypeScript): adjust onClose type #5975

Merged
merged 6 commits into from
Jun 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/main/src/components/MessageBox/MessageBox.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import addIcon from '@ui5/webcomponents-icons/dist/add.js';
import { useState } from 'react';
import { Button, Icon, MessageBoxAction, MessageBoxType } from '../..';
import { MessageBox } from './index.js';

Expand Down Expand Up @@ -29,6 +30,54 @@ describe('MessageBox', () => {
});
});

it('close event', () => {
const callback = cy.spy().as('close');
function TestComp() {
const [open, setOpen] = useState(false);
const [type, setType] = useState('');
return (
<>
<Button
onClick={() => {
setOpen(true);
}}
>
Open
</Button>
<MessageBox
open={open}
onClose={(e) => {
callback(e);
setType(e.type);
setOpen(false);
}}
>
My Message Box Content
</MessageBox>
<span data-testid="eventType">{type}</span>
</>
);
}

cy.mount(<TestComp />);

cy.findByText('Open').click();
cy.findByText('OK').click();
cy.get('@close').should('have.been.calledOnce');
cy.wrap(callback).should(
'have.been.calledWith',
Cypress.sinon.match({
type: 'click'
})
);
cy.findByTestId('eventType').should('have.text', 'click');

cy.findByText('Open').click();
cy.realPress('Escape');
cy.get('@close').should('have.been.calledTwice');
cy.findByTestId('eventType').should('have.text', 'before-close');
});

it('Custom Button', () => {
const click = cy.spy().as('onButtonClick');
const close = cy.spy().as('onMessageBoxClose');
Expand Down
35 changes: 26 additions & 9 deletions packages/main/src/components/MessageBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
WARNING,
YES
} from '../../i18n/i18n-defaults.js';
import { stopPropagation } from '../../internal/stopPropagation.js';
import type { ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
import type { Ui5CustomEvent } from '../../types/index.js';
import type { ButtonDomRef, ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
import { Button, Dialog, Icon, Title } from '../../webComponents/index.js';
import { Text } from '../Text/index.js';
import { classNames, styleData } from './MessageBox.module.css.js';
Expand Down Expand Up @@ -90,9 +90,17 @@ export interface MessageBoxPropTypes
*/
initialFocus?: MessageBoxActionType;
/**
* Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the `ESC` key). `event.detail.action` contains the pressed action button.
* Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the `ESC` key).
* `event.detail.action` contains the pressed action button.
*
* __Note:__ The target of the event differs according to how the user closed the dialog.
*/
onClose?: (event: CustomEvent<{ action: MessageBoxActionType }>) => void;
onClose?: (
//todo adjust this once enrichEventWithDetails forwards the native `detail`
event:
| Ui5CustomEvent<DialogDomRef, { action: undefined }>
| (MouseEvent & ButtonDomRef & { detail: { action: MessageBoxActionType } })
) => void;
}

const getIcon = (icon, type, classes) => {
Expand Down Expand Up @@ -188,9 +196,18 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
}
};

const handleOnClose = (e) => {
const { action } = e.target.dataset;
stopPropagation(e);
const handleDialogClose: DialogPropTypes['onBeforeClose'] = (e) => {
if (typeof props.onBeforeClose === 'function') {
props.onBeforeClose(e);
}
if (e.detail.escPressed) {
// @ts-expect-error: todo check type
onClose(enrichEventWithDetails(e, { action: undefined }));
}
};

const handleOnClose: ButtonPropTypes['onClick'] = (e) => {
const { action } = e.currentTarget.dataset;
onClose(enrichEventWithDetails(e, { action }));
};

Expand All @@ -206,7 +223,7 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
};

// @ts-expect-error: footer, headerText and onClose are already omitted via prop types
const { footer: _0, headerText: _1, onClose: _2, ...restWithoutOmitted } = rest;
const { footer: _0, headerText: _1, onClose: _2, onBeforeClose: _3, ...restWithoutOmitted } = rest;

const iconToRender = getIcon(icon, type, classNames);
const needsCustomHeader = !props.header && !!iconToRender;
Expand All @@ -216,7 +233,7 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
open={open}
ref={ref}
className={clsx(classNames.messageBox, className)}
onClose={open ? handleOnClose : stopPropagation}
onBeforeClose={handleDialogClose}
accessibleNameRef={needsCustomHeader ? `${messageBoxId}-title ${messageBoxId}-text` : undefined}
accessibleRole={PopupAccessibleRole.AlertDialog}
{...restWithoutOmitted}
Expand Down
Loading