Skip to content

Commit 47805ab

Browse files
authored
TabPanel: Added focus to the selected tab after initialization (T1277409) (#29998)
Cherry-pick of #29989
1 parent e3db766 commit 47805ab

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/devextreme/js/__internal/ui/tab_panel/m_tab_panel.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ const TabPanel = MultiView.inherit({
225225
.addClass(TABPANEL_CONTAINER_CLASS)
226226
.appendTo($element);
227227
this._$container.append(this._$wrapper);
228+
229+
const { focusStateEnabled, selectedIndex } = this.option();
230+
if (focusStateEnabled && isDefined(selectedIndex)) {
231+
const selectedItem = this._tabs.itemElements().get(selectedIndex);
232+
233+
if (selectedItem) {
234+
this._tabs.option({ focusedElement: selectedItem });
235+
}
236+
}
228237
},
229238

230239
_refreshActiveDescendant() {

packages/devextreme/testing/tests/DevExpress.ui.widgets/tabPanel.tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,26 @@ QUnit.module('focus policy', {
604604

605605
assert.strictEqual($tabPanel.find(`.${MULTIVIEW_WRAPPER_CLASS}`).hasClass(FOCUS_STATE_CLASS), false);
606606
});
607+
608+
QUnit.test('first tab should be focused after initialization (T1277409)', function(assert) {
609+
const items = Array.from({ length: 20 }, (_, index) => ({
610+
title: `Tab ${index + 1}`,
611+
text: `Content ${index + 1}`
612+
}));
613+
614+
const $tabPanel = $('#tabPanel').dxTabPanel({
615+
items,
616+
focusStateEnabled: true,
617+
});
618+
619+
const $tabs = $tabPanel.find(toSelector(TABPANEL_TABS_ITEM_CLASS));
620+
const tabsInstance = $tabPanel.find(toSelector(TABS_CLASS)).dxTabs('instance');
621+
const $firstTab = $tabs.first();
622+
623+
assert.strictEqual($tabs.filter(`.${FOCUS_STATE_CLASS}`).length, 1, 'only one tab is focused');
624+
assert.strictEqual($firstTab.hasClass(FOCUS_STATE_CLASS), true, 'first tab is focused');
625+
assert.strictEqual($(tabsInstance.option('focusedElement')).get(0), $firstTab.get(0), 'focusedElement is correct');
626+
});
607627
});
608628

609629
QUnit.module('keyboard navigation', {

0 commit comments

Comments
 (0)