Skip to content

Commit ef490f4

Browse files
authored
refactor(MessageBox): refactor onClose event (#5989)
BREAKING CHANGE: `onClose` is now a plain callback and not a `CustomEvent` event anymore. It now receives two params: `action` & `escPressed`.
1 parent a30867a commit ef490f4

File tree

3 files changed

+47
-89
lines changed

3 files changed

+47
-89
lines changed

docs/MigrationGuide.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ function MyComponent() {
249249
}
250250
```
251251

252+
### MessageBox
253+
254+
- `onClose` is now a plain callback function and no `CustomEvent` anymore. It receives two parameters, `action` and `escPressed`.
255+
256+
```jsx
257+
// v1
258+
// onClose?: (event: CustomEvent<{ action: MessageBoxAction }>) => void;
259+
260+
<MessageBox
261+
onClose={(event) => {
262+
console.log(event.detail.action);
263+
}}
264+
>
265+
{children}
266+
</MessageBox>
267+
268+
// v2
269+
// onClose?: (action: MessageBoxActionType | undefined, escPressed?: true) => void;
270+
271+
<MessageBox
272+
onClose={(action, escPressed) => {
273+
console.log(action, escPressed);
274+
}}
275+
>
276+
{children}
277+
</MessageBox>
278+
279+
```
280+
252281
### ObjectPage
253282

254283
The newly introduced `DynamicPage` web component comes with its own `DynamicPageHeader` and `DynamicPageTitle` components, which are unfortunately incompatible with our `ObjectPage` implementation.

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

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ describe('MessageBox', () => {
1919
</MessageBox>
2020
);
2121
cy.findByText(buttonText).click();
22-
cy.wrap(callback).should(
23-
'have.been.calledWith',
24-
Cypress.sinon.match({
25-
detail: {
26-
action: buttonText
27-
}
28-
})
29-
);
22+
cy.wrap(callback).should('have.been.calledWith', Cypress.sinon.match(buttonText));
3023
});
3124
});
3225

@@ -46,9 +39,9 @@ describe('MessageBox', () => {
4639
</Button>
4740
<MessageBox
4841
open={open}
49-
onClose={(e) => {
50-
callback(e);
51-
setType(e.type);
42+
onClose={(action, escPressed) => {
43+
callback(action, escPressed);
44+
setType(escPressed ? 'before-close' : 'click');
5245
setOpen(false);
5346
}}
5447
>
@@ -64,12 +57,6 @@ describe('MessageBox', () => {
6457
cy.findByText('Open').click();
6558
cy.findByText('OK').click();
6659
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-
);
7360
cy.findByTestId('eventType').should('have.text', 'click');
7461

7562
cy.findByText('Open').click();
@@ -105,14 +92,7 @@ describe('MessageBox', () => {
10592
cy.findByText('Custom').click();
10693
cy.get('@onMessageBoxClose')
10794
.should('have.been.calledOnce')
108-
.should(
109-
'have.been.calledWith',
110-
Cypress.sinon.match({
111-
detail: {
112-
action: `1: custom action`
113-
}
114-
})
115-
);
95+
.should('have.been.calledWith', Cypress.sinon.match('1: custom action'));
11696
cy.get('@onButtonClick').should('have.been.calledOnce');
11797
});
11898

@@ -127,14 +107,7 @@ describe('MessageBox', () => {
127107
cy.findByText('Cancel').click();
128108
cy.get('@onMessageBoxClose')
129109
.should('have.been.calledOnce')
130-
.should(
131-
'have.been.calledWith',
132-
Cypress.sinon.match({
133-
detail: {
134-
action: MessageBoxAction.Cancel
135-
}
136-
})
137-
);
110+
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.Cancel));
138111
});
139112

140113
it('Show', () => {
@@ -148,26 +121,12 @@ describe('MessageBox', () => {
148121
cy.findByText('Yes').click();
149122
cy.get('@onMessageBoxClose')
150123
.should('have.been.calledOnce')
151-
.should(
152-
'have.been.calledWith',
153-
Cypress.sinon.match({
154-
detail: {
155-
action: MessageBoxAction.Yes
156-
}
157-
})
158-
);
124+
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.Yes));
159125

160126
cy.findByText('No').click();
161127
cy.get('@onMessageBoxClose')
162128
.should('have.been.calledTwice')
163-
.should(
164-
'have.been.calledWith',
165-
Cypress.sinon.match({
166-
detail: {
167-
action: MessageBoxAction.No
168-
}
169-
})
170-
);
129+
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.No));
171130
});
172131

173132
it('Success w/ custom title', () => {
@@ -187,14 +146,7 @@ describe('MessageBox', () => {
187146
cy.findByText('OK').click();
188147
cy.get('@onMessageBoxClose')
189148
.should('have.been.calledOnce')
190-
.should(
191-
'have.been.calledWith',
192-
Cypress.sinon.match({
193-
detail: {
194-
action: MessageBoxAction.OK
195-
}
196-
})
197-
);
149+
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.OK));
198150
});
199151

200152
it('No Title', () => {
@@ -224,25 +176,11 @@ describe('MessageBox', () => {
224176
cy.findByText(MessageBoxAction.OK).should('be.visible').click();
225177
cy.get('@onMessageBoxClose')
226178
.should('have.been.calledOnce')
227-
.should(
228-
'have.been.calledWith',
229-
Cypress.sinon.match({
230-
detail: {
231-
action: MessageBoxAction.OK
232-
}
233-
})
234-
);
179+
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.OK));
235180
cy.findByText('My Custom Action').should('be.visible').click();
236181
cy.get('@onMessageBoxClose')
237182
.should('have.been.calledTwice')
238-
.should(
239-
'have.been.calledWith',
240-
Cypress.sinon.match({
241-
detail: {
242-
action: 'My Custom Action'
243-
}
244-
})
245-
);
183+
.should('have.been.calledWith', Cypress.sinon.match('My Custom Action'));
246184
});
247185

248186
it("Don't crash on unknown type", () => {

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PopupAccessibleRole from '@ui5/webcomponents/dist/types/PopupAccessibleRo
66
import TitleLevel from '@ui5/webcomponents/dist/types/TitleLevel.js';
77
import ValueState from '@ui5/webcomponents-base/dist/types/ValueState.js';
88
import iconSysHelp from '@ui5/webcomponents-icons/dist/sys-help-2.js';
9-
import { enrichEventWithDetails, useI18nBundle, useIsomorphicId, useStylesheet } from '@ui5/webcomponents-react-base';
9+
import { useI18nBundle, useIsomorphicId, useStylesheet } from '@ui5/webcomponents-react-base';
1010
import { clsx } from 'clsx';
1111
import type { ReactElement, ReactNode } from 'react';
1212
import { cloneElement, forwardRef, isValidElement } from 'react';
@@ -27,8 +27,7 @@ import {
2727
WARNING,
2828
YES
2929
} from '../../i18n/i18n-defaults.js';
30-
import type { Ui5CustomEvent } from '../../types/index.js';
31-
import type { ButtonDomRef, ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
30+
import type { ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
3231
import { Button, Dialog, Icon, Title } from '../../webComponents/index.js';
3332
import { Text } from '../Text/index.js';
3433
import { classNames, styleData } from './MessageBox.module.css.js';
@@ -90,17 +89,10 @@ export interface MessageBoxPropTypes
9089
*/
9190
initialFocus?: MessageBoxActionType;
9291
/**
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.
92+
* Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the Escape key).
93+
* `action` is the pressed action button, it's `undefined` when closed via ESC.
9794
*/
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;
95+
onClose?: (action: MessageBoxActionType | undefined, escPressed?: true) => void;
10496
}
10597

10698
const getIcon = (icon, type, classes) => {
@@ -201,14 +193,13 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
201193
props.onBeforeClose(e);
202194
}
203195
if (e.detail.escPressed) {
204-
// @ts-expect-error: todo check type
205-
onClose(enrichEventWithDetails(e, { action: undefined }));
196+
onClose(undefined, e.detail.escPressed);
206197
}
207198
};
208199

209200
const handleOnClose: ButtonPropTypes['onClick'] = (e) => {
210201
const { action } = e.currentTarget.dataset;
211-
onClose(enrichEventWithDetails(e, { action }));
202+
onClose(action);
212203
};
213204

214205
const messageBoxId = useIsomorphicId();

0 commit comments

Comments
 (0)