Skip to content

Commit b5054a9

Browse files
List: selected item should receive focus when the list is grouped (T1278005) (DevExpress#29354)
1 parent 234951d commit b5054a9

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

packages/devextreme/js/__internal/ui/collection/collection_widget.base.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ class CollectionWidget<
423423
return $focusedElement;
424424
}
425425

426-
const { focusOnSelectedItem, selectedIndex } = this.option();
426+
const { focusOnSelectedItem } = this.option();
427427

428-
let index = focusOnSelectedItem ? selectedIndex : 0;
428+
let index = focusOnSelectedItem ? this._getFlatIndex() : 0;
429429

430430
const activeElements = this._getActiveElement();
431431
const lastIndex = activeElements.length - 1;
@@ -438,6 +438,12 @@ class CollectionWidget<
438438
return activeElements.eq(index);
439439
}
440440

441+
_getFlatIndex(): number | undefined {
442+
const { selectedIndex } = this.option();
443+
444+
return selectedIndex;
445+
}
446+
441447
// eslint-disable-next-line consistent-return
442448
_moveFocus(
443449
location: string,

packages/devextreme/js/__internal/ui/list/m_list.edit.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { dxElementWrapper } from '@js/core/renderer';
44
import $ from '@js/core/renderer';
55
import { extend } from '@js/core/utils/extend';
66
import type { Item } from '@js/ui/list';
7+
import { isNumeric } from '@ts/core/utils/m_type';
78
import type { OptionChanged } from '@ts/core/widget/types';
89

910
import type { ListBaseProperties } from './m_list.base';
@@ -317,6 +318,19 @@ class ListEdit extends ListBase {
317318
this.scrollToItem(this.option('focusedElement'));
318319
}
319320

321+
_getFlatIndex(): number | undefined {
322+
const { selectedIndex } = this.option();
323+
324+
if (isNumeric(selectedIndex) || !selectedIndex) {
325+
return selectedIndex;
326+
}
327+
328+
const $item = this._editStrategy.getItemElement(selectedIndex);
329+
const index = this.getFlatIndexByItemElement($item);
330+
331+
return index;
332+
}
333+
320334
_optionChanged(args: OptionChanged<ListBaseProperties>): void {
321335
switch (args.name) {
322336
case 'selectAllMode':

packages/devextreme/testing/tests/DevExpress.ui.widgets/listParts/commonTests.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,6 +4315,46 @@ QUnit.module('keyboard navigation', {
43154315
assert.ok(secondItemIsFocused, 'focused item change to last visible item on new page');
43164316
});
43174317

4318+
QUnit.test('focus should be moved to selectedItem after focusing of grouped list (T1278005)', function(assert) {
4319+
const list = $('#list').dxList({
4320+
selectedItemKeys: [5],
4321+
keyExpr: 'id',
4322+
items: [{
4323+
key: 'a',
4324+
items: [
4325+
{ id: 1, text: '1' },
4326+
{ id: 2, text: '2' },
4327+
{ id: 3, text: '3' },
4328+
] }, {
4329+
key: 'b',
4330+
items: [
4331+
{ id: 4, text: '4' },
4332+
{ id: 5, text: '5' },
4333+
{ id: 6, text: '6' },
4334+
] },
4335+
],
4336+
grouped: true,
4337+
focusStateEnabled: true,
4338+
collapsibleGroups: false,
4339+
selectionMode: 'multiple',
4340+
showSelectionControls: false,
4341+
}).dxList('instance');
4342+
4343+
const $element = $(list.element());
4344+
4345+
const keyboard = getListKeyboard($element);
4346+
list.focus();
4347+
4348+
const $items = $element.find(`.${LIST_ITEM_CLASS}`);
4349+
4350+
assert.strictEqual($items.eq(4).hasClass(FOCUSED_STATE_CLASS), true, 'focused class is added to selected item');
4351+
4352+
keyboard.keyDown('down');
4353+
4354+
assert.strictEqual($items.eq(4).hasClass(FOCUSED_STATE_CLASS), false, 'focused class is removed');
4355+
assert.strictEqual($items.eq(5).hasClass(FOCUSED_STATE_CLASS), true, 'next item is focused');
4356+
});
4357+
43184358
QUnit.test('list should attach keyboard events even if focusStateEnabled is false when the widget\'s onKeyboardHandled is defined', function(assert) {
43194359
const handler = sinon.stub();
43204360
const $element = $('#list');

0 commit comments

Comments
 (0)