Skip to content

Commit 047592d

Browse files
fix(tabs): only set aria-controls attribute when tab panels exist (#4270)
* fix(tabs): only set aria-controls attribute when tab panels exist * fix(tabs): only set aria-controls when matching panel index exists --------- Co-authored-by: Maximilian Franzke <[email protected]>
1 parent c2dc849 commit 047592d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/components/src/components/tabs/tabs.lite.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ export default function DBTabs(props: DBTabsProps) {
8888
const tabItems = Array.from<Element>(
8989
_ref.getElementsByClassName('db-tab-item')
9090
);
91+
const tabPanels = Array.from<Element>(
92+
_ref.querySelectorAll(
93+
':is(:scope > .db-tab-panel, :scope > db-tab-panel > .db-tab-panel)'
94+
)
95+
);
9196
for (const tabItem of tabItems) {
9297
const index: number = tabItems.indexOf(tabItem);
9398
const label = tabItem.querySelector('label');
@@ -97,12 +102,14 @@ export default function DBTabs(props: DBTabsProps) {
97102
if (!input.id) {
98103
const tabId = `${state._name}-tab-${index}`;
99104
label.setAttribute('for', tabId);
100-
input.setAttribute(
101-
'aria-controls',
102-
`${state._name}-tab-panel-${index}`
103-
);
104105
input.id = tabId;
105106
input.setAttribute('name', state._name);
107+
if (tabPanels.length > index) {
108+
input.setAttribute(
109+
'aria-controls',
110+
`${state._name}-tab-panel-${index}`
111+
);
112+
}
106113
}
107114

108115
if (init) {
@@ -121,11 +128,6 @@ export default function DBTabs(props: DBTabsProps) {
121128
}
122129
}
123130

124-
const tabPanels = Array.from<Element>(
125-
_ref.querySelectorAll(
126-
':is(:scope > .db-tab-panel, :scope > db-tab-panel > .db-tab-panel)'
127-
)
128-
);
129131
for (const panel of tabPanels) {
130132
if (panel.id) continue;
131133
const index: number = tabPanels.indexOf(panel);

0 commit comments

Comments
 (0)