diff --git a/docs/pages/api-docs/click-away-listener.md b/docs/pages/api-docs/click-away-listener.md index c1dc348d395f6d..3711a7e33f223c 100644 --- a/docs/pages/api-docs/click-away-listener.md +++ b/docs/pages/api-docs/click-away-listener.md @@ -28,7 +28,7 @@ For instance, if you need to hide a menu when people click anywhere else on your | Name | Type | Default | Description | |:-----|:-----|:--------|:------------| | children * | element | | The wrapped element.
⚠️ [Needs to be able to hold a ref](/guides/composition/#caveat-with-refs). | -| disableReactTree | bool | false | The mouse event to listen to. You can disable the listener by providing `false`. | +| disableReactTree | bool | false | If `true`, the React tree is ignored and only the DOM tree is considered. This prop changes how portaled elements are handled. | | mouseEvent | 'onClick'
| 'onMouseDown'
| 'onMouseUp'
| false
| 'onClick' | The mouse event to listen to. You can disable the listener by providing `false`. | | onClickAway * | func | | Callback fired when a "click away" event is detected. | | touchEvent | 'onTouchEnd'
| 'onTouchStart'
| false
| 'onTouchEnd' | The touch event to listen to. You can disable the listener by providing `false`. | diff --git a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.d.ts b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.d.ts index 1f63430bf0b61e..375ed4f26a623a 100644 --- a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.d.ts +++ b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.d.ts @@ -6,7 +6,7 @@ export interface ClickAwayListenerProps { */ children: React.ReactNode; /** - * The mouse event to listen to. You can disable the listener by providing `false`. + * If `true`, the React tree is ignored and only the DOM tree is considered. This prop changes how portaled elements are handled. */ disableReactTree?: boolean; /** diff --git a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js index 6d56629582fb10..cf4d06a4dfc512 100644 --- a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js +++ b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js @@ -159,7 +159,7 @@ ClickAwayListener.propTypes = { */ children: elementAcceptingRef.isRequired, /** - * The mouse event to listen to. You can disable the listener by providing `false`. + * If `true`, the React tree is ignored and only the DOM tree is considered. This prop changes how portaled elements are handled. */ disableReactTree: PropTypes.bool, /** diff --git a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.test.js b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.test.js index 4d4cc77133cef5..e1f8964d9b00a4 100644 --- a/packages/material-ui/src/ClickAwayListener/ClickAwayListener.test.js +++ b/packages/material-ui/src/ClickAwayListener/ClickAwayListener.test.js @@ -1,7 +1,7 @@ import * as React from 'react'; import { expect } from 'chai'; import { spy } from 'sinon'; -import { createClientRender, fireEvent } from 'test/utils/createClientRender'; +import { createClientRender, fireEvent, screen } from 'test/utils/createClientRender'; import Portal from '../Portal'; import ClickAwayListener from './ClickAwayListener'; @@ -219,4 +219,50 @@ describe('', () => { fireEvent.click(document.body); expect(handleClickAway.callCount).to.equal(0); }); + + [ + ['onClick', false], + ['onClick', true], + ['onClickCapture', false], + ['onClickCapture', true], + ].forEach(([eventName, disableReactTree]) => { + it(`when 'disableRectTree=${disableReactTree}' ${eventName} triggers onClickAway if an outside target is removed`, () => { + const handleClickAway = spy(); + function Test() { + const [buttonShown, hideButton] = React.useReducer(() => false, true); + + return ( + + {buttonShown &&