From 81675b50cebadd890815d5441a9da167198a2c61 Mon Sep 17 00:00:00 2001 From: Willian Watanabe Date: Sun, 20 Dec 2020 11:34:17 -0300 Subject: [PATCH] fix(#1185): navigates through this._targets for placing popover event (fixes reintroduced bug) --- src/TooltipPopoverWrapper.js | 13 +++++++++++-- src/__tests__/TooltipPopoverWrapper.spec.js | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/TooltipPopoverWrapper.js b/src/TooltipPopoverWrapper.js index 8cd57e7ac..80a27f100 100644 --- a/src/TooltipPopoverWrapper.js +++ b/src/TooltipPopoverWrapper.js @@ -160,10 +160,19 @@ class TooltipPopoverWrapper extends React.Component { return delay; } + getCurrentTarget(target) { + if (!target) + return null; + const index = this._targets.indexOf(target); + if (index >= 0) + return this._targets[index]; + return this.getCurrentTarget(target.parentElement); + } + show(e) { if (!this.props.isOpen) { this.clearShowTimeout(); - this.currentTargetElement = e ? e.currentTarget || e.target : null; + this.currentTargetElement = e ? e.currentTarget || this.getCurrentTarget(e.target) : null; if (e && e.composedPath && typeof e.composedPath === 'function') { const path = e.composedPath(); this.currentTargetElement = (path && path[0]) || this.currentTargetElement; @@ -351,7 +360,7 @@ class TooltipPopoverWrapper extends React.Component { return ( { }); describe('multi target', () => { - let targets, targetContainer; + let targets, innerTarget, targetContainer; beforeEach(() => { targetContainer = document.createElement('div'); - targetContainer.innerHTML = `Target 1Target 2` + targetContainer.innerHTML = `Target 1Target 2Inner target` element.appendChild(targetContainer); targets = targetContainer.querySelectorAll('.example'); + innerTarget = targetContainer.querySelector('.inner_example'); }); afterEach(() => { element.removeChild(targetContainer); targets = null; + innerTarget = null; }); it("should attach tooltip on multiple target when a target selector matches multiple elements", () => { @@ -433,6 +435,19 @@ describe('Tooltip', () => { expect(isOpen).toBe(false); wrapper.detach(); }); + + it("should attach tooltip on second target with correct placement, when inner element is clicked", () => { + const wrapper = mount( + Yo!, + { attachTo: container }); + + innerTarget.dispatchEvent(new Event('click')); + jest.runTimersToTime(0) + expect(isOpen).toBe(true); + wrapper.setProps({ isOpen: true }); + expect(wrapper.find(PopperContent).props().target.className).toBe('example second'); + wrapper.detach(); + }); }); describe('delay', () => {