From 369932262b353354ad36a988d7457af4805dc26a Mon Sep 17 00:00:00 2001 From: Amogh Shah Date: Fri, 13 Jan 2023 14:10:36 +0530 Subject: [PATCH] Fixed: dynamic tab page selection wont work if the page is re loaded from microflow (#3) * Issue resolved by iterating on each target div and it's LI element * executed lint:fix * version incremented Co-authored-by: Amogh.Shah --- package.json | 2 +- src/TabPageSelector.tsx | 83 +++++++++++++++++++---------------------- src/package.xml | 2 +- 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index ba24e8e..a82f538 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "tabpageselector", "widgetName": "TabPageSelector", - "version": "1.0.0", + "version": "1.0.1", "description": "Use this widget to dynamically set the active tab page.", "copyright": "2020 Mendix Technology BV", "author": "Amogh Shah", diff --git a/src/TabPageSelector.tsx b/src/TabPageSelector.tsx index 2b9ac88..49d0865 100644 --- a/src/TabPageSelector.tsx +++ b/src/TabPageSelector.tsx @@ -15,25 +15,25 @@ export default class TabPageSelector extends Component li"); - if (li == null) { - console.error("Unable find tab pages"); - return; - } - li.forEach((currentValue, _currentIndex, _listObj) => { - currentValue.addEventListener( - "click", - () => { - this.props.paneIndexByAttr?.setValue(Big(_currentIndex + 1)); - }, - false - ); - }, this); - } else { - throw new Error("Unable to find target Tab Container. Check above error logs for more info."); + this.checkTargetDivPresent(this.props.targetTabCtrl); + if (document.querySelectorAll(".mx-name-" + this.props.targetTabCtrl + " > ul > li").length == 0) { + console.error("Unable find tab pages"); + return; } + document + .querySelectorAll(".mx-name-" + this.props.targetTabCtrl + " > ul") + .forEach((ultValue, _ulIndex, _listObj) => { + ultValue.querySelectorAll("li").forEach((currentValue, _currentIndex, _listObj) => { + console.log(currentValue); + currentValue.addEventListener( + "click", + () => { + this.props.paneIndexByAttr?.setValue(Big(_currentIndex + 1)); + }, + false + ); + }, this); + }, this); } componentDidUpdate( _prevProps: Readonly, @@ -49,35 +49,30 @@ export default class TabPageSelector extends Component li"); - const li: HTMLElement = liList.item( - parseInt(this.props.paneIndexByAttr.value.toString(), 10) - 1 - ) as HTMLElement; - if (li == null) { - console.debug("Determined tab page number is: " + this.props.paneIndexByAttr.value); - console.error("Unable find tab page by specified number."); - } - if (li != null) { - li.click(); - } - } else { - throw new Error("Unable to find target Tab Container. Check above error logs for more info."); + this.checkTargetDivPresent(this.props.targetTabCtrl); + if (document.querySelectorAll(".mx-name-" + this.props.targetTabCtrl + " > ul > li").length == 0) { + console.error("Unable find tab pages"); + return; } + const liIndix: number = parseInt(this.props.paneIndexByAttr.value.toString(), 10) - 1; + document + .querySelectorAll(".mx-name-" + this.props.targetTabCtrl + " > ul") + .forEach((ultValue, _ulIndex, _listObj) => { + const liList = ultValue.querySelectorAll("li"); + const li: HTMLElement = liList.item(liIndix) as HTMLElement; + if (li == null) { + console.debug("Determined tab page number is: " + this.props.paneIndexByAttr.value); + console.error("Unable find tab page by specified number."); + } + if (li != null) { + li.click(); + } + }, this); } - getTargetDiv(targetTabCtrl: string): Element | null { + checkTargetDivPresent(targetTabCtrl: string): void { const divList = document.getElementsByClassName("mx-name-" + targetTabCtrl); - if (divList.length > 0) { - const div = divList.item(0); - if (div != null) { - return div; - } else { - console.error("Tab container DOM element found, but unable to get it's referance."); - } - } else { - console.error("Tab container DOM element not found. Please check provided taget tab container name."); + if (divList.length == 0) { + throw new Error("Tab container DOM element not found. Please check provided target tab container name."); } - return null; } } diff --git a/src/package.xml b/src/package.xml index f6c4a34..348d86b 100644 --- a/src/package.xml +++ b/src/package.xml @@ -1,6 +1,6 @@ - +