Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add activeTab, selectedTabIndex props #85

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/tab-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export class TabContainerElement extends HTMLElement {
)
}

get activeTab() {
return this.#tabs[this.selectedTabIndex]
}

get activePanel() {
return this.#panelSlot.assignedNodes()[0] as HTMLElement
}
Expand Down Expand Up @@ -181,7 +185,7 @@ export class TabContainerElement extends HTMLElement {
const tabs = this.#tabs
if (!tabs.includes(tab as HTMLElement)) return

const currentIndex = tabs.indexOf(tabs.find(e => e.matches('[aria-selected="true"]'))!)
const currentIndex = this.selectedTabIndex
const vertical = tab.closest('[role="tablist"]')?.getAttribute('aria-orientation') === 'vertical'
const prevTab = event.code === 'ArrowLeft' || (vertical && event.code === 'ArrowUp')
const nextTab = event.code === 'ArrowRight' || (vertical && event.code === 'ArrowDown')
Expand Down Expand Up @@ -218,6 +222,14 @@ export class TabContainerElement extends HTMLElement {
}
}

get selectedTabIndex(): number {
return this.#tabs.findIndex(el => el.matches('[aria-selected=true]'))
}

set selectedTabIndex(i: number) {
this.selectTab(i)
}

selectTab(index: number): void {
if (!this.#setupComplete) {
const tabListSlot = this.#tabListSlot
Expand Down Expand Up @@ -276,7 +288,7 @@ export class TabContainerElement extends HTMLElement {
for (const el of afterSlotted) el.setAttribute('slot', 'after-panels')
}
const defaultTab = Number(this.getAttribute('default-tab') || -1)
const defaultIndex = defaultTab >= 0 ? defaultTab : this.#tabs.findIndex(el => el.matches('[aria-selected=true]'))
const defaultIndex = defaultTab >= 0 ? defaultTab : this.selectedTabIndex
index = index >= 0 ? index : Math.max(0, defaultIndex)
}

Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ describe('tab-container', function () {
'change events point to second panel',
)
assert.equal(document.activeElement, tabs[1])
assert.equal(tabContainer.activeTab, tabs[1])
assert.equal(tabContainer.selectedTabIndex, 1)
})

it('keyboard shortcuts work and `tab-container-changed` events are dispatched', function () {
Expand All @@ -187,6 +189,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [true, false, false], 'First tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'First panel is visible')
assert.equal(document.activeElement, tabs[0])
assert.equal(tabContainer.activeTab, tabs[0])
assert.equal(tabContainer.selectedTabIndex, 0)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand Down Expand Up @@ -280,6 +284,8 @@ describe('tab-container', function () {
'change events point to second panel',
)
assert.equal(document.activeElement, tabs[1])
assert.equal(tabContainer.activeTab, tabs[1])
assert.equal(tabContainer.selectedTabIndex, 1)
})

it('result in noop, when selectTab receives out of bounds index', function () {
Expand Down Expand Up @@ -417,6 +423,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, false, true], 'Third tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, true, false], 'Third panel is visible')
assert.equal(document.activeElement, tabs[2])
assert.equal(tabContainer.activeTab, tabs[2])
assert.equal(tabContainer.selectedTabIndex, 2)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -439,6 +447,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [true, false, false], 'First tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'First panel is visible')
assert.equal(document.activeElement, tabs[0])
assert.equal(tabContainer.activeTab, tabs[0])
assert.equal(tabContainer.selectedTabIndex, 0)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -461,6 +471,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
assert.equal(document.activeElement, tabs[1])
assert.equal(tabContainer.activeTab, tabs[1])
assert.equal(tabContainer.selectedTabIndex, 1)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -483,6 +495,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, false, true], 'Third tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, true, false], 'Third panel is visible')
assert.equal(document.activeElement, tabs[2])
assert.equal(tabContainer.activeTab, tabs[2])
assert.equal(tabContainer.selectedTabIndex, 2)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -506,6 +520,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, false, true], 'Third tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, true, false], 'Third panel is visible')
assert.equal(document.activeElement, tabs[2])
assert.equal(tabContainer.activetab, tabs[2])
owenniblock marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(tabContainer.selectedTabIndex, 2)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -528,6 +544,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [true, false, false], 'First tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'First panel is visible')
assert.equal(document.activeElement, tabs[0])
assert.equal(tabContainer.activeTab, tabs[0])
assert.equal(tabContainer.selectedTabIndex, 0)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -550,6 +568,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
assert.equal(document.activeElement, tabs[1])
assert.equal(tabContainer.activeTab, tabs[1])
assert.equal(tabContainer.selectedTabIndex, 1)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand All @@ -572,6 +592,8 @@ describe('tab-container', function () {
assert.deepStrictEqual(tabs.map(isSelected), [false, false, true], 'Third tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, true, false], 'Third panel is visible')
assert.equal(document.activeElement, tabs[2])
assert.equal(tabContainer.activetab, tabs[2])
owenniblock marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(tabContainer.selectedTabIndex, 2)
assert.equal(events.length, 2, 'tab-container-change(d) called')
assert.deepStrictEqual(
events.map(e => e.type),
Expand Down
Loading