Skip to content

Commit

Permalink
feat: o-tooltip opts.target can be HTMLElement
Browse files Browse the repository at this point in the history
Fixes #1609.
  • Loading branch information
aendra-rininsland committed Apr 26, 2024
1 parent 0eef525 commit 7c4663b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion components/o-tooltip/src/js/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Check failure on line 126 in components/o-tooltip/src/js/tooltip.js

View workflow job for this annotation

GitHub Actions / test (components/o-tooltip)

Unexpected negating the left operand of 'instanceof' operator
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) {
Expand Down
6 changes: 6 additions & 0 deletions components/o-tooltip/test/Tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 7c4663b

Please sign in to comment.