Skip to content

Commit cba398b

Browse files
authored
Merge pull request #7 from github/add-flow-definition-file
add flow definition file for the element API
2 parents 2e36b7d + 46708f6 commit cba398b

4 files changed

Lines changed: 35 additions & 39 deletions

File tree

index.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ class TabContainerElement extends HTMLElement {
1414
if (event.code === 'ArrowRight') {
1515
let index = currentIndex + 1
1616
if (index >= tabs.length) index = 0
17-
this.selectTab(index)
17+
selectTab(this, index)
1818
} else if (event.code === 'ArrowLeft') {
1919
let index = currentIndex - 1
2020
if (index < 0) index = tabs.length - 1
21-
this.selectTab(index)
21+
selectTab(this, index)
2222
} else if (event.code === 'Home') {
23-
this.selectTab(0)
23+
selectTab(this, 0)
2424
event.preventDefault()
2525
} else if (event.code === 'End') {
26-
this.selectTab(tabs.length - 1)
26+
selectTab(this, tabs.length - 1)
2727
event.preventDefault()
2828
}
2929
})
@@ -36,38 +36,38 @@ class TabContainerElement extends HTMLElement {
3636
if (!tab || !tab.closest('[role="tablist"]')) return
3737

3838
const index = tabs.indexOf(tab)
39-
this.selectTab(index)
39+
selectTab(this, index)
4040
})
4141
}
42+
}
4243

43-
selectTab(index: number) {
44-
const tabs = this.querySelectorAll('[role="tablist"] [role="tab"]')
45-
const panels = this.querySelectorAll('[role="tabpanel"]')
44+
function selectTab(tabContainer: TabContainerElement, index: number) {
45+
const tabs = tabContainer.querySelectorAll('[role="tablist"] [role="tab"]')
46+
const panels = tabContainer.querySelectorAll('[role="tabpanel"]')
4647

47-
for (const tab of tabs) {
48-
tab.setAttribute('aria-selected', 'false')
49-
tab.setAttribute('tabindex', '-1')
50-
}
51-
for (const panel of panels) {
52-
panel.hidden = true
53-
panel.setAttribute('tabindex', '0')
54-
}
48+
for (const tab of tabs) {
49+
tab.setAttribute('aria-selected', 'false')
50+
tab.setAttribute('tabindex', '-1')
51+
}
52+
for (const panel of panels) {
53+
panel.hidden = true
54+
panel.setAttribute('tabindex', '0')
55+
}
5556

56-
const tab = tabs[index]
57-
const panel = panels[index]
57+
const tab = tabs[index]
58+
const panel = panels[index]
5859

59-
tab.setAttribute('aria-selected', 'true')
60-
tab.removeAttribute('tabindex')
61-
tab.focus()
62-
panel.hidden = false
60+
tab.setAttribute('aria-selected', 'true')
61+
tab.removeAttribute('tabindex')
62+
tab.focus()
63+
panel.hidden = false
6364

64-
this.dispatchEvent(
65-
new CustomEvent('tab-container-change', {
66-
bubbles: true,
67-
detail: {relatedTarget: panel}
68-
})
69-
)
70-
}
65+
tabContainer.dispatchEvent(
66+
new CustomEvent('tab-container-change', {
67+
bubbles: true,
68+
detail: {relatedTarget: panel}
69+
})
70+
)
7171
}
7272

7373
if (!window.customElements.get('tab-container')) {

index.js.flow

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* @flow strict */
2+
3+
declare module '@github/tab-container-element' {
4+
declare export default class TabContainerElement extends HTMLElement { }
5+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prebuild": "npm run clean && npm run lint && mkdir dist",
1616
"build-umd": "BABEL_ENV=umd babel index.js -o dist/index.umd.js",
1717
"build-esm": "BABEL_ENV=esm babel index.js -o dist/index.esm.js",
18-
"build": "npm run build-umd && npm run build-esm",
18+
"build": "npm run build-umd && npm run build-esm && cp index.js.flow dist/index.esm.js.flow && cp index.js.flow dist/index.umd.js.flow",
1919
"pretest": "npm run build",
2020
"test": "karma start test/karma.config.js",
2121
"prepublishOnly": "npm run build"

test/test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,5 @@ describe('tab-container', function() {
7272
assert.equal(document.activeElement, tabs[0])
7373
assert.equal(counter, 2)
7474
})
75-
76-
it('selectTab(index) works', function() {
77-
const tabContainer = document.querySelector('tab-container')
78-
const panels = document.querySelectorAll('[role="tabpanel"]')
79-
80-
tabContainer.selectTab(2)
81-
assert(panels[0].hidden)
82-
assert(!panels[2].hidden)
83-
})
8475
})
8576
})

0 commit comments

Comments
 (0)