Skip to content

fix(MessageBox): don't throw error if onClose is not passed #6226

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 1 commit into from
Aug 19, 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
10 changes: 10 additions & 0 deletions packages/main/src/components/MessageBox/MessageBox.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,14 @@ describe('MessageBox', () => {
cy.findByText('Confirmation').should('not.exist');
cy.findByText('Custom Header').should('be.visible');
});

it('#6215 - should not crash if no onClose handler is passed', () => {
cy.on('uncaught:exception', (err) => {
if (err.message.contains('onClose is not a function')) {
throw err;
}
});
cy.mount(<MessageBox open>Content</MessageBox>);
cy.findByText('OK').click();
});
});
6 changes: 4 additions & 2 deletions packages/main/src/components/MessageBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,16 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
if (typeof props.onBeforeClose === 'function') {
props.onBeforeClose(e);
}
if (e.detail.escPressed) {
if (e.detail.escPressed && typeof onClose === 'function') {
onClose(undefined, e.detail.escPressed);
}
};

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

const messageBoxId = useId();
Expand Down
Loading