Skip to content

Commit b97ced1

Browse files
Collection: only the CollectionWidget.Edit should contain logic related to selection functionality (#29577)
1 parent 139d7fb commit b97ced1

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

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

Lines changed: 13 additions & 21 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>;
@@ -235,7 +232,6 @@ class CollectionWidget<
235232

236233
_itemAttributes: {},
237234
itemTemplateProperty: 'template',
238-
focusOnSelectedItem: true,
239235
focusedElement: null,
240236

241237
displayExpr: undefined,
@@ -423,25 +419,24 @@ class CollectionWidget<
423419
return $focusedElement;
424420
}
425421

426-
const { focusOnSelectedItem } = this.option();
422+
return this._determineFocusedElement(last);
423+
}
427424

428-
let index = focusOnSelectedItem ? this._getFlatIndex() : 0;
425+
_determineFocusedElement(last?: boolean): dxElementWrapper {
426+
let index = this._getFocusedElementIndex();
429427

430428
const activeElements = this._getActiveElement();
431429
const lastIndex = activeElements.length - 1;
432430

433-
// @ts-expect-error ts-error
434431
if (index < 0) {
435432
index = last ? lastIndex : 0;
436433
}
437-
// @ts-expect-error ts-error
434+
438435
return activeElements.eq(index);
439436
}
440437

441-
_getFlatIndex(): number | undefined {
442-
const { selectedIndex } = this.option();
443-
444-
return selectedIndex;
438+
_getFocusedElementIndex(): number {
439+
return 0;
445440
}
446441

447442
// eslint-disable-next-line consistent-return
@@ -660,10 +655,14 @@ class CollectionWidget<
660655
_resetItemFocus($item: dxElementWrapper): void {
661656
// @ts-expect-error ts-error
662657
if ($item.is(this.option('focusedElement'))) {
663-
this.option('focusedElement', null);
658+
this._resetFocusedElement();
664659
}
665660
}
666661

662+
_resetFocusedElement(): void {
663+
this.option('focusedElement', null);
664+
}
665+
667666
_refreshItem(
668667
$item: dxElementWrapper,
669668
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -738,7 +737,6 @@ class CollectionWidget<
738737
break;
739738
case 'selectOnFocus':
740739
case 'loopItemFocus':
741-
case 'focusOnSelectedItem':
742740
break;
743741
case 'focusedElement':
744742
this._updateFocusedItemState(previousValue as Element | undefined, false, true);
@@ -759,7 +757,7 @@ class CollectionWidget<
759757
}
760758

761759
_invalidate(): void {
762-
this.option('focusedElement', null);
760+
this._resetFocusedElement();
763761

764762
super._invalidate();
765763
}
@@ -866,12 +864,6 @@ class CollectionWidget<
866864
return `${this._itemClass()}${CONTENT_CLASS_POSTFIX}`;
867865
}
868866

869-
// eslint-disable-next-line class-methods-use-this
870-
_selectedItemClass(): string {
871-
return SELECTED_ITEM_CLASS;
872-
}
873-
874-
// eslint-disable-next-line class-methods-use-this
875867
_itemResponseWaitClass(): string {
876868
return ITEM_RESPONSE_WAIT_CLASS;
877869
}

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

Lines changed: 28 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

@@ -44,6 +46,10 @@ export interface CollectionWidgetEditProperties<
4446
TKey = any,
4547
> extends CollectionWidgetBaseProperties<TComponent, TItem, TKey> {
4648
selectionMode?: SingleMultipleOrNone | SingleMultipleAllOrNone;
49+
50+
selectionRequired?: boolean;
51+
52+
focusOnSelectedItem?: boolean;
4753
}
4854

4955
class CollectionWidget<
@@ -85,6 +91,7 @@ class CollectionWidget<
8591
maxFilterLengthInRequest: 1500,
8692
keyExpr: null,
8793
selectedIndex: NOT_EXISTING_INDEX,
94+
focusOnSelectedItem: true,
8895
selectedItem: null,
8996
onSelectionChanging: null,
9097
onSelectionChanged: null,
@@ -117,6 +124,10 @@ class CollectionWidget<
117124
this._keyGetter = compileGetter(this.option('keyExpr'));
118125
}
119126

127+
_selectedItemClass(): string {
128+
return SELECTED_ITEM_CLASS;
129+
}
130+
120131
// eslint-disable-next-line class-methods-use-this
121132
_getActionsList(): ('onSelectionChanging' | 'onSelectionChanged')[] {
122133
return ['onSelectionChanging', 'onSelectionChanged'];
@@ -569,7 +580,6 @@ class CollectionWidget<
569580
}
570581

571582
this._editStrategy.endCache();
572-
573583
this._updateSelection(addedSelection, removedSelection);
574584
}
575585

@@ -590,6 +600,20 @@ class CollectionWidget<
590600
this.setAria('selected', value, $target);
591601
}
592602

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

@@ -652,6 +676,8 @@ class CollectionWidget<
652676
case 'onItemReordered':
653677
case 'maxFilterLengthInRequest':
654678
break;
679+
case 'focusOnSelectedItem':
680+
break;
655681
default:
656682
super._optionChanged(args);
657683
}

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, isObject } 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';
@@ -326,8 +327,8 @@ class ListEdit extends ListBase {
326327
this.scrollToItem(this.option('focusedElement'));
327328
}
328329

329-
_getFlatIndex(): number | undefined {
330-
const { selectedIndex } = this.option();
330+
_getFlatIndex(): number {
331+
const { selectedIndex = NOT_EXISTING_INDEX } = this.option();
331332

332333
if (isNumeric(selectedIndex) || !selectedIndex) {
333334
return selectedIndex;

0 commit comments

Comments
 (0)