@@ -15,78 +15,60 @@ export default class PgTabs extends HTMLElement {
1515 @Part ( ) $tabset : HTMLUListElement ;
1616 @Part ( ) $slot : HTMLSlotElement ;
1717
18- tabs : any [ ] = [ ] ;
18+ @Prop ( )
19+ #tabs: any [ ] = [ ] ;
20+
21+ #selectedTab: number = 0 ;
1922
2023 connectedCallback ( ) {
21- this . addEventListener ( 'tab' , this . handleTab . bind ( this ) ) ;
24+ this . addEventListener ( 'tab' , this . # handleTab. bind ( this ) ) ;
2225 this . $slot . addEventListener ( 'slotchange' , this . handleSlotChange . bind ( this ) ) ;
2326 forEach ( {
2427 container : this . $tabset ,
25- items : this . tabs ,
28+ items : this . # tabs,
2629 type : ( tab : any ) => {
2730 return PgPartialTab ;
28- }
29- } ) ;
30- }
31-
32- handleTab ( e : CustomEvent ) {
33- const { detail } = e ;
34- this . tabs . push ( detail ) ;
35-
36- /*list(
37- this.$tabset,
38- this.tabs,
39- 'id',
40- (tab: any) => {
41- const n = node<HTMLDivElement>(partialTab, {
42- button: {
43- innerText: tab.label
31+ } ,
32+ // @ts -ignore
33+ connect : ( $tab , tab , $tabs ) => {
34+ $tab . addEventListener ( 'select' , ( e : any ) => {
35+ const { index } = e . detail ;
36+ console . log ( index , this . #tabs[ e . detail . index ] ) ;
37+ const elements = this . $slot . assignedElements ( ) as PgTab [ ] ;
38+ elements [ this . #selectedTab] . hide ( ) ;
39+ elements [ index ] . show ( ) ;
40+ this . #selectedTab = index ;
41+ } ) ;
42+ $tab . addEventListener ( 'arrowleft' , ( e : any ) => {
43+ const { index } = e . detail ;
44+ if ( this . #tabs. length > 1 ) {
45+ if ( index === 0 ) {
46+ this . #selectedTab = this . #tabs. length - 1 ;
47+ } else {
48+ this . #selectedTab = index - 1 ;
49+ }
50+ $tabs [ this . #selectedTab] . focus ( ) ;
4451 }
4552 } ) ;
46- const button = n.querySelector<HTMLDivElement>('[part="tab"]');
47- button?.addEventListener('click', (e) => {
48- this.selectTab(tab.id);
49- this.dispatchEvent(new CustomEvent<any>('select', { detail: tab }));
53+ $tab . addEventListener ( 'arrowright' , ( e : any ) => {
54+ const { index } = e . detail ;
55+ if ( this . #tabs. length > 1 ) {
56+ if ( index === this . #tabs. length - 1 ) {
57+ this . #selectedTab = 0 ;
58+ } else {
59+ this . #selectedTab++ ;
60+ }
61+ $tabs [ this . #selectedTab] . focus ( ) ;
62+ }
5063 } ) ;
51- button?.addEventListener('keydown', this.handleTabKeypress.bind(this));
52- if (this.tabs[0].id === tab.id) {
53- button?.classList.add('active');
54- }
55- return n;
56- },
57- (tab, $item) => {
58-
5964 }
60- );*/
61- e . stopPropagation ( ) ;
65+ } ) ;
6266 }
6367
64- handleTabKeypress ( e ) {
65- const tabs = Array . from ( this . $tabset . children , x => x . children [ 0 ] ) ;
66- if ( tabs . length === 1 ) {
67- return ;
68- }
69- const index = tabs . findIndex ( x => x === e . target ) ;
70- const previousIndex = index === 0 ? tabs . length - 1 : index - 1 ;
71- const nextIndex = index === tabs . length - 1 ? 0 : index + 1 ;
72- const previousTab = this . tabs [ previousIndex ] ;
73- const previousButton = tabs [ previousIndex ] as HTMLButtonElement ;
74- const nextTab = this . tabs [ nextIndex ] ;
75- const nextButton = tabs [ nextIndex ] as HTMLButtonElement ;
76- switch ( e . key ) {
77- case 'ArrowLeft' :
78- if ( previousTab ) {
79- previousButton . focus ( ) ;
80- this . selectTab ( previousTab . id ) ;
81- }
82- break ;
83- case 'ArrowRight' :
84- if ( nextTab ) {
85- nextButton . focus ( ) ;
86- this . selectTab ( nextTab . id ) ;
87- }
88- break ;
89- }
68+ #handleTab( e : CustomEvent ) {
69+ const { detail } = e ;
70+ this . #tabs. push ( detail ) ;
71+ e . stopPropagation ( ) ;
9072 }
9173
9274 handleSlotChange ( e ) {
@@ -96,14 +78,4 @@ export default class PgTabs extends HTMLElement {
9678 }
9779 }
9880
99- selectTab ( id : string ) {
100- const tabs = Array . from ( this . $tabset . children ) as HTMLElement [ ] ;
101- for ( let tab of tabs ) {
102- tab . classList . toggle ( 'active' , tab . dataset [ 'key' ] === id ) ;
103- }
104- const elements = this . $slot . assignedElements ( ) as PgTab [ ] ;
105- elements . forEach ( e => e . hide ( ) ) ;
106- const tab = elements . find ( e => e . uuid === id ) as PgTab ;
107- tab . show ( ) ;
108- }
10981}
0 commit comments