@@ -3,67 +3,81 @@ import { Big } from "big.js";
33import { TabPageSelectorContainerProps } from "../typings/TabPageSelectorProps" ;
44
55export default class TabPageSelector extends Component < TabPageSelectorContainerProps > {
6- render ( ) : ReactNode {
7- if ( this . props . paneIndexByAttr === undefined )
8- {
6+ render ( ) : ReactNode {
7+ if ( this . props . paneIndexByAttr === undefined ) {
98 console . error ( "Tab page selector not specified. Please specify the attribute to determine tab pane index." ) ;
109 return ;
11- }
12- if ( this . props . targetTabCtrl === undefined || this . props . targetTabCtrl . trim ( ) == '' )
13- {
10+ }
11+ if ( this . props . targetTabCtrl === undefined || this . props . targetTabCtrl . trim ( ) === "" ) {
1412 console . error ( "Target tab container name not specified. Please specify the target tab container name." ) ;
1513 return ;
16- }
14+ }
1715 return "" ;
1816 }
1917 componentDidMount ( ) : void {
20- let div = document . getElementsByClassName ( "mx-name-" + this . props . targetTabCtrl ) ;
21- if ( div . length > 0 && div . item ( 0 ) != null )
22- {
23- let li = div . item ( 0 ) ! . querySelectorAll ( 'ul > li' ) ;
24- if ( li == null )
25- {
26- console . error ( "Unable find tab pane index by specified index." ) ;
18+ const div = this . getTargetDiv ( this . props . targetTabCtrl ) ;
19+ if ( div != null ) {
20+ const li = div . querySelectorAll ( "ul > li" ) ;
21+ if ( li == null ) {
22+ console . error ( "Unable find tab pages" ) ;
2723 return ;
28- }
29- li . forEach (
30- ( currentValue , _currentIndex , _listObj ) => {
31- currentValue . addEventListener ( "click" , ( ) => {
32- this . props . paneIndexByAttr ?. setValue ( Big ( _currentIndex + 1 ) ) ;
33- } , false ) ;
34- } ,
35- this
36- ) ;
37- }
38- else
39- {
40- console . error ( "Tab container DOM element not found. Please check provided taget tab container name." ) ;
41- return ;
24+ }
25+ li . forEach ( ( currentValue , _currentIndex , _listObj ) => {
26+ currentValue . addEventListener (
27+ "click" ,
28+ ( ) => {
29+ this . props . paneIndexByAttr ?. setValue ( Big ( _currentIndex + 1 ) ) ;
30+ } ,
31+ false
32+ ) ;
33+ } , this ) ;
34+ } else {
35+ throw new Error ( "Unable to find target Tab Container. Check above error logs for more info." ) ;
4236 }
4337 }
44- componentDidUpdate ( _prevProps : Readonly < TabPageSelectorContainerProps > , _prevState : Readonly < { } > , _snapshot ?: any ) : void {
45- if ( this . props . paneIndexByAttr ! . value === undefined )
46- {
38+ componentDidUpdate (
39+ _prevProps : Readonly < TabPageSelectorContainerProps > ,
40+ _prevState : Readonly < { } > ,
41+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42+ _snapshot ?: any
43+ ) : void {
44+ if ( this . props . paneIndexByAttr . value === undefined ) {
4745 console . error ( "Tab page selector not specified. Please specify the attribute to determine tab pane index." ) ;
4846 return ;
49- }
50- if ( this . props . paneIndexByAttr ! . value . lte ( Big ( 0 ) ) )
51- {
47+ }
48+ if ( this . props . paneIndexByAttr . value . lte ( Big ( 0 ) ) ) {
5249 console . error ( "Tab page selector number cannot be less than or equal to zero." ) ;
5350 return ;
5451 }
55- let div = document . getElementsByClassName ( "mx-name-" + this . props . targetTabCtrl ) ;
56- if ( div . length > 0 && div . item ( 0 ) != null )
57- {
58- let liList = div . item ( 0 ) ! . querySelectorAll ( 'ul > li' ) ;
59- let li : HTMLElement = liList . item ( parseInt ( this . props . paneIndexByAttr ! . value ! . toString ( ) ! ) - 1 ) as HTMLElement ;
60- if ( li == null )
61- {
62- console . debug ( "Determined tab page index is: " + this . props . paneIndexByAttr ! . value ) ;
63- console . error ( "Unable find tab page index by specified index." ) ;
64- }
65- if ( li != null )
66- li . click ( )
67- }
52+ const div = this . getTargetDiv ( this . props . targetTabCtrl ) ;
53+ if ( div != null ) {
54+ const liList = div . querySelectorAll ( "ul > li" ) ;
55+ const li : HTMLElement = liList . item (
56+ parseInt ( this . props . paneIndexByAttr . value . toString ( ) , 10 ) - 1
57+ ) as HTMLElement ;
58+ if ( li == null ) {
59+ console . debug ( "Determined tab page number is: " + this . props . paneIndexByAttr . value ) ;
60+ console . error ( "Unable find tab page by specified number." ) ;
61+ }
62+ if ( li != null ) {
63+ li . click ( ) ;
64+ }
65+ } else {
66+ throw new Error ( "Unable to find target Tab Container. Check above error logs for more info." ) ;
67+ }
68+ }
69+ getTargetDiv ( targetTabCtrl : string ) : Element | null {
70+ const divList = document . getElementsByClassName ( "mx-name-" + targetTabCtrl ) ;
71+ if ( divList . length > 0 ) {
72+ const div = divList . item ( 0 ) ;
73+ if ( div != null ) {
74+ return div ;
75+ } else {
76+ console . error ( "Tab container DOM element found, but unable to get it's referance." ) ;
77+ }
78+ } else {
79+ console . error ( "Tab container DOM element not found. Please check provided taget tab container name." ) ;
80+ }
81+ return null ;
6882 }
6983}
0 commit comments