Skip to content

Commit 14ac92f

Browse files
author
Ryan A. Johnson
committed
feat(HXTabsetElement): add declarative functionality
* Add `selectPrevious()` to activate previous tab * Add `selectNext()` to activate next tab * Refactor source to use `$` API from HXElement * Add JSDoc comments
1 parent df59fe0 commit 14ac92f

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

docs/elements/hx-tabset/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@
4242
</hx-def>
4343
</hx-dl>
4444
</section>
45+
46+
<section>
47+
<h2 id="methods">Methods</h2>
48+
<dl>
49+
<dt>selectNext()</dt>
50+
<dd>
51+
Selects the next tab in the tabset. If the last tab is currently active,
52+
selects the first tab.
53+
</dd>
54+
</dl>
55+
<dl>
56+
<dt>selectPrevious()</dt>
57+
<dd>
58+
Selects the previous tab in the tabset. If the first tab is currently
59+
active, selects the last tab.
60+
</dd>
61+
</dl>
62+
</section>
4563
{% endblock %}
4664

4765
{% block attributes %}

src/helix-ui/elements/HXTabsetElement.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ export class HXTabsetElement extends HXElement {
5757
$onAttributeChange (attr, oldVal, newVal) {
5858
if (attr === 'current-tab') {
5959
if (!isNaN(newVal)) {
60-
this._openTab(Number(newVal));
60+
this._activateTab(Number(newVal));
6161
this.$emit('tabchange');
6262
}
6363
}
6464
}
6565

66+
/**
67+
* Zero-based index of the currently active tab.
68+
* @type {Number}
69+
*/
6670
get currentTab () {
6771
return Number(this.getAttribute('current-tab') || 0);
6872
}
69-
7073
set currentTab (idx) {
7174
if (isNaN(idx)) {
7275
throw new TypeError(`'currentTab' expects an numeric index. Got ${typeof idx} instead.`);
@@ -79,15 +82,28 @@ export class HXTabsetElement extends HXElement {
7982
this.setAttribute('current-tab', idx);
8083
}
8184

85+
/**
86+
* All `<hx-tab>` elements within the tabset.
87+
* @readonly
88+
* @type {HXTabElement[]}
89+
*/
8290
get tabs () {
8391
return Array.from(this.querySelectorAll('hx-tablist > hx-tab'));
8492
}
8593

94+
/**
95+
* All `<hx-tabpanel>` elements within the tabset.
96+
* @readonly
97+
* @type {HXTabpanelElement[]}
98+
*/
8699
get tabpanels () {
87100
return Array.from(this.querySelectorAll('hx-tabpanel'));
88101
}
89102

90-
_selectNext () {
103+
/**
104+
* Select next tab in tabset.
105+
*/
106+
selectNext () {
91107
// if current tab is the last tab
92108
if (this.currentTab === (this.tabs.length - 1)) {
93109
// select first
@@ -97,9 +113,12 @@ export class HXTabsetElement extends HXElement {
97113
this.currentTab += 1;
98114
}
99115
this.tabs[this.currentTab].focus();
100-
}//_selectNext()
116+
}
101117

102-
_selectPrevious () {
118+
/**
119+
* Select previous tab in tabset.
120+
*/
121+
selectPrevious () {
103122
// if current tab is the first tab
104123
if (this.currentTab === 0) {
105124
// select last
@@ -109,24 +128,29 @@ export class HXTabsetElement extends HXElement {
109128
this.currentTab -= 1;
110129
}
111130
this.tabs[this.currentTab].focus();
112-
}//_selectPrevious()
131+
}
113132

114-
// Handle navigating the tabs via arrow keys
133+
/**
134+
* Handle navigating the tabs via arrow keys
135+
* @private
136+
*/
115137
_onKeyUp (evt) {
116138
if (evt.keyCode === KEYS.Right) {
117-
this._selectNext();
139+
this.selectNext();
118140
}
119141

120142
if (evt.keyCode === KEYS.Left) {
121-
this._selectPrevious();
143+
this.selectPrevious();
122144
}
123-
}//_onKeyUp()
145+
}
124146

147+
/** @private */
125148
_onTabClick (evt) {
126149
this.currentTab = this.tabs.indexOf(evt.target);
127150
}
128151

129-
_openTab (idx) {
152+
/** @private */
153+
_activateTab (idx) {
130154
this.tabs.forEach((tab, tabIdx) => {
131155
if (idx === tabIdx) {
132156
tab.current = true;
@@ -143,6 +167,7 @@ export class HXTabsetElement extends HXElement {
143167
});
144168
}
145169

170+
/** @private */
146171
_setupIds () {
147172
let tabsetId = this.getAttribute('id');
148173
this.tabs.forEach((tab, idx) => {
@@ -168,5 +193,5 @@ export class HXTabsetElement extends HXElement {
168193
tab.setAttribute('aria-controls', tabpanelId);
169194
tabpanel.setAttribute('aria-labelledby', tabId);
170195
});
171-
}//_setupIds
196+
}
172197
}

0 commit comments

Comments
 (0)