Skip to content

Commit 2d279f3

Browse files
authored
T1261532: DataGrid - FocusedRowChanged event isn't raised when the push API is used to remove the last row (#28790)
1 parent 0d43098 commit 2d279f3

File tree

2 files changed

+48
-5
lines changed
  • e2e/testcafe-devextreme/tests/dataGrid/focus
  • packages/devextreme/js/__internal/grids/grid_core/focus

2 files changed

+48
-5
lines changed

e2e/testcafe-devextreme/tests/dataGrid/focus/focus.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ test('DataGrid - FilterRow cell loses focus when focusedRowEnabled is true and e
241241
});
242242
});
243243

244+
test('DataGrid - FocusedRowChanged event isnt raised when the push API is used to remove the last row (T1261532)', async (t) => {
245+
const grid = new DataGrid(GRID_SELECTOR);
246+
247+
await t
248+
.expect(grid.option('focusedRowKey'))
249+
.eql(null)
250+
.expect(grid.option('focusedRowIndex'))
251+
.eql(-1);
252+
}).before(async () => createWidget('dxDataGrid', {
253+
dataSource: {
254+
store: {
255+
data: [
256+
{
257+
id: 1,
258+
name: 'Item 1 ',
259+
},
260+
],
261+
type: 'array',
262+
key: 'id',
263+
},
264+
reshapeOnPush: true,
265+
},
266+
keyExpr: 'id',
267+
showBorders: true,
268+
focusedRowEnabled: true,
269+
focusedRowKey: 1,
270+
onInitialized(e) {
271+
e.component?.getDataSource().store().push([{ type: 'remove', key: 1 }]);
272+
},
273+
}));
274+
244275
['onFocusedRowChanged', 'onFocusedRowChanging'].forEach((event) => {
245276
test(`Focus should be preserved on datagrid when rowsview repaints in ${event} event (T1224663)`, async (t) => {
246277
const dataGrid = new DataGrid('#container');

packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,28 @@ export class FocusController extends core.ViewController {
8282
if (!this.option('focusedRowEnabled')) {
8383
return;
8484
}
85+
const isEmptyData = this.getDataController().isEmpty();
86+
const currentIndex = this._getCurrentFocusRowIndex(isEmptyData, index);
8587

86-
index = index !== undefined ? index : this.option('focusedRowIndex');
87-
88-
if (index < 0) {
89-
if (this.isAutoNavigateToFocusedRow()) {
88+
if (currentIndex < 0) {
89+
if (isEmptyData || this.isAutoNavigateToFocusedRow()) {
9090
this._resetFocusedRow();
9191
}
9292
} else {
93-
this._focusRowByIndexCore(index, operationTypes);
93+
this._focusRowByIndexCore(currentIndex, operationTypes);
94+
}
95+
}
96+
97+
private _getCurrentFocusRowIndex(isEmptyData, index?): number {
98+
let currentIndex = index;
99+
if (currentIndex === undefined) {
100+
if (isEmptyData) {
101+
currentIndex = -1;
102+
} else {
103+
currentIndex = this.option('focusedRowIndex');
104+
}
94105
}
106+
return currentIndex;
95107
}
96108

97109
private _focusRowByIndexCore(index, operationTypes) {

0 commit comments

Comments
 (0)