Skip to content

Detach custom event listener #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/decorators/customEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,27 @@ const setUntargetItems = function (currentTarget, targetArray) {
}
}

let customListener
const customListeners = {
registry: new WeakMap(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we won't be able to accept the PR until we can build the standalone with babel-polyfill. Is there a way to use a standard object instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can define a property on given object. This is how WeakMap is usually polyfilled.

set (target, event, listener) {
if (this.registry.has(target)) {
const map = this.registry.get(target)
map[event] = listener
return this.registry
}

return this.registry.set(target, { [event]: listener })
},

get (target, event) {
const map = this.registry.get(target)
if (map !== undefined) {
return map[event]
}

return void 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not seen this return type before - I'd use 'undefined' if they behave similarly.

}
}

export default function (target) {
target.prototype.isCustomEvent = function (ele) {
Expand All @@ -48,8 +68,9 @@ export default function (target) {
const dataEventOff = ele.getAttribute('data-event-off') || eventOff

dataEvent.split(' ').forEach(event => {
ele.removeEventListener(event, customListener)
customListener = checkStatus.bind(this, dataEventOff)
ele.removeEventListener(event, customListeners.get(ele, event))
const customListener = checkStatus.bind(this, dataEventOff)
customListeners.set(ele, event, customListener)
ele.addEventListener(event, customListener, false)
})
if (dataEventOff) {
Expand All @@ -66,7 +87,7 @@ export default function (target) {
const dataEvent = event || ele.getAttribute('data-event')
const dataEventOff = eventOff || ele.getAttribute('data-event-off')

ele.removeEventListener(dataEvent, customListener)
ele.removeEventListener(dataEvent, customListeners.get(ele, event))
if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip)
}
}
34 changes: 28 additions & 6 deletions standalone/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,9 @@ exports.default = function (target) {
var dataEventOff = ele.getAttribute('data-event-off') || eventOff;

dataEvent.split(' ').forEach(function (event) {
ele.removeEventListener(event, customListener);
customListener = checkStatus.bind(_this, dataEventOff);
ele.removeEventListener(event, customListeners.get(ele, event));
var customListener = checkStatus.bind(_this, dataEventOff);
customListeners.set(ele, event, customListener);
ele.addEventListener(event, customListener, false);
});
if (dataEventOff) {
Expand All @@ -1261,11 +1262,13 @@ exports.default = function (target) {
var dataEvent = event || ele.getAttribute('data-event');
var dataEventOff = eventOff || ele.getAttribute('data-event-off');

ele.removeEventListener(dataEvent, customListener);
ele.removeEventListener(dataEvent, customListeners.get(ele, event));
if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip);
};
};

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
* Custom events to control showing and hiding of tooltip
*
Expand Down Expand Up @@ -1302,7 +1305,26 @@ var setUntargetItems = function setUntargetItems(currentTarget, targetArray) {
}
};

var customListener = void 0;
var customListeners = {
registry: new WeakMap(),
set: function set(target, event, listener) {
if (this.registry.has(target)) {
var map = this.registry.get(target);
map[event] = listener;
return this.registry;
}

return this.registry.set(target, _defineProperty({}, event, listener));
},
get: function get(target, event) {
var map = this.registry.get(target);
if (map !== undefined) {
return map[event];
}

return void 0;
}
};

},{}],14:[function(require,module,exports){
'use strict';
Expand Down Expand Up @@ -1806,9 +1828,9 @@ var ReactTooltip = (0, _staticMethods2.default)(_class = (0, _windowListener2.de
var content = void 0;
if (getContent) {
if (Array.isArray(getContent)) {
content = getContent[0] && getContent[0]();
content = getContent[0] && getContent[0](this.state.originTooltip);
} else {
content = getContent();
content = getContent(this.state.originTooltip);
}
}

Expand Down
Loading