Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,134 @@ describe('GridCore focus', () => {
});
});

describe('when another row is updated via the store push API with repaintChangesOnly', () => {
it('should keep DOM focus on the focused cell (T1338023)', async () => {
const { instance } = await createDataGrid({
dataSource: {
store: {
type: 'array',
key: 'id',
data: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
},
pushAggregationTimeout: 0,
},
focusedRowEnabled: true,
focusedRowKey: 1,
repaintChangesOnly: true,
columns: ['id', 'name'],
});

const cell = instance.getCellElement(0, 'name') as HTMLElement;
instance.focus(cell);
await flushAsync();
cell.dispatchEvent(new KeyboardEvent('keydown', { key: 'Shift', bubbles: true }));

expect(document.activeElement).toBe(cell);

instance.getDataSource().store().push([{ type: 'update', key: 2, data: { name: 'Item 2 updated' } }]);
await flushAsync();

expect($(instance.getCellElement(1, 'name')).text()).toBe('Item 2 updated');
expect(cell.hasAttribute('tabindex')).toBe(true);
expect($(instance.getRowElement(0)).get(0).hasAttribute('tabindex')).toBe(false);
expect(document.activeElement).toBe(cell);
});
});

describe('when a focused group row is updated via the store push API with repaintChangesOnly', () => {
it('should keep DOM focus on the group row in cell focus mode (T1338023)', async () => {
const { instance } = await createDataGrid({
dataSource: {
store: {
type: 'array',
key: 'id',
data: [
{ id: 1, group: 'A', value: 1 },
{ id: 2, group: 'A', value: 2 },
{ id: 3, group: 'B', value: 3 },
],
},
reshapeOnPush: true,
pushAggregationTimeout: 0,
},
columns: ['id', { dataField: 'group', groupIndex: 0 }, 'value'],
summary: { groupItems: [{ column: 'value', summaryType: 'sum' }] },
focusedRowEnabled: true,
repaintChangesOnly: true,
});

const cell = instance.getCellElement(1, 'id') as HTMLElement;
instance.focus(cell);
await flushAsync();
cell.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowUp', bubbles: true }));
await flushAsync();
const groupRow = $(instance.getRowElement(0)).get(0);
groupRow.dispatchEvent(new KeyboardEvent('keydown', { key: 'Shift', bubbles: true }));

expect(instance.option('focusedRowKey')).toEqual(['A']);
expect(document.activeElement).toBe(groupRow);
expect(groupRow.classList.contains('dx-focused')).toBe(true);

const removeAttribute = jest.spyOn(groupRow, 'removeAttribute');
instance.getDataSource().store().push([{ type: 'update', key: 1, data: { value: 100 } }]);
await flushAsync();

expect($(instance.getRowElement(0)).text()).toContain('102');
expect(removeAttribute).not.toHaveBeenCalledWith('tabindex');
expect(groupRow.classList.contains('dx-focused')).toBe(true);
expect(document.activeElement).toBe(groupRow);
});
});

describe('when focusedRowKey is changed with a custom tabIndex', () => {
it('should apply the tabIndex option to the new focused row (T1338023)', async () => {
const { instance } = await createDataGrid({
dataSource: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
keyExpr: 'id',
focusedRowEnabled: true,
focusedRowKey: 1,
tabIndex: 3,
columns: ['id', 'name'],
});

instance.option('focusedRowKey', 2);
await flushAsync();

expect($(instance.getRowElement(1)).get(0).getAttribute('tabindex')).toBe('3');
});
});

describe('when the focused row has an expanded master detail', () => {
it('should not make the detail row a tab stop (T1338023)', async () => {
const { instance } = await createDataGrid({
dataSource: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
keyExpr: 'id',
focusedRowEnabled: true,
focusedRowKey: 1,
columns: ['id', 'name'],
masterDetail: {
enabled: true,
template: (container) => { $(container).text('detail'); },
},
});

await instance.expandRow(1);
await flushAsync();

expect(instance.getVisibleRows()[1].rowType).toBe('detail');
expect($(instance.getRowElement(1)).get(0).hasAttribute('tabindex')).toBe(false);
});
});

describe('when the focused row is reset while the navigation to a virtual row is pending', () => {
it('should not focus the row again when the row is rendered', async () => {
const rows = Array.from({ length: 100 }, (_, i) => ({ id: i, value: `row ${i}` }));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-classes-per-file */
import type { dxElementWrapper } from '@js/core/renderer';
import $ from '@js/core/renderer';
import { equalByValue } from '@js/core/utils/common';
import { Deferred, type DeferredObj, when } from '@js/core/utils/deferred';
Expand Down Expand Up @@ -402,16 +403,28 @@ export class FocusController extends core.ViewController {
}
}

private _clearPreviousFocusedRow($tableElement, focusedRowIndex) {
private findStaleFocusedRows($tableElement, focusedRowIndex: number): dxElementWrapper {
const isNotMasterDetailFocusedRow = (_, focusedRow) => {
const $focusedRowTable = $(focusedRow).closest(`.${this.addWidgetPrefix(TABLE_POSTFIX_CLASS)}`);
return $tableElement.is($focusedRowTable);
};

const $prevRowFocusedElement = $tableElement
const $focusedRows = $tableElement
.find(FOCUSED_ROW_SELECTOR)
.filter(isNotMasterDetailFocusedRow);

if (focusedRowIndex < 0) {
return $focusedRows;
}

const $focusedRow = this.getView('rowsView')._getRowElements($tableElement).eq(focusedRowIndex);

return $focusedRows.not($focusedRow);
}

private _clearPreviousFocusedRow($tableElement, focusedRowIndex) {
const $prevRowFocusedElement = this.findStaleFocusedRows($tableElement, focusedRowIndex);

$prevRowFocusedElement
.removeClass(ROW_FOCUSED_CLASS)
.removeClass(CELL_FOCUS_DISABLED_CLASS)
Expand All @@ -430,11 +443,15 @@ export class FocusController extends core.ViewController {
if (changedItem && (changedItem.rowType === 'data' || changedItem.rowType === 'group')) {
const { focusedRowIndex } = options;
const { $tableElement } = options;
const tabIndex = this.option('tabindex') || 0;
const tabIndex = this.option('tabIndex') ?? 0;
const rowsView = this.getView('rowsView');

$row = $(rowsView._getRowElements($tableElement).eq(focusedRowIndex));
$row.addClass(ROW_FOCUSED_CLASS).attr('tabindex', tabIndex);
$row.addClass(ROW_FOCUSED_CLASS);

if (!$row.children('[tabindex]').length) {
$row.attr('tabindex', tabIndex);
}
}

return $row;
Expand Down Expand Up @@ -918,6 +935,13 @@ const rowsView = (Base: ModuleType<RowsView>) => class RowsViewFocusController e
if (this.option('focusedRowEnabled') && row) {
if (this._focusController.isRowFocused(row.key)) {
$row.addClass(ROW_FOCUSED_CLASS);

const isRowTabStop = row.rowType === 'group'
|| (row.rowType === 'data' && !this._keyboardNavigationController.isCellFocusType());

if (isRowTabStop) {
$row.attr('tabindex', this.option('tabIndex') ?? 0);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,17 @@ const rowsView = (Base: ModuleType<RowsView>) => class RowsViewKeyboardExtender
}
}

protected _updateCells($rowElement, $newRowElement, columnIndices, options?): void {
const focusStateClasses = [FOCUSED_CLASS, CELL_FOCUS_DISABLED_CLASS]
.filter((className) => $rowElement.hasClass(className));

super._updateCells($rowElement, $newRowElement, columnIndices, options);

focusStateClasses.forEach((className) => {
$rowElement.addClass(className);
});
}

public renderFocusState(params) {
super.renderFocusState(params);

Expand Down
Loading
Loading