Skip to content

Commit

Permalink
Revert "Components: Remove redundant onClickOutside handler from Drop…
Browse files Browse the repository at this point in the history
…down (#11253)"

This reverts commit 58725c4.
  • Loading branch information
aduth committed Nov 8, 2018
1 parent 3f6fd05 commit 72ae189
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/components/src/dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import Popover from '../popover';
class Dropdown extends Component {
constructor() {
super( ...arguments );

this.toggle = this.toggle.bind( this );
this.close = this.close.bind( this );
this.clickOutside = this.clickOutside.bind( this );
this.bindContainer = this.bindContainer.bind( this );
this.refresh = this.refresh.bind( this );

this.popoverRef = createRef();

this.state = {
isOpen: false,
};
Expand All @@ -39,6 +38,10 @@ class Dropdown extends Component {
}
}

bindContainer( ref ) {
this.container = ref;
}

/**
* When contents change height due to user interaction,
* `refresh` can be called to re-render Popover with correct
Expand All @@ -56,6 +59,12 @@ class Dropdown extends Component {
} ) );
}

clickOutside( event ) {
if ( ! this.container.contains( event.target ) ) {
this.close();
}
}

close() {
this.setState( { isOpen: false } );
}
Expand All @@ -75,14 +84,15 @@ class Dropdown extends Component {
const args = { isOpen, onToggle: this.toggle, onClose: this.close };

return (
<div className={ className }>
<div className={ className } ref={ this.bindContainer }>
{ renderToggle( args ) }
{ isOpen && (
<Popover
className={ contentClassName }
ref={ this.popoverRef }
position={ position }
onClose={ this.close }
onClickOutside={ this.clickOutside }
expandOnMobile={ expandOnMobile }
headerTitle={ headerTitle }
>
Expand Down

0 comments on commit 72ae189

Please sign in to comment.