forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request react-bootstrap#698 from taion/overlay-root-close
[added] Enable rootClose for OverlayTrigger
- Loading branch information
Showing
6 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const positionerInstance = ( | ||
<ButtonToolbar> | ||
<OverlayTrigger trigger='click' placement='bottom' overlay={<Popover title='Popover bottom'><strong>Holy guacamole!</strong> Check this info.</Popover>}> | ||
<Button bsStyle='default'>Click</Button> | ||
</OverlayTrigger> | ||
<OverlayTrigger trigger='hover' placement='bottom' overlay={<Popover title='Popover bottom'><strong>Holy guacamole!</strong> Check this info.</Popover>}> | ||
<Button bsStyle='default'>Hover</Button> | ||
</OverlayTrigger> | ||
<OverlayTrigger trigger='focus' placement='bottom' overlay={<Popover title='Popover bottom'><strong>Holy guacamole!</strong> Check this info.</Popover>}> | ||
<Button bsStyle='default'>Focus</Button> | ||
</OverlayTrigger> | ||
<OverlayTrigger trigger='click' rootClose={true} placement='bottom' overlay={<Popover title='Popover bottom'><strong>Holy guacamole!</strong> Check this info.</Popover>}> | ||
<Button bsStyle='default'>Click + rootClose</Button> | ||
</OverlayTrigger> | ||
</ButtonToolbar> | ||
); | ||
|
||
React.render(positionerInstance, mountNode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import domUtils from './utils/domUtils'; | ||
import EventListener from './utils/EventListener'; | ||
|
||
// TODO: Merge this logic with dropdown logic once #526 is done. | ||
|
||
/** | ||
* Checks whether a node is within | ||
* a root nodes tree | ||
* | ||
* @param {DOMElement} node | ||
* @param {DOMElement} root | ||
* @returns {boolean} | ||
*/ | ||
function isNodeInRoot(node, root) { | ||
while (node) { | ||
if (node === root) { | ||
return true; | ||
} | ||
node = node.parentNode; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default class RootCloseWrapper extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.handleDocumentClick = this.handleDocumentClick.bind(this); | ||
this.handleDocumentKeyUp = this.handleDocumentKeyUp.bind(this); | ||
} | ||
|
||
bindRootCloseHandlers() { | ||
const doc = domUtils.ownerDocument(this); | ||
|
||
this._onDocumentClickListener = | ||
EventListener.listen(doc, 'click', this.handleDocumentClick); | ||
this._onDocumentKeyupListener = | ||
EventListener.listen(doc, 'keyup', this.handleDocumentKeyUp); | ||
} | ||
|
||
handleDocumentClick(e) { | ||
// If the click originated from within this component, don't do anything. | ||
if (isNodeInRoot(e.target, React.findDOMNode(this))) { | ||
return; | ||
} | ||
|
||
this.props.onRootClose(); | ||
} | ||
|
||
handleDocumentKeyUp(e) { | ||
if (e.keyCode === 27) { | ||
this.props.onRootClose(); | ||
} | ||
} | ||
|
||
unbindRootCloseHandlers() { | ||
if (this._onDocumentClickListener) { | ||
this._onDocumentClickListener.remove(); | ||
} | ||
|
||
if (this._onDocumentKeyupListener) { | ||
this._onDocumentKeyupListener.remove(); | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.bindRootCloseHandlers(); | ||
} | ||
|
||
render() { | ||
return React.Children.only(this.props.children); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unbindRootCloseHandlers(); | ||
} | ||
} | ||
RootCloseWrapper.propTypes = { | ||
onRootClose: React.PropTypes.func.isRequired | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters