diff --git a/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.integration.test.ts b/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.integration.test.ts index 4655cbd7ede3..eeda6f7295c8 100644 --- a/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.integration.test.ts +++ b/packages/devextreme/js/__internal/grids/grid_core/focus/m_focus.integration.test.ts @@ -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}` })); 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 42b7c778e3e6..d5c80b5693bc 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 @@ -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'; @@ -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) @@ -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; @@ -918,6 +935,13 @@ const rowsView = (Base: ModuleType) => 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); + } } } diff --git a/packages/devextreme/js/__internal/grids/grid_core/keyboard_navigation/m_keyboard_navigation.ts b/packages/devextreme/js/__internal/grids/grid_core/keyboard_navigation/m_keyboard_navigation.ts index 02727c53cb08..c1c9d15ea497 100644 --- a/packages/devextreme/js/__internal/grids/grid_core/keyboard_navigation/m_keyboard_navigation.ts +++ b/packages/devextreme/js/__internal/grids/grid_core/keyboard_navigation/m_keyboard_navigation.ts @@ -3008,6 +3008,17 @@ const rowsView = (Base: ModuleType) => 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); diff --git a/packages/devextreme/js/__internal/grids/tree_list/__tests__/focus_navigation.integration.test.ts b/packages/devextreme/js/__internal/grids/tree_list/__tests__/focus_navigation.integration.test.ts new file mode 100644 index 000000000000..e13e2c2cf706 --- /dev/null +++ b/packages/devextreme/js/__internal/grids/tree_list/__tests__/focus_navigation.integration.test.ts @@ -0,0 +1,233 @@ +import { + afterEach, beforeEach, describe, expect, it, jest, +} from '@jest/globals'; +import $ from '@js/core/renderer'; +import type { Properties as TreeListProperties } from '@js/ui/tree_list'; +import TreeList from '@js/ui/tree_list'; +import eventsEngine from '@ts/events/core/m_events_engine'; + +const TREELIST_CONTAINER_ID = 'treeListContainer'; + +const DATA = [ + { id: 1, parentId: 0, name: 'root A' }, + { id: 11, parentId: 1, name: 'child A1' }, + { id: 111, parentId: 11, name: 'leaf A1a' }, + { id: 2, parentId: 0, name: 'root B' }, + { id: 21, parentId: 2, name: 'child B1' }, +]; + +const flushAsync = async (): Promise => { + jest.runAllTimers(); + await Promise.resolve(); +}; + +const createTreeList = (options: TreeListProperties = {}): { instance: TreeList } => { + const $container = $('
') + .attr('id', TREELIST_CONTAINER_ID) + .appendTo(document.body); + + const instance = new TreeList($container.get(0) as HTMLDivElement, { + keyExpr: 'id', + ...options, + }); + + jest.runAllTimers(); + + return { instance }; +}; + +const click = (element: Element): void => { + eventsEngine.trigger(element, 'dxpointerdown'); + eventsEngine.trigger(element, 'dxpointerup'); + eventsEngine.trigger(element, 'dxclick'); +}; + +const visibleKeys = (instance: TreeList): unknown[] => instance + .getVisibleRows() + .map((row) => row.key); + +describe('TreeList focus — partial update of the focused row', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + const $container = $(`#${TREELIST_CONTAINER_ID}`); + + (TreeList.getInstance($container.get(0) as HTMLElement) as TreeList)?.dispose(); + $container.remove(); + jest.clearAllMocks(); + jest.useRealTimers(); + }); + + it('keeps DOM focus on the focused cell when the focused row is expanded with repaintChangesOnly (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: DATA, + columns: ['id', 'name'], + focusedRowEnabled: true, + focusedRowKey: 1, + repaintChangesOnly: true, + autoExpandAll: false, + }); + await flushAsync(); + + 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); + + const expanding = instance.expandRow(1); + await flushAsync(); + await expanding; + + expect(visibleKeys(instance)).toEqual([1, 11, 2]); + expect(cell.hasAttribute('tabindex')).toBe(true); + expect($(instance.getRowElement(0)).get(0).hasAttribute('tabindex')).toBe(false); + expect(document.activeElement).toBe(cell); + }); + + it('keeps the focused row tab stop when the focused row is expanded with repaintChangesOnly (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: DATA, + columns: ['id', 'name'], + focusedRowEnabled: true, + focusedRowKey: 1, + repaintChangesOnly: true, + autoExpandAll: false, + tabIndex: 3, + }); + await flushAsync(); + + const row = $(instance.getRowElement(0)).get(0); + instance.focus(row); + await flushAsync(); + row.dispatchEvent(new KeyboardEvent('keydown', { key: 'Shift', bubbles: true })); + + expect(document.activeElement).toBe(row); + + const expanding = instance.expandRow(1); + await flushAsync(); + await expanding; + + expect(visibleKeys(instance)).toEqual([1, 11, 2]); + expect(row.getAttribute('tabindex')).toBe('3'); + }); + + it('never removes the tab stop from the focused row while it is expanded with repaintChangesOnly (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: DATA, + columns: ['id', 'name'], + focusedRowEnabled: true, + focusedRowKey: 1, + repaintChangesOnly: true, + autoExpandAll: false, + }); + await flushAsync(); + + const row = $(instance.getRowElement(0)).get(0); + instance.focus(row); + await flushAsync(); + const removeAttribute = jest.spyOn(row, 'removeAttribute'); + + const expanding = instance.expandRow(1); + await flushAsync(); + await expanding; + + expect(visibleKeys(instance)).toEqual([1, 11, 2]); + expect(removeAttribute).not.toHaveBeenCalledWith('tabindex'); + expect(document.activeElement).toBe(row); + }); + + it('moves the tab stop from the first row to the focused row when its hidden focused row is expanded (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: [ + { id: 1, parentId: 0, name: 'root A' }, + { id: 2, parentId: 0, name: 'root B' }, + { id: 21, parentId: 2, name: 'child B1' }, + { id: 3, parentId: 0, name: 'root C' }, + ], + columns: ['id', 'name'], + focusedRowEnabled: true, + focusedRowKey: 21, + autoNavigateToFocusedRow: false, + repaintChangesOnly: true, + autoExpandAll: false, + }); + await flushAsync(); + + instance.repaint(); + await flushAsync(); + + expect($(instance.getRowElement(0)).get(0).hasAttribute('tabindex')).toBe(true); + + const expanding = instance.expandRow(2); + await flushAsync(); + await expanding; + + expect(visibleKeys(instance)).toEqual([1, 2, 21, 3]); + expect($(instance.getRowElement(0)).get(0).hasAttribute('tabindex')).toBe(false); + expect($(instance.getRowElement(2)).get(0).hasAttribute('tabindex')).toBe(true); + }); + + it('keeps DOM focus on the focused cell when another row is updated via push (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: { + store: { + type: 'array', + key: 'id', + data: DATA.map((item) => ({ ...item })), + }, + pushAggregationTimeout: 0, + }, + columns: ['id', 'name'], + focusedRowEnabled: true, + focusedRowKey: 1, + repaintChangesOnly: true, + autoExpandAll: false, + }); + await flushAsync(); + + 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: 'root B updated' } }]); + await flushAsync(); + + expect($(instance.getCellElement(1, 'name')).text()).toBe('root B updated'); + expect(cell.hasAttribute('tabindex')).toBe(true); + expect($(instance.getRowElement(0)).get(0).hasAttribute('tabindex')).toBe(false); + expect(document.activeElement).toBe(cell); + }); + + it('keeps the pointer focus state of the focused row when it is expanded with repaintChangesOnly (T1338023)', async () => { + const { instance } = createTreeList({ + dataSource: DATA, + columns: ['id', 'name'], + focusedRowEnabled: true, + repaintChangesOnly: true, + autoExpandAll: false, + }); + await flushAsync(); + + const row = $(instance.getRowElement(0)).get(0); + click(instance.getCellElement(0, 'name') as HTMLElement); + await flushAsync(); + + expect(instance.option('focusedRowKey')).toBe(1); + expect(row.classList.contains('dx-cell-focus-disabled')).toBe(true); + + const expanding = instance.expandRow(1); + await flushAsync(); + await expanding; + + expect(visibleKeys(instance)).toEqual([1, 11, 2]); + expect(row.classList.contains('dx-cell-focus-disabled')).toBe(true); + expect(row.classList.contains('dx-focused')).toBe(false); + }); +});