Skip to content

Commit d264672

Browse files
authored
Fix/getRelativePosition NaN value with native event (#8459)
* fix bug where onClick value returned NaN because originalEvent does not exist * add test for this behaviour * test to async
1 parent aa5e0fe commit d264672

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/helpers/helpers.dom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function getPositionedStyle(styles, style, suffix) {
5353
const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot);
5454

5555
function getCanvasPosition(evt, canvas) {
56-
const e = evt.originalEvent || evt;
56+
const e = evt.native || evt;
5757
const touches = e.touches;
5858
const source = touches && touches.length ? touches[0] : e;
5959
const {offsetX, offsetY} = source;

test/specs/helpers.dom.tests.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,33 @@ describe('DOM helpers tests', function() {
399399
expect(Math.abs(pos3.x - Math.round((event.clientX - rect3.x - 10) / 360 * 400))).toBeLessThanOrEqual(1);
400400
expect(Math.abs(pos3.y - Math.round((event.clientY - rect3.y - 10) / 360 * 400))).toBeLessThanOrEqual(1);
401401
});
402+
403+
it('Should not return NaN with a custom event', async function() {
404+
let dataX = null;
405+
let dataY = null;
406+
const chart = window.acquireChart(
407+
{
408+
type: 'bar',
409+
data: {
410+
datasets: [{
411+
data: [{x: 'first', y: 10}, {x: 'second', y: 5}, {x: 'third', y: 15}]
412+
}]
413+
},
414+
options: {
415+
onHover: (e) => {
416+
const canvasPosition = Chart.helpers.getRelativePosition(e, chart);
417+
418+
dataX = canvasPosition.x;
419+
dataY = canvasPosition.y;
420+
}
421+
}
422+
});
423+
424+
const point = chart.getDatasetMeta(0).data[1];
425+
await jasmine.triggerMouseEvent(chart, 'mousemove', point);
426+
427+
expect(dataX).not.toEqual(NaN);
428+
expect(dataY).not.toEqual(NaN);
429+
});
402430
});
403431
});

0 commit comments

Comments
 (0)