Skip to content

Commit 7981491

Browse files
fix(MessageBox): don't throw error if onClose is not passed (#6226)
Fixes #6215
1 parent 70f9f27 commit 7981491

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,14 @@ describe('MessageBox', () => {
220220
cy.findByText('Confirmation').should('not.exist');
221221
cy.findByText('Custom Header').should('be.visible');
222222
});
223+
224+
it('#6215 - should not crash if no onClose handler is passed', () => {
225+
cy.on('uncaught:exception', (err) => {
226+
if (err.message.contains('onClose is not a function')) {
227+
throw err;
228+
}
229+
});
230+
cy.mount(<MessageBox open>Content</MessageBox>);
231+
cy.findByText('OK').click();
232+
});
223233
});

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,16 @@ const MessageBox = forwardRef<DialogDomRef, MessageBoxPropTypes>((props, ref) =>
190190
if (typeof props.onBeforeClose === 'function') {
191191
props.onBeforeClose(e);
192192
}
193-
if (e.detail.escPressed) {
193+
if (e.detail.escPressed && typeof onClose === 'function') {
194194
onClose(undefined, e.detail.escPressed);
195195
}
196196
};
197197

198198
const handleOnClose: ButtonPropTypes['onClick'] = (e) => {
199199
const { action } = e.currentTarget.dataset;
200-
onClose(action);
200+
if (typeof onClose === 'function') {
201+
onClose(action);
202+
}
201203
};
202204

203205
const messageBoxId = useId();

0 commit comments

Comments
 (0)