Skip to content

refactor(MessageBox): refactor onClose event #5989

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

Merged
merged 3 commits into from
Jun 27, 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
29 changes: 29 additions & 0 deletions docs/MigrationGuide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ function MyComponent() {
}
```

### MessageBox

- `onClose` is now a plain callback function and no `CustomEvent` anymore. It receives two parameters, `action` and `escPressed`.

```jsx
// v1
// onClose?: (event: CustomEvent<{ action: MessageBoxAction }>) => void;

<MessageBox
onClose={(event) => {
console.log(event.detail.action);
}}
>
{children}
</MessageBox>

// v2
// onClose?: (action: MessageBoxActionType | undefined, escPressed?: true) => void;

<MessageBox
onClose={(action, escPressed) => {
console.log(action, escPressed);
}}
>
{children}
</MessageBox>

```

### ObjectPage

The newly introduced `DynamicPage` web component comes with its own `DynamicPageHeader` and `DynamicPageTitle` components, which are unfortunately incompatible with our `ObjectPage` implementation.
Expand Down
84 changes: 11 additions & 73 deletions packages/main/src/components/MessageBox/MessageBox.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ describe('MessageBox', () => {
</MessageBox>
);
cy.findByText(buttonText).click();
cy.wrap(callback).should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: buttonText
}
})
);
cy.wrap(callback).should('have.been.calledWith', Cypress.sinon.match(buttonText));
});
});

Expand All @@ -46,9 +39,9 @@ describe('MessageBox', () => {
</Button>
<MessageBox
open={open}
onClose={(e) => {
callback(e);
setType(e.type);
onClose={(action, escPressed) => {
callback(action, escPressed);
setType(escPressed ? 'before-close' : 'click');
setOpen(false);
}}
>
Expand All @@ -64,12 +57,6 @@ describe('MessageBox', () => {
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();
Expand Down Expand Up @@ -105,14 +92,7 @@ describe('MessageBox', () => {
cy.findByText('Custom').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledOnce')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: `1: custom action`
}
})
);
.should('have.been.calledWith', Cypress.sinon.match('1: custom action'));
cy.get('@onButtonClick').should('have.been.calledOnce');
});

Expand All @@ -127,14 +107,7 @@ describe('MessageBox', () => {
cy.findByText('Cancel').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledOnce')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: MessageBoxAction.Cancel
}
})
);
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.Cancel));
});

it('Show', () => {
Expand All @@ -148,26 +121,12 @@ describe('MessageBox', () => {
cy.findByText('Yes').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledOnce')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: MessageBoxAction.Yes
}
})
);
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.Yes));

cy.findByText('No').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledTwice')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: MessageBoxAction.No
}
})
);
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.No));
});

it('Success w/ custom title', () => {
Expand All @@ -187,14 +146,7 @@ describe('MessageBox', () => {
cy.findByText('OK').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledOnce')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: MessageBoxAction.OK
}
})
);
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.OK));
});

it('No Title', () => {
Expand Down Expand Up @@ -224,25 +176,11 @@ describe('MessageBox', () => {
cy.findByText(MessageBoxAction.OK).should('be.visible').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledOnce')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: MessageBoxAction.OK
}
})
);
.should('have.been.calledWith', Cypress.sinon.match(MessageBoxAction.OK));
cy.findByText('My Custom Action').should('be.visible').click();
cy.get('@onMessageBoxClose')
.should('have.been.calledTwice')
.should(
'have.been.calledWith',
Cypress.sinon.match({
detail: {
action: 'My Custom Action'
}
})
);
.should('have.been.calledWith', Cypress.sinon.match('My Custom Action'));
});

it("Don't crash on unknown type", () => {
Expand Down
23 changes: 7 additions & 16 deletions packages/main/src/components/MessageBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PopupAccessibleRole from '@ui5/webcomponents/dist/types/PopupAccessibleRo
import TitleLevel from '@ui5/webcomponents/dist/types/TitleLevel.js';
import ValueState from '@ui5/webcomponents-base/dist/types/ValueState.js';
import iconSysHelp from '@ui5/webcomponents-icons/dist/sys-help-2.js';
import { enrichEventWithDetails, useI18nBundle, useIsomorphicId, useStylesheet } from '@ui5/webcomponents-react-base';
import { useI18nBundle, useIsomorphicId, useStylesheet } from '@ui5/webcomponents-react-base';
import { clsx } from 'clsx';
import type { ReactElement, ReactNode } from 'react';
import { cloneElement, forwardRef, isValidElement } from 'react';
Expand All @@ -27,8 +27,7 @@ import {
WARNING,
YES
} from '../../i18n/i18n-defaults.js';
import type { Ui5CustomEvent } from '../../types/index.js';
import type { ButtonDomRef, ButtonPropTypes, DialogDomRef, DialogPropTypes } from '../../webComponents/index.js';
import type { 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,17 +89,10 @@ 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.
*
* __Note:__ The target of the event differs according to how the user closed the dialog.
* Callback to be executed when the `MessageBox` is closed (either by pressing on one of the `actions` or by pressing the Escape key).
* `action` is the pressed action button, it's `undefined` when closed via ESC.
*/
onClose?: (
//todo adjust this once enrichEventWithDetails forwards the native `detail`
event:
| Ui5CustomEvent<DialogDomRef, { action: undefined }>
| (MouseEvent & ButtonDomRef & { detail: { action: MessageBoxActionType } })
) => void;
onClose?: (action: MessageBoxActionType | undefined, escPressed?: true) => void;
}

const getIcon = (icon, type, classes) => {
Expand Down Expand Up @@ -201,14 +193,13 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
props.onBeforeClose(e);
}
if (e.detail.escPressed) {
// @ts-expect-error: todo check type
onClose(enrichEventWithDetails(e, { action: undefined }));
onClose(undefined, e.detail.escPressed);
}
};

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

const messageBoxId = useIsomorphicId();
Expand Down
Loading