Skip to content

Commit 1d69075

Browse files
committed
refactor(tabset): surf-1500 apply compatibility audit fixes for tabs
1 parent d2a8327 commit 1d69075

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

src/helix-ui/elements/HXTabsetElement.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ export class HXTabsetElement extends HXElement {
3636
this.$defaultAttribute('id', `tabset-${generateId()}`);
3737
this._setupIds();
3838
this.currentTab = Number(this.getAttribute('current-tab')) || 0;
39-
// FIXME: convert this.$tablist to private getter as this._tablist
40-
this.$tablist = this.querySelector('hx-tablist');
41-
this.$tablist.addEventListener('keyup', this._onKeyUp);
42-
this.$tablist.addEventListener('keydown', preventKeyScroll);
39+
this._tablist.addEventListener('keyup', this._onKeyUp);
40+
this._tablist.addEventListener('keydown', preventKeyScroll);
4341
this.tabs.forEach(tab => {
4442
tab.addEventListener('click', this._onTabClick);
4543
});
4644

47-
// FIXME: if current-tab set, ensure the associated tab and tabpanel is open
45+
if (this.hasAttribute('current-tab')) {
46+
this._activateTab(this.currentTab);
47+
}
4848
}
4949

5050
$onDisconnect () {
51-
this.$tablist.removeEventListener('keyup', this._onKeyUp);
52-
this.$tablist.removeEventListener('keydown', preventKeyScroll);
51+
this._tablist.removeEventListener('keyup', this._onKeyUp);
52+
this._tablist.removeEventListener('keydown', preventKeyScroll);
5353
this.tabs.forEach(tab => {
5454
tab.removeEventListener('click', this._onTabClick);
5555
});
@@ -77,8 +77,9 @@ export class HXTabsetElement extends HXElement {
7777
}
7878
set currentTab (idx) {
7979
// NOTE: Keep an eye on this logic for React compatibility
80-
// React _may_ set the currentTab property before connect
81-
// if so, we'll need to check if isConnected before continuing
80+
if (!this.isConnected) {
81+
return;
82+
}
8283

8384
if (isNaN(idx)) {
8485
throw new TypeError(`'currentTab' expects an numeric index. Got ${typeof idx} instead.`);
@@ -92,29 +93,30 @@ export class HXTabsetElement extends HXElement {
9293
}
9394

9495
/**
95-
* All `<hx-tab>` elements within the tabset.
96+
* All `<hx-tabpanel>` elements within the tabset.
9697
* @readonly
97-
* @type {HXTabElement[]}
98+
* @type {HXTabpanelElement[]}
9899
*/
99-
get tabs () {
100-
return Array.from(this.querySelectorAll('hx-tablist > hx-tab'));
100+
get tabpanels () {
101+
return Array.from(this.querySelectorAll('hx-tabpanel'));
101102
}
102103

103104
/**
104-
* All `<hx-tabpanel>` elements within the tabset.
105+
* All `<hx-tab>` elements within the tabset.
105106
* @readonly
106-
* @type {HXTabpanelElement[]}
107+
* @type {HXTabElement[]}
107108
*/
108-
get tabpanels () {
109-
return Array.from(this.querySelectorAll('hx-tabpanel'));
109+
get tabs () {
110+
return Array.from(this.querySelectorAll('hx-tablist > hx-tab'));
110111
}
111112

112113
/**
113114
* Select next tab in tabset.
114115
*/
115116
selectNext () {
116-
// FIXME: this.tabs might return an empty array
117-
// maybe short-circuit if not connected
117+
if (!this.isConnected) {
118+
return;
119+
}
118120

119121
// if current tab is the last tab
120122
if (this.currentTab === (this.tabs.length - 1)) {
@@ -131,8 +133,9 @@ export class HXTabsetElement extends HXElement {
131133
* Select previous tab in tabset.
132134
*/
133135
selectPrevious () {
134-
// FIXME: this.tabs might return an empty array
135-
// maybe short-circuit if not connected
136+
if (!this.isConnected) {
137+
return;
138+
}
136139

137140
// if current tab is the first tab
138141
if (this.currentTab === 0) {
@@ -145,6 +148,11 @@ export class HXTabsetElement extends HXElement {
145148
this.tabs[this.currentTab].focus();
146149
}
147150

151+
/** @private */
152+
get _tablist () {
153+
return this.querySelector('hx-tablist');
154+
}
155+
148156
/**
149157
* Handle navigating the tabs via arrow keys
150158
* @private
@@ -160,14 +168,6 @@ export class HXTabsetElement extends HXElement {
160168
}
161169
}
162170

163-
/**
164-
* @private
165-
* @todo migrate tab click listener logic to HXTabElement
166-
*/
167-
_onTabClick (evt) {
168-
this.currentTab = this.tabs.indexOf(evt.target);
169-
}
170-
171171
/** @private */
172172
_activateTab (idx) {
173173
this.tabs.forEach((tab, tabIdx) => {
@@ -186,6 +186,14 @@ export class HXTabsetElement extends HXElement {
186186
});
187187
}
188188

189+
/**
190+
* @private
191+
* @todo migrate tab click listener logic to HXTabElement
192+
*/
193+
_onTabClick (evt) {
194+
this.currentTab = this.tabs.indexOf(evt.target);
195+
}
196+
189197
/** @private */
190198
_setupIds () {
191199
let tabsetId = this.getAttribute('id');

0 commit comments

Comments
 (0)