Skip to content

Commit afc6ed5

Browse files
committed
fix(pat autotoc): Fix for having duplicated ids incase of multiple autotoc elements
1 parent b7ffd36 commit afc6ed5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/pat/autotoc/autotoc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export default Base.extend({
2828

2929
var self = this;
3030

31+
const autotocInstances = $(".pat-autotoc");
32+
self.instanceIndex = autotocInstances.index(self.$el);
33+
3134
const $nav = $("<nav/>")
3235
.attr("aria-label", "Tab Navigation");
3336

@@ -81,7 +84,7 @@ export default Base.extend({
8184
const $section = $level.closest(self.options.section);
8285
let sectionId = $section.prop("id");
8386
let sectionHash = `#${sectionId}`;
84-
const tabId = `${self.options.IDPrefix}${self.name}-${i}`;
87+
const tabId = `${self.options.IDPrefix}${self.name}-${self.instanceIndex}-${i}`;
8588
const tabHash = `#${tabId}`;
8689
const levelId = `${tabId}-pane`;
8790
const levelHash = `#${levelId}`;

src/pat/autotoc/autotoc.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@ describe("1 - AutoTOC", function () {
5858
await utils.timeout(1);
5959

6060
const firstTab = document.querySelectorAll(".nav > li > a")[0]
61-
expect(firstTab.getAttribute("id")).toEqual(
62-
"autotoc-item-autotoc-0"
61+
expect(firstTab.getAttribute("id")).toEqual("autotoc-item-autotoc-0-0");
62+
expect(firstTab.getAttribute("data-bs-target")).toEqual(
63+
"#autotoc-item-autotoc-0-0-pane",
6364
);
64-
expect(
65-
firstTab.getAttribute("data-bs-target")
66-
).toEqual("#autotoc-item-autotoc-0-pane");
6765
});
6866
it("can have custom levels", async function () {
6967
this.$el.attr("data-pat-autotoc", "levels: h1");

0 commit comments

Comments
 (0)