Skip to content

Commit dd0687c

Browse files
authored
TabPanel: Added focus to the selected tab after initialization (T1277409) (DevExpress#29989)
1 parent 4ac1254 commit dd0687c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ class TabPanel extends MultiView<TabPanelProperties> {
259259
.addClass(TABPANEL_CONTAINER_CLASS)
260260
.appendTo($element);
261261
this._$container.append(this._$wrapper);
262+
263+
const { focusStateEnabled, selectedIndex } = this.option();
264+
if (focusStateEnabled && isDefined(selectedIndex)) {
265+
const selectedItem = this._tabs.itemElements().get(selectedIndex);
266+
267+
if (selectedItem) {
268+
this._tabs.option({ focusedElement: selectedItem });
269+
}
270+
}
262271
}
263272

264273
_refreshActiveDescendant(): void {

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

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

10361036
assert.strictEqual($tabPanel.find(`.${MULTIVIEW_WRAPPER_CLASS}`).hasClass(FOCUS_STATE_CLASS), false);
10371037
});
1038+
1039+
QUnit.test('first tab should be focused after initialization (T1277409)', function(assert) {
1040+
const items = Array.from({ length: 20 }, (_, index) => ({
1041+
title: `Tab ${index + 1}`,
1042+
text: `Content ${index + 1}`
1043+
}));
1044+
1045+
const $tabPanel = $('#tabPanel').dxTabPanel({
1046+
items,
1047+
focusStateEnabled: true,
1048+
});
1049+
1050+
const $tabs = $tabPanel.find(toSelector(TABPANEL_TABS_ITEM_CLASS));
1051+
const tabsInstance = $tabPanel.find(toSelector(TABS_CLASS)).dxTabs('instance');
1052+
const $firstTab = $tabs.first();
1053+
1054+
assert.strictEqual($tabs.filter(`.${FOCUS_STATE_CLASS}`).length, 1, 'only one tab is focused');
1055+
assert.strictEqual($firstTab.hasClass(FOCUS_STATE_CLASS), true, 'first tab is focused');
1056+
assert.strictEqual($(tabsInstance.option('focusedElement')).get(0), $firstTab.get(0), 'focusedElement is correct');
1057+
});
10381058
});
10391059

10401060
QUnit.module('keyboard navigation', {

0 commit comments

Comments
 (0)