From 7c4663bb7eb0e290ee616ca7cdc2f9752a2b9183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86ndra=20Rininsland?= Date: Fri, 26 Apr 2024 12:37:43 +0000 Subject: [PATCH] feat: o-tooltip opts.target can be HTMLElement Fixes #1609. --- components/o-tooltip/src/js/tooltip.js | 6 +++++- components/o-tooltip/test/Tooltip.test.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/components/o-tooltip/src/js/tooltip.js b/components/o-tooltip/src/js/tooltip.js index 83c7379a97..c247d2f1aa 100755 --- a/components/o-tooltip/src/js/tooltip.js +++ b/components/o-tooltip/src/js/tooltip.js @@ -31,7 +31,7 @@ class Tooltip { Tooltip._tooltips.set(this.tooltipEl, this); - this.targetNode = document.getElementById(this.opts.target); + this.targetNode = typeof this.opts.target === 'string' ? document.getElementById(this.opts.target) : this.opts.target; this.target = new Tooltip.Target(this.targetNode); this.tooltipPosition = this._getConfiguredTooltipPosition(); this.tooltipAlignment = null; @@ -123,6 +123,10 @@ class Tooltip { Tooltip.throwError("tooltip.target is not set. An target for the tooltip to point at must be provided"); } + if (!typeof opts.target === 'string' || !opts.target instanceof HTMLElement) { + Tooltip.throwError("tooltip.target is invalid, it must be either an ID selector string or HTMLElement"); + } + // Check that the value of tooltip position is valid. Default to below. if (opts.position) { if (Tooltip.validTooltipPositions.indexOf(opts.position) === -1) { diff --git a/components/o-tooltip/test/Tooltip.test.js b/components/o-tooltip/test/Tooltip.test.js index be5812a833..fb6ac4ecc8 100644 --- a/components/o-tooltip/test/Tooltip.test.js +++ b/components/o-tooltip/test/Tooltip.test.js @@ -254,6 +254,12 @@ describe("Tooltip", () => { const opts = Tooltip.checkOptions({"target": "#el"}); proclaim.isObject(opts); }); + + it("does not error if HTMLElement is passed as opts.target", () => { + const targetEl = document.createElement('div'); + Tooltip.checkOptions({target: targetEl}); + proclaim.isFalse(throwStub.called); + }) }); describe('constructElement', () => {