Skip to content

Commit

Permalink
fix(isCapture): guard use of currentTarget
Browse files Browse the repository at this point in the history
currentTarget was undefined, save it's value using
state before using in scroll listener.
  • Loading branch information
aronhelser committed Jun 4, 2018
1 parent b3ed903 commit 4f89a2d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/decorators/isCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

export default function (target) {
target.prototype.isCapture = function (currentTarget) {
if (!currentTarget) return false
const dataIsCapture = currentTarget.getAttribute('data-iscapture')
return dataIsCapture && dataIsCapture === 'true' || this.props.isCapture || false
}
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ class ReactTooltip extends React.Component {
extraClass: e.currentTarget.getAttribute('data-class') || this.props.class || this.props.className || '',
disable: e.currentTarget.getAttribute('data-tip-disable')
? e.currentTarget.getAttribute('data-tip-disable') === 'true'
: (this.props.disable || false)
: (this.props.disable || false),
currentTarget: e.currentTarget
}, () => {
if (scrollHide) this.addScrollListener(e)
if (scrollHide) this.addScrollListener(this.state.currentTarget)
this.updateTooltip(e)

if (getContent && Array.isArray(getContent)) {
Expand All @@ -328,7 +329,7 @@ class ReactTooltip extends React.Component {
const {afterShow} = this.props
const placeholder = this.getTooltipContent()
const delayTime = show ? 0 : parseInt(delayShow, 10)
const eventTarget = e.currentTarget
const eventTarget = e.currentTarget || e.target

if (this.isEmptyTip(placeholder) || disable) return // if the tooltip is empty, disable the tooltip
const updateState = () => {
Expand Down Expand Up @@ -390,8 +391,8 @@ class ReactTooltip extends React.Component {
* Add scroll eventlistener when tooltip show
* automatically hide the tooltip when scrolling
*/
addScrollListener (e) {
const isCaptureMode = this.isCapture(e.currentTarget)
addScrollListener (currentTarget) {
const isCaptureMode = this.isCapture(currentTarget)
window.addEventListener('scroll', this.hideTooltip, isCaptureMode)
}

Expand Down
12 changes: 7 additions & 5 deletions standalone/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,7 @@ Object.defineProperty(exports, "__esModule", {

exports.default = function (target) {
target.prototype.isCapture = function (currentTarget) {
if (!currentTarget) return false;
var dataIsCapture = currentTarget.getAttribute('data-iscapture');
return dataIsCapture && dataIsCapture === 'true' || this.props.isCapture || false;
};
Expand Down Expand Up @@ -1873,9 +1874,10 @@ var ReactTooltip = (0, _staticMethods2.default)(_class = (0, _windowListener2.de
delayHide: e.currentTarget.getAttribute('data-delay-hide') || this.props.delayHide || 0,
border: e.currentTarget.getAttribute('data-border') ? e.currentTarget.getAttribute('data-border') === 'true' : this.props.border || false,
extraClass: e.currentTarget.getAttribute('data-class') || this.props.class || this.props.className || '',
disable: e.currentTarget.getAttribute('data-tip-disable') ? e.currentTarget.getAttribute('data-tip-disable') === 'true' : this.props.disable || false
disable: e.currentTarget.getAttribute('data-tip-disable') ? e.currentTarget.getAttribute('data-tip-disable') === 'true' : this.props.disable || false,
currentTarget: e.currentTarget
}, function () {
if (scrollHide) _this5.addScrollListener(e);
if (scrollHide) _this5.addScrollListener(_this5.state.currentTarget);
_this5.updateTooltip(e);

if (getContent && Array.isArray(getContent)) {
Expand Down Expand Up @@ -1912,7 +1914,7 @@ var ReactTooltip = (0, _staticMethods2.default)(_class = (0, _windowListener2.de

var placeholder = this.getTooltipContent();
var delayTime = show ? 0 : parseInt(delayShow, 10);
var eventTarget = e.currentTarget;
var eventTarget = e.currentTarget || e.target;

if (this.isEmptyTip(placeholder) || disable) return; // if the tooltip is empty, disable the tooltip
var updateState = function updateState() {
Expand Down Expand Up @@ -1987,8 +1989,8 @@ var ReactTooltip = (0, _staticMethods2.default)(_class = (0, _windowListener2.de

}, {
key: 'addScrollListener',
value: function addScrollListener(e) {
var isCaptureMode = this.isCapture(e.currentTarget);
value: function addScrollListener(currentTarget) {
var isCaptureMode = this.isCapture(currentTarget);
window.addEventListener('scroll', this.hideTooltip, isCaptureMode);
}
}, {
Expand Down
4 changes: 2 additions & 2 deletions standalone/react-tooltip.min.js

Large diffs are not rendered by default.

0 comments on commit 4f89a2d

Please sign in to comment.