Skip to content

Commit 72ae189

Browse files
committed
Revert "Components: Remove redundant onClickOutside handler from Dropdown (#11253)"
This reverts commit 58725c4.
1 parent 3f6fd05 commit 72ae189

File tree

1 file changed

+14
-4
lines changed
  • packages/components/src/dropdown

1 file changed

+14
-4
lines changed

packages/components/src/dropdown/index.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import Popover from '../popover';
1111
class Dropdown extends Component {
1212
constructor() {
1313
super( ...arguments );
14-
1514
this.toggle = this.toggle.bind( this );
1615
this.close = this.close.bind( this );
16+
this.clickOutside = this.clickOutside.bind( this );
17+
this.bindContainer = this.bindContainer.bind( this );
1718
this.refresh = this.refresh.bind( this );
18-
1919
this.popoverRef = createRef();
20-
2120
this.state = {
2221
isOpen: false,
2322
};
@@ -39,6 +38,10 @@ class Dropdown extends Component {
3938
}
4039
}
4140

41+
bindContainer( ref ) {
42+
this.container = ref;
43+
}
44+
4245
/**
4346
* When contents change height due to user interaction,
4447
* `refresh` can be called to re-render Popover with correct
@@ -56,6 +59,12 @@ class Dropdown extends Component {
5659
} ) );
5760
}
5861

62+
clickOutside( event ) {
63+
if ( ! this.container.contains( event.target ) ) {
64+
this.close();
65+
}
66+
}
67+
5968
close() {
6069
this.setState( { isOpen: false } );
6170
}
@@ -75,14 +84,15 @@ class Dropdown extends Component {
7584
const args = { isOpen, onToggle: this.toggle, onClose: this.close };
7685

7786
return (
78-
<div className={ className }>
87+
<div className={ className } ref={ this.bindContainer }>
7988
{ renderToggle( args ) }
8089
{ isOpen && (
8190
<Popover
8291
className={ contentClassName }
8392
ref={ this.popoverRef }
8493
position={ position }
8594
onClose={ this.close }
95+
onClickOutside={ this.clickOutside }
8696
expandOnMobile={ expandOnMobile }
8797
headerTitle={ headerTitle }
8898
>

0 commit comments

Comments
 (0)