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

Change default-tab to default-tab-index #87

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
"kind": "field",
"name": "selectedTabIndex"
},
{
"kind": "field",
"name": "defaultTabIndex"
},
{
"kind": "method",
"name": "selectTab",
Expand Down Expand Up @@ -550,6 +554,13 @@
"text": "number"
}
},
{
"kind": "field",
"name": "defaultTabIndex",
"type": {
"text": "number"
}
},
{
"kind": "method",
"name": "selectTab",
Expand Down
19 changes: 18 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ <h2>Set initially selected tab</h2>
</div>
</tab-container>

<h2>Set default tab</h2>

<tab-container default-tab-index="1">
<button type="button" id="tab-one" role="tab">Tab one</button>
<button type="button" id="tab-two" role="tab">Tab two</button>
<button type="button" id="tab-three" role="tab">Tab three</button>
<div role="tabpanel" aria-labelledby="tab-one" hidden>
Panel 1
</div>
<div role="tabpanel" aria-labelledby="tab-two">
Panel 2
</div>
<div role="tabpanel" aria-labelledby="tab-three" hidden>
Panel 3
</div>
</tab-container>

<h2>Panel with extra buttons</h2>

<tab-container>
Expand All @@ -123,7 +140,7 @@ <h2>Panel with extra buttons</h2>

</main>

<!-- <script src="../dist/index.js" type="module"></script> -->
<!--<script src="../dist/index.js" type="module"></script>-->
<script src="https://unpkg.com/@github/tab-container-element@latest/dist/index.js" type="module"></script>
</body>
</html>
10 changes: 9 additions & 1 deletion src/tab-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ export class TabContainerElement extends HTMLElement {
this.selectTab(i)
}

get defaultTabIndex(): number {
return Number(this.getAttribute('default-tab') || -1)
}

set defaultTabIndex(index: number) {
this.setAttribute('default-tab', String(index))
}

selectTab(index: number): void {
if (!this.#setupComplete) {
const tabListSlot = this.#tabListSlot
Expand Down Expand Up @@ -310,7 +318,7 @@ export class TabContainerElement extends HTMLElement {
for (const el of afterTabSlotted) el.setAttribute('slot', 'after-tabs')
for (const el of afterSlotted) el.setAttribute('slot', 'after-panels')
}
const defaultTab = Number(this.getAttribute('default-tab') || -1)
const defaultTab = this.defaultTabIndex
const defaultIndex = defaultTab >= 0 ? defaultTab : this.selectedTabIndex
index = index >= 0 ? index : Math.max(0, defaultIndex)
}
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ describe('tab-container', function () {
})
})

describe('after tree insertion with default-tab', function () {
describe('after tree insertion with defaulTabIndex', function () {
beforeEach(function () {
document.body.innerHTML = `
<tab-container default-tab=1>
<tab-container default-tab="1">
<button type="button" role="tab">Tab one</button>
<button type="button" role="tab">Tab two</button>
<button type="button" role="tab">Tab three</button>
Expand Down
Loading