From 4f63c66c6ab3a764d5219cef30c43d9f72d127f0 Mon Sep 17 00:00:00 2001 From: Andrey Dolzhikov Date: Tue, 21 Jan 2025 09:36:22 +0200 Subject: [PATCH] T1261532: DataGrid - FocusedRowChanged event isn't raised when the push API is used to remove the last row (#28687) --- .../tests/dataGrid/focus/focus.ts | 31 +++++++++++++++++++ .../grids/grid_core/focus/m_focus.ts | 22 ++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts b/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts index b3e056b76ab7..e24e00669d08 100644 --- a/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts +++ b/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts @@ -241,6 +241,37 @@ test('DataGrid - FilterRow cell loses focus when focusedRowEnabled is true and e }); }); +test('DataGrid - FocusedRowChanged event isnt raised when the push API is used to remove the last row (T1261532)', async (t) => { + const grid = new DataGrid(GRID_SELECTOR); + + await t + .expect(grid.option('focusedRowKey')) + .eql(null) + .expect(grid.option('focusedRowIndex')) + .eql(-1); +}).before(async () => createWidget('dxDataGrid', { + dataSource: { + store: { + data: [ + { + id: 1, + name: 'Item 1 ', + }, + ], + type: 'array', + key: 'id', + }, + reshapeOnPush: true, + }, + keyExpr: 'id', + showBorders: true, + focusedRowEnabled: true, + focusedRowKey: 1, + onInitialized(e) { + e.component?.getDataSource().store().push([{ type: 'remove', key: 1 }]); + }, +})); + ['onFocusedRowChanged', 'onFocusedRowChanging'].forEach((event) => { test(`Focus should be preserved on datagrid when rowsview repaints in ${event} event (T1224663)`, async (t) => { const dataGrid = new DataGrid('#container'); diff --git a/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.ts b/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.ts index 973c154cef60..504bc1707749 100644 --- a/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.ts +++ b/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.ts @@ -82,16 +82,28 @@ export class FocusController extends core.ViewController { if (!this.option('focusedRowEnabled')) { return; } + const isEmptyData = this.getDataController().isEmpty(); + const currentIndex = this._getCurrentFocusRowIndex(isEmptyData, index); - index = index !== undefined ? index : this.option('focusedRowIndex'); - - if (index < 0) { - if (this.isAutoNavigateToFocusedRow()) { + if (currentIndex < 0) { + if (isEmptyData || this.isAutoNavigateToFocusedRow()) { this._resetFocusedRow(); } } else { - this._focusRowByIndexCore(index, operationTypes); + this._focusRowByIndexCore(currentIndex, operationTypes); + } + } + + private _getCurrentFocusRowIndex(isEmptyData, index?): number { + let currentIndex = index; + if (currentIndex === undefined) { + if (isEmptyData) { + currentIndex = -1; + } else { + currentIndex = this.option('focusedRowIndex'); + } } + return currentIndex; } private _focusRowByIndexCore(index, operationTypes) {