Skip to content

Commit 4f47ff9

Browse files
DataGrid - Fix the issue with the focus method not setting the focus to the grid (T1308919) (#33088)
1 parent ab5b637 commit 4f47ff9

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

e2e/testcafe-devextreme/tests/dataGrid/common/keyboardNavigation/keyboardNavigation.functional.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Selector, ClientFunction } from 'testcafe';
22
import { createScreenshotsComparer } from 'devextreme-screenshot-comparer';
3+
import Button from 'devextreme-testcafe-models/button';
34
import DataGrid from 'devextreme-testcafe-models/dataGrid';
45
import CommandCell from 'devextreme-testcafe-models/dataGrid/commandCell';
56
import { ClassNames } from 'devextreme-testcafe-models/dataGrid/classNames';
@@ -6576,3 +6577,66 @@ test('The last cell should be focused after changing the page size (T1063530)',
65766577
})();
65776578
});
65786579
});
6580+
6581+
test('Focus should be set to the grid to allow keyboard navigation when the focus method is called (T1308919)', async (t) => {
6582+
// arrange
6583+
const button = new Button('#otherContainer');
6584+
const dataGrid = new DataGrid('#container');
6585+
const firstDataCell = dataGrid.getDataCell(0, 0);
6586+
const firstRow = dataGrid.getDataRow(0);
6587+
const secondRow = dataGrid.getDataRow(1);
6588+
6589+
// assert
6590+
await t.expect(dataGrid.isReady()).ok();
6591+
6592+
// act
6593+
await dataGrid.apiFocus(firstDataCell.element);
6594+
6595+
// assert
6596+
await t
6597+
.expect(firstDataCell.isFocused).ok()
6598+
.expect(firstRow.isFocusedRow).ok();
6599+
6600+
// act
6601+
await button.focus();
6602+
6603+
// assert
6604+
await t
6605+
.expect(button.isFocused)
6606+
.ok()
6607+
.expect(firstDataCell.isFocused)
6608+
.notOk('focus should be on the button')
6609+
.expect(firstRow.isFocusedRow)
6610+
.ok('row should still have the focused-row style');
6611+
6612+
// act
6613+
await t.pressKey('down');
6614+
6615+
// assert
6616+
await t
6617+
.expect(secondRow.isFocusedRow)
6618+
.notOk('grid kbn should not work');
6619+
6620+
// act
6621+
await t
6622+
.pressKey('enter') // trigger button click
6623+
.pressKey('down');
6624+
6625+
// assert
6626+
await t
6627+
.expect(secondRow.isFocusedRow)
6628+
.ok('grid is focused, so kbn should work');
6629+
}).before(async () => {
6630+
await createWidget('dxDataGrid', {
6631+
dataSource: [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }],
6632+
keyExpr: 'id',
6633+
focusedRowEnabled: true,
6634+
});
6635+
await createWidget('dxButton', {
6636+
text: 'Focus Grid',
6637+
onClick() {
6638+
const grid = ($ as any)('#container').dxDataGrid('instance');
6639+
grid.focus();
6640+
},
6641+
}, '#otherContainer');
6642+
});

packages/devextreme/js/__internal/grids/grid_core/keyboard_navigation/m_keyboard_navigation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,12 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
16741674
$focusElement
16751675
.removeClass(CELL_FOCUS_DISABLED_CLASS)
16761676
.removeClass(FOCUSED_CLASS);
1677+
1678+
const isTargetInGrid = gridCoreUtils.isElementInCurrentGrid(this, $(e.relatedTarget));
1679+
if (!isTargetInGrid) {
1680+
this._resetFocusedCell(true);
1681+
this._resetFocusedView();
1682+
}
16771683
}
16781684
});
16791685
if (!skipFocusEvent) {

packages/testcafe-models/dataGrid/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -742,16 +742,12 @@ export default class DataGrid extends GridCore {
742742
)();
743743
}
744744

745-
apiFocus(): Promise<void> {
745+
apiFocus(cellElement?: Selector): Promise<void> {
746746
const { getInstance } = this;
747747

748748
return ClientFunction(
749-
() => (getInstance() as any).focus(),
750-
{
751-
dependencies: {
752-
getInstance,
753-
},
754-
},
749+
() => (getInstance() as any).focus(cellElement?.()),
750+
{ dependencies: { getInstance, cellElement } },
755751
)();
756752
}
757753

0 commit comments

Comments
 (0)