diff --git a/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts b/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts index b1c9e7a0697f..b1aa3fc26349 100644 --- a/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts +++ b/e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts @@ -242,6 +242,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 7b1da377da19..e001b4dfbd6f 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) {