Skip to content

Commit

Permalink
[material-ui][Dialog] Prevent onClick on the root element from being …
Browse files Browse the repository at this point in the history
…overwritten (#41881)

Signed-off-by: Ryan Burr <ryan.burr2@gmail.com>
Co-authored-by: ZeeshanTamboli <zeeshan.tamboli@gmail.com>
  • Loading branch information
ryanburr and ZeeshanTamboli committed Apr 15, 2024
1 parent 357c50e commit 418fb02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/mui-material/src/Dialog/Dialog.js
Expand Up @@ -182,6 +182,7 @@ const Dialog = React.forwardRef(function Dialog(inProps, ref) {
fullWidth = false,
maxWidth = 'sm',
onBackdropClick,
onClick,
onClose,
open,
PaperComponent = Paper,
Expand Down Expand Up @@ -211,6 +212,10 @@ const Dialog = React.forwardRef(function Dialog(inProps, ref) {
backdropClick.current = event.target === event.currentTarget;
};
const handleBackdropClick = (event) => {
if (onClick) {
onClick(event);
}

// Ignore the events not coming from the "backdrop".
if (!backdropClick.current) {
return;
Expand Down Expand Up @@ -360,6 +365,10 @@ Dialog.propTypes /* remove-proptypes */ = {
* @deprecated Use the `onClose` prop with the `reason` argument to handle the `backdropClick` events.
*/
onBackdropClick: PropTypes.func,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* Callback fired when the component requests to be closed.
*
Expand Down
22 changes: 22 additions & 0 deletions packages/mui-material/src/Dialog/Dialog.test.js
Expand Up @@ -187,6 +187,28 @@ describe('<Dialog />', () => {
expect(onClose.callCount).to.equal(1);
});

it('calls onBackdropClick when onClick callback also exists', () => {
const onBackdropClick = spy();
const onClick = spy();
render(
<Dialog
onClick={onClick}
onClose={(event, reason) => {
if (reason === 'backdropClick') {
onBackdropClick();
}
}}
open
>
foo
</Dialog>,
);

clickBackdrop(screen);
expect(onBackdropClick.callCount).to.equal(1);
expect(onClick.callCount).to.equal(1);
});

it('should ignore the backdrop click if the event did not come from the backdrop', () => {
const onBackdropClick = spy();
const { getByRole } = render(
Expand Down

0 comments on commit 418fb02

Please sign in to comment.