Skip to content

Commit 581d7c1

Browse files
Collection: only the CollectionWidget.Edit should contain logic related to selection functionality (DevExpress#29567)
1 parent 448e076 commit 581d7c1

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const ITEM_DATA_KEY = 'dxItemData';
5050
const ITEM_INDEX_KEY = 'dxItemIndex';
5151
const ITEM_TEMPLATE_ID_PREFIX = 'tmpl-';
5252
const ITEMS_OPTIONS_NAME = 'dxItem';
53-
const SELECTED_ITEM_CLASS = 'dx-item-selected';
5453
const ITEM_RESPONSE_WAIT_CLASS = 'dx-item-response-wait';
5554
const EMPTY_COLLECTION = 'dx-empty-collection';
5655
const TEMPLATE_WRAPPER_CLASS = 'dx-template-wrapper';
@@ -105,8 +104,6 @@ export interface CollectionWidgetBaseProperties<
105104
> extends CollectionWidgetOptions<TComponent, TItem, TKey> {
106105
focusedElement?: dxElementWrapper;
107106

108-
focusOnSelectedItem?: boolean;
109-
110107
encodeNoDataText?: boolean;
111108

112109
_itemAttributes?: Record<string, unknown>;
@@ -236,7 +233,6 @@ class CollectionWidget<
236233

237234
_itemAttributes: {},
238235
itemTemplateProperty: 'template',
239-
focusOnSelectedItem: true,
240236
focusedElement: null,
241237

242238
displayExpr: undefined,
@@ -420,25 +416,24 @@ class CollectionWidget<
420416
return $focusedElement;
421417
}
422418

423-
const { focusOnSelectedItem } = this.option();
419+
return this._determineFocusedElement(last);
420+
}
424421

425-
let index = focusOnSelectedItem ? this._getFlatIndex() : 0;
422+
_determineFocusedElement(last?: boolean): dxElementWrapper {
423+
let index = this._getFocusedElementIndex();
426424

427425
const activeElements = this._getActiveElement();
428426
const lastIndex = activeElements.length - 1;
429427

430-
// @ts-expect-error ts-error
431428
if (index < 0) {
432429
index = last ? lastIndex : 0;
433430
}
434-
// @ts-expect-error ts-error
431+
435432
return activeElements.eq(index);
436433
}
437434

438-
_getFlatIndex(): number | undefined {
439-
const { selectedIndex } = this.option();
440-
441-
return selectedIndex;
435+
_getFocusedElementIndex(): number {
436+
return 0;
442437
}
443438

444439
// eslint-disable-next-line consistent-return
@@ -655,10 +650,14 @@ class CollectionWidget<
655650
_resetItemFocus($item: dxElementWrapper): void {
656651
// @ts-expect-error ts-error
657652
if ($item.is(this.option('focusedElement'))) {
658-
this.option('focusedElement', null);
653+
this._resetFocusedElement();
659654
}
660655
}
661656

657+
_resetFocusedElement(): void {
658+
this.option('focusedElement', null);
659+
}
660+
662661
_refreshItem(
663662
$item: dxElementWrapper,
664663
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -732,7 +731,6 @@ class CollectionWidget<
732731
break;
733732
case 'selectOnFocus':
734733
case 'loopItemFocus':
735-
case 'focusOnSelectedItem':
736734
break;
737735
case 'focusedElement':
738736
this._updateFocusedItemState(previousValue as Element | undefined, false, true);
@@ -753,7 +751,7 @@ class CollectionWidget<
753751
}
754752

755753
_invalidate(): void {
756-
this.option('focusedElement', null);
754+
this._resetFocusedElement();
757755

758756
super._invalidate();
759757
}
@@ -858,10 +856,6 @@ class CollectionWidget<
858856
return `${this._itemClass()}${CONTENT_CLASS_POSTFIX}`;
859857
}
860858

861-
_selectedItemClass(): string {
862-
return SELECTED_ITEM_CLASS;
863-
}
864-
865859
_itemResponseWaitClass(): string {
866860
return ITEM_RESPONSE_WAIT_CLASS;
867861
}

packages/devextreme/js/__internal/ui/collection/m_collection_widget.edit.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import type MenuBaseEditStrategy from '../context_menu/m_menu_base.edit.strategy
3131
import type GroupedEditStrategy from '../list/m_list.edit.strategy.grouped';
3232

3333
const ITEM_DELETING_DATA_KEY = 'dxItemDeleting';
34-
const NOT_EXISTING_INDEX = -1;
34+
const SELECTED_ITEM_CLASS = 'dx-item-selected';
35+
36+
export const NOT_EXISTING_INDEX = -1;
3537

3638
const indexExists = (index): boolean => index !== NOT_EXISTING_INDEX;
3739

@@ -46,6 +48,8 @@ export interface CollectionWidgetEditProperties<
4648
selectionMode?: SingleMultipleOrNone | SingleMultipleAllOrNone;
4749

4850
selectionRequired?: boolean;
51+
52+
focusOnSelectedItem?: boolean;
4953
}
5054

5155
class CollectionWidget<
@@ -87,6 +91,7 @@ class CollectionWidget<
8791
maxFilterLengthInRequest: 1500,
8892
keyExpr: null,
8993
selectedIndex: NOT_EXISTING_INDEX,
94+
focusOnSelectedItem: true,
9095
selectedItem: null,
9196
onSelectionChanging: null,
9297
onSelectionChanged: null,
@@ -119,6 +124,10 @@ class CollectionWidget<
119124
this._keyGetter = compileGetter(this.option('keyExpr'));
120125
}
121126

127+
_selectedItemClass(): string {
128+
return SELECTED_ITEM_CLASS;
129+
}
130+
122131
// eslint-disable-next-line class-methods-use-this
123132
_getActionsList(): ('onSelectionChanging' | 'onSelectionChanged')[] {
124133
return ['onSelectionChanging', 'onSelectionChanged'];
@@ -570,7 +579,6 @@ class CollectionWidget<
570579
}
571580

572581
this._editStrategy.endCache();
573-
574582
this._updateSelection(addedSelection, removedSelection);
575583
}
576584

@@ -591,6 +599,20 @@ class CollectionWidget<
591599
this.setAria('selected', value, $target);
592600
}
593601

602+
_getFocusedElementIndex(): number {
603+
const { focusOnSelectedItem } = this.option();
604+
605+
return focusOnSelectedItem
606+
? this._getFlatIndex()
607+
: super._getFocusedElementIndex();
608+
}
609+
610+
_getFlatIndex(): number {
611+
const { selectedIndex = NOT_EXISTING_INDEX } = this.option();
612+
613+
return selectedIndex;
614+
}
615+
594616
_removeSelection(normalizedIndex: number): void {
595617
const $itemElement = this._editStrategy.getItemElement(normalizedIndex);
596618

@@ -653,6 +675,8 @@ class CollectionWidget<
653675
case 'onItemReordered':
654676
case 'maxFilterLengthInRequest':
655677
break;
678+
case 'focusOnSelectedItem':
679+
break;
656680
default:
657681
super._optionChanged(args);
658682
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { extend } from '@js/core/utils/extend';
66
import type { Item } from '@js/ui/list';
77
import { isNumeric } from '@ts/core/utils/m_type';
88
import type { OptionChanged } from '@ts/core/widget/types';
9+
import { NOT_EXISTING_INDEX } from '@ts/ui/collection/m_collection_widget.edit';
910

1011
import type { ListBaseProperties } from './m_list.base';
1112
import { ListBase } from './m_list.base';
@@ -322,8 +323,8 @@ class ListEdit extends ListBase {
322323
this.scrollToItem(this.option('focusedElement'));
323324
}
324325

325-
_getFlatIndex(): number | undefined {
326-
const { selectedIndex } = this.option();
326+
_getFlatIndex(): number {
327+
const { selectedIndex = NOT_EXISTING_INDEX } = this.option();
327328

328329
if (isNumeric(selectedIndex) || !selectedIndex) {
329330
return selectedIndex;

0 commit comments

Comments
 (0)