Skip to content

Commit 940597d

Browse files
fix(MessageBox): don't throw error if onClose is not passed (#6227)
Fixes #6215
1 parent 543a5f0 commit 940597d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,14 @@ describe('MessageBox', () => {
233233
cy.findByText('Confirmation').should('not.exist');
234234
cy.findByText('Custom Header').should('be.visible');
235235
});
236+
237+
it('#6215 - should not crash if no onClose handler is passed', () => {
238+
cy.on('uncaught:exception', (err) => {
239+
if (err.message.contains('onClose is not a function')) {
240+
throw err;
241+
}
242+
});
243+
cy.mount(<MessageBox open>Content</MessageBox>);
244+
cy.findByText('OK').click();
245+
});
236246
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
191191
const handleOnClose = (e) => {
192192
const { action } = e.target.dataset;
193193
stopPropagation(e);
194-
onClose(enrichEventWithDetails(e, { action }));
194+
if (typeof onClose === 'function') {
195+
onClose(enrichEventWithDetails(e, { action }));
196+
}
195197
};
196198

197199
const messageBoxId = useIsomorphicId();

0 commit comments

Comments
 (0)