Skip to content

Commit a30867a

Browse files
fix(MessageBox - TypeScript): adjust onClose type (#5975)
Co-authored-by: Marcus Notheis <[email protected]>
1 parent c32ccab commit a30867a

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

packages/main/src/components/MessageBox/MessageBox.cy.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import addIcon from '@ui5/webcomponents-icons/dist/add.js';
2+
import { useState } from 'react';
23
import { Button, Icon, MessageBoxAction, MessageBoxType } from '../..';
34
import { MessageBox } from './index.js';
45

@@ -29,6 +30,54 @@ describe('MessageBox', () => {
2930
});
3031
});
3132

33+
it('close event', () => {
34+
const callback = cy.spy().as('close');
35+
function TestComp() {
36+
const [open, setOpen] = useState(false);
37+
const [type, setType] = useState('');
38+
return (
39+
<>
40+
<Button
41+
onClick={() => {
42+
setOpen(true);
43+
}}
44+
>
45+
Open
46+
</Button>
47+
<MessageBox
48+
open={open}
49+
onClose={(e) => {
50+
callback(e);
51+
setType(e.type);
52+
setOpen(false);
53+
}}
54+
>
55+
My Message Box Content
56+
</MessageBox>
57+
<span data-testid="eventType">{type}</span>
58+
</>
59+
);
60+
}
61+
62+
cy.mount(<TestComp />);
63+
64+
cy.findByText('Open').click();
65+
cy.findByText('OK').click();
66+
cy.get('@close').should('have.been.calledOnce');
67+
cy.wrap(callback).should(
68+
'have.been.calledWith',
69+
Cypress.sinon.match({
70+
type: 'click'
71+
})
72+
);
73+
cy.findByTestId('eventType').should('have.text', 'click');
74+
75+
cy.findByText('Open').click();
76+
cy.realPress('Escape');
77+
cy.get('@close').should('have.been.calledTwice');
78+
cy.findByTestId('eventType').should('have.text', 'before-close');
79+
});
80+
3281
it('Custom Button', () => {
3382
const click = cy.spy().as('onButtonClick');
3483
const close = cy.spy().as('onMessageBoxClose');

packages/main/src/components/MessageBox/index.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import {
2727
WARNING,
2828
YES
2929
} from '../../i18n/i18n-defaults.js';
30-
import { stopPropagation } from '../../internal/stopPropagation.js';
31-
import type { ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
30+
import type { Ui5CustomEvent } from '../../types/index.js';
31+
import type { ButtonDomRef, ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
3232
import { Button, Dialog, Icon, Title } from '../../webComponents/index.js';
3333
import { Text } from '../Text/index.js';
3434
import { classNames, styleData } from './MessageBox.module.css.js';
@@ -90,9 +90,17 @@ export interface MessageBoxPropTypes
9090
*/
9191
initialFocus?: MessageBoxActionType;
9292
/**
93-
* 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.
93+
* Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the `ESC` key).
94+
* `event.detail.action` contains the pressed action button.
95+
*
96+
* __Note:__ The target of the event differs according to how the user closed the dialog.
9497
*/
95-
onClose?: (event: CustomEvent<{ action: MessageBoxActionType }>) => void;
98+
onClose?: (
99+
//todo adjust this once enrichEventWithDetails forwards the native `detail`
100+
event:
101+
| Ui5CustomEvent<DialogDomRef, { action: undefined }>
102+
| (MouseEvent & ButtonDomRef & { detail: { action: MessageBoxActionType } })
103+
) => void;
96104
}
97105

98106
const getIcon = (icon, type, classes) => {
@@ -188,9 +196,18 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
188196
}
189197
};
190198

191-
const handleOnClose = (e) => {
192-
const { action } = e.target.dataset;
193-
stopPropagation(e);
199+
const handleDialogClose: DialogPropTypes['onBeforeClose'] = (e) => {
200+
if (typeof props.onBeforeClose === 'function') {
201+
props.onBeforeClose(e);
202+
}
203+
if (e.detail.escPressed) {
204+
// @ts-expect-error: todo check type
205+
onClose(enrichEventWithDetails(e, { action: undefined }));
206+
}
207+
};
208+
209+
const handleOnClose: ButtonPropTypes['onClick'] = (e) => {
210+
const { action } = e.currentTarget.dataset;
194211
onClose(enrichEventWithDetails(e, { action }));
195212
};
196213

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

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

211228
const iconToRender = getIcon(icon, type, classNames);
212229
const needsCustomHeader = !props.header && !!iconToRender;
@@ -216,7 +233,7 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
216233
open={open}
217234
ref={ref}
218235
className={clsx(classNames.messageBox, className)}
219-
onClose={open ? handleOnClose : stopPropagation}
236+
onBeforeClose={handleDialogClose}
220237
accessibleNameRef={needsCustomHeader ? `${messageBoxId}-title ${messageBoxId}-text` : undefined}
221238
accessibleRole={PopupAccessibleRole.AlertDialog}
222239
{...restWithoutOmitted}

0 commit comments

Comments
 (0)