Skip to content

Commit 6bcb471

Browse files
github-actions[bot]ryanburrZeeshanTamboli
authored
[material-ui][Dialog] Prevent onClick on the root element from being overwritten (@ryanburr) (#41914)
Signed-off-by: Ryan Burr <[email protected]> Co-authored-by: Ryan Burr <[email protected]> Co-authored-by: ZeeshanTamboli <[email protected]>
1 parent 3faf9c3 commit 6bcb471

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/mui-material/src/Dialog/Dialog.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const Dialog = React.forwardRef(function Dialog(inProps, ref) {
182182
fullWidth = false,
183183
maxWidth = 'sm',
184184
onBackdropClick,
185+
onClick,
185186
onClose,
186187
open,
187188
PaperComponent = Paper,
@@ -211,6 +212,10 @@ const Dialog = React.forwardRef(function Dialog(inProps, ref) {
211212
backdropClick.current = event.target === event.currentTarget;
212213
};
213214
const handleBackdropClick = (event) => {
215+
if (onClick) {
216+
onClick(event);
217+
}
218+
214219
// Ignore the events not coming from the "backdrop".
215220
if (!backdropClick.current) {
216221
return;
@@ -360,6 +365,10 @@ Dialog.propTypes /* remove-proptypes */ = {
360365
* @deprecated Use the `onClose` prop with the `reason` argument to handle the `backdropClick` events.
361366
*/
362367
onBackdropClick: PropTypes.func,
368+
/**
369+
* @ignore
370+
*/
371+
onClick: PropTypes.func,
363372
/**
364373
* Callback fired when the component requests to be closed.
365374
*

packages/mui-material/src/Dialog/Dialog.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ describe('<Dialog />', () => {
187187
expect(onClose.callCount).to.equal(1);
188188
});
189189

190+
it('calls onBackdropClick when onClick callback also exists', () => {
191+
const onBackdropClick = spy();
192+
const onClick = spy();
193+
render(
194+
<Dialog
195+
onClick={onClick}
196+
onClose={(event, reason) => {
197+
if (reason === 'backdropClick') {
198+
onBackdropClick();
199+
}
200+
}}
201+
open
202+
>
203+
foo
204+
</Dialog>,
205+
);
206+
207+
clickBackdrop(screen);
208+
expect(onBackdropClick.callCount).to.equal(1);
209+
expect(onClick.callCount).to.equal(1);
210+
});
211+
190212
it('should ignore the backdrop click if the event did not come from the backdrop', () => {
191213
const onBackdropClick = spy();
192214
const { getByRole } = render(

0 commit comments

Comments
 (0)