Skip to content
Open
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo

private focusRestoreScrollTop = 0;

private isPageNavigationPending = false;

// #region Initialization
public init(): void {
this._dataController = this.getController('data');
Expand Down Expand Up @@ -333,6 +335,8 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
return;
}

this.completePageNavigation();
Comment thread
Copilot marked this conversation as resolved.
Resolved

const isFullUpdate = !e || e.changeType === 'refresh';
const isFocusedViewCorrect = this._focusedView?.name === this._rowsView.name;
const isAppend = e && (e.changeType === 'append' || e.changeType === 'prepend');
Expand All @@ -347,7 +351,7 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
this.initKeyDownHandler();
this._setRowsViewAttributes();

if (isFocusedViewCorrect && isFocusedElementCorrect) {
if (isFocusedViewCorrect && isFocusedElementCorrect && !this.isPageNavigationPending) {
const needUpdateFocus = this._isNeedFocus
? !isAppend
: this._isHiddenFocus && isFullUpdate && !e?.virtualColumnsScrolling;
Expand Down Expand Up @@ -745,7 +749,10 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
}

private isWaitingForRowsRendering(eventArgs: KeyboardKeyDownEvent): boolean {
return this._rowsView.isWaitingForRowsLayout()
const isWaitingForRows = this._rowsView.isWaitingForRowsLayout()
|| (this.isPageNavigationPending && this._rowsView.isViewportRowsRenderPending());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this condition, every key in FOCUSED_CELL_KEYS (including tab, enter, space, home/end, see const.ts:38) gets preventDefault() and stopPropagation() for the whole remote load after PageDown. Before, keys were blocked only during async template layout, which is short.

With a slow or hanging request, a keyboard user can't Tab out of the grid until the load ends, and Enter/Space presses are dropped, not queued. Could Tab/Shift+Tab at least be allowed to leave the grid while page navigation is pending, or the wait be limited to the keys that depend on the focused row position?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, with a slow request this is a long wait. I compared it with base: Tab during the same wait isn't better there. It runs with the stale row picked before the new rows arrived, so it doesn't reach the right cell either. With this PR, Tab is ignored until the load ends, and then focus is on the correct row. PageUp/PageDown, Ctrl+Up/Down, Escape and the mouse still work during the wait.

Letting Tab through isn't simple. The grid's Tab handler would move from the row before PageDown, which is scrolled away. A native Tab from the focused rows view can land on a cell or editor of the stale rows. Both need a browser check, and neither looks better than waiting. I'd keep this PR as is and handle "Tab out of the grid during a long load" in a separate task if we want it. What do you think?


return isWaitingForRows
&& !this._editingController?.isEditing()
&& !this.isInsideMasterDetail($(eventArgs.originalEvent.target));
}
Expand Down Expand Up @@ -2050,43 +2057,56 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
}

private restoreFocusableElement(rowIndex?: number, $event?: Event): void {
// eslint-disable-next-line @typescript-eslint/init-declarations -- set in the up-arrow branch
let args: FocusedRowChangingArgs | undefined;
const isUpArrow = isDefined(rowIndex);
const $rowsViewElement = this._rowsView.element();

if (!$rowsViewElement) {
return;
}

const { columnIndex } = this._focusedCellPosition;
const rowIndexOffset = this._dataController.getRowIndexOffset();

let newRowIndex = isUpArrow
? rowIndex
: this._rowsView.getTopVisibleItemIndex() + rowIndexOffset;

if (!isUpArrow) {
if (!isDefined(rowIndex)) {
this._editorFactory.loseFocus();
this.applyTabIndexToElement($rowsViewElement);
eventsEngine.trigger($rowsViewElement, 'focus');
} else {
// @ts-expect-error getRow() is typed as possibly undefined
const $rowElement: dxElementWrapper = this._rowsView.getRow(newRowIndex - rowIndexOffset);
args = this._fireFocusedRowChanging($event, $rowElement);

if (!args.cancel && args.rowIndexChanged) {
newRowIndex = args.newRowIndex;
}
this.isPageNavigationPending = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, and not a regression (the old code had the same gap): the flag is set only here. This code runs from _scrollBy, after the scrollable's next scroll event and then a setTimeout. Between PageDown and this callback, isWaitingForRowsRendering doesn't block anything. So an ArrowDown pressed right after PageDown moves from the old row and scrolls it back into view, undoing the page scroll.

Setting the flag when the PageUp/PageDown scroll starts would close most of that window. If no scroll event follows (for example, the grid is already scrolled to the boundary), the flag would need to be reset there, or it would stay pending.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this window exists in base too. I checked that ArrowDown pressed between PageDown and the scroll callback behaves the same with and without this PR: in both, the completion then moves focus to the new top row.

To set the flag on the key press, we'd need a reliable "no scroll will happen" signal (already at the top or bottom, or the scrollable clamps the distance). Otherwise the flag stays set and a later render overwrites the user's focus. I'd keep it out of this PR and can file a follow-up if you think it's worth it.

this.completePageNavigation();
return;
}

const { columnIndex } = this._focusedCellPosition;
const rowIndexOffset = this._dataController.getRowIndexOffset();
// @ts-expect-error getRow() is typed as possibly undefined
const $rowElement: dxElementWrapper = this._rowsView.getRow(rowIndex - rowIndexOffset);
const args = this._fireFocusedRowChanging($event, $rowElement);

if (!args.cancel) {
this.setFocusedCellPosition(args.rowIndexChanged ? args.newRowIndex : rowIndex, columnIndex);
}

if (!isUpArrow || !args?.cancel) {
this.setFocusedCellPosition(newRowIndex, columnIndex);
this.updateFocus(false);
}

public setFocusedRowIndex(rowIndex: number | undefined): void {
if (rowIndex !== this._focusedCellPosition?.rowIndex) {
this.isPageNavigationPending = false;
}
super.setFocusedRowIndex(rowIndex);
}

private getTopVisibleRowIndex(): number {
return this._rowsView.getTopVisibleItemIndex() + this._dataController.getRowIndexOffset();
}

if (isUpArrow) {
this.updateFocus(false);
private completePageNavigation(): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only runs from renderCompleted, so if the viewport update ends without a rows render, the flag stays set and _focusedCellPosition keeps the row from before PageDown.

Example: remote store, scrolling.mode: 'virtual', renderAsync: true, PageDown to a page that isn't cached, and the load fails. DataSourceAdapter.loadErrorHandler fires changed with changeType: 'loadError', and RowsView._handleDataChanged sends it to _update(), which doesn't render (only the focus and selection extenders handle their own change types there). So renderCompleted never fires. Once loading ends, keys are no longer blocked, and the next ArrowDown moves from the old row, which is the symptom this PR fixes. Before this change, restoreFocusableElement always set the position immediately.

This is a different path from the PageDown → PageUp reversal discussed above. Could completion be tied to the end of the viewport update itself (rendered, no-op, or failed) instead of the next renderCompleted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right: after a failed load the flag stays set and the position keeps the row from before PageDown until the next render.

The problem is that there is no correct row to complete with. A failed load doesn't re-render, so the DOM still has the old rows while the scroll position and the row index offset are already new. I tried completing the navigation on dataErrorOccurred: the pick landed on row 15, which isn't rendered, and arrows still did nothing. Base without renderAsync behaves the same after a failed load: focus stays on the rows view and arrows do nothing.

The "no-op" case doesn't occur: after the scroll timeout a render always follows (partialUpdateItem returns an update for every kept row, so cancelEmptyChanges doesn't cancel it).

After the error, the next successful render (retry, scroll, refresh) completes the navigation and focuses the top visible row. Any focus change (a click, the API) now drops the flag too (see the setFocusedRowIndex thread). So I'd leave the error case as is in this PR, unless you see a better target row for it.

if (!this.isPageNavigationPending || this._rowsView.isViewportRowsRenderPending()) {
return;
}

this.isPageNavigationPending = false;
this.setFocusedCellPosition(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up to the grid.focus() thread: clearing the flag in _focusElement and in the success branch of _clickTargetCellHandler covers those two paths. Other focus changes made while navigation is pending still get replaced with the top visible row here:

  • a click in a master-detail cell or in the free-space row: _pointerEventHandler calls _updateFocusedCellPosition directly;
  • args.resetFocusedRow in _clickTargetCellHandler: it returns before the flag is cleared, so the reset row is focused again on completion.

The pending window is the scroll timeout plus the whole remote load, so a click in master-detail during a slow load is realistic. Because the focus extender's setFocusedRowIndex writes the focusedRowIndex option, this also changes focusedRowKey and raises onFocusedRowChanged. I haven't traced whether option('focusedRowKey', ...) / navigateToRow() while pending end up in _focusElement. If they don't, they're affected too.

Since every new focus path has to remember to clear the flag, could the pending state be dropped whenever the focused position changes from anywhere other than this completion?

this.getTopVisibleRowIndex(),
this._focusedCellPosition.columnIndex,
);
Comment thread
Copilot marked this conversation as resolved.
Resolved
}

// #endregion Focusing
Expand Down Expand Up @@ -2813,7 +2833,7 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo
const { rowIndex = -1, columnIndex = -1 } = cellPosition;
const rowIndexOffset = this._dataController.getRowIndexOffset();
const column = this._columnsController.getVisibleColumns(null, true)[columnIndex];
const columnIndexOffset = column && column.fixed
const columnIndexOffset = column?.fixed
? this._getFixedColumnIndexOffset(column)
: this._columnsController.getColumnIndexOffset();

Expand Down Expand Up @@ -3265,7 +3285,7 @@ const editing = (
const visibleColumnIndex = this._keyboardNavigationController.getVisibleColumnIndex();
const column = this._columnsController.getVisibleColumns()[visibleColumnIndex];

if ((column && column.type) || this.getEditMode() === EDIT_MODE.form) {
if ((column?.type) || this.getEditMode() === EDIT_MODE.form) {
this._keyboardNavigationController.resetFocusedCell();
}
super.editRow(rowIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ export class RowsView extends ColumnsView {
return this._findContentElement();
}

/**
* @extended: virtual_scrolling
*/
public isViewportRowsRenderPending(): boolean {
return false;
}

public isWaitingForRowsLayout(): boolean {
return this.isWaitingForAsyncTemplates() && !this.isRenderedLayoutCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ export const virtualScrollingDataControllerExtender = (
}
}

public isViewportPositionPending(): boolean {
return !!this._rowsScrollController?.isViewportPositionPending();
Comment thread
Alyar666 marked this conversation as resolved.
Resolved
}

public setContentItemSizes(sizes: number[]): void {
const dataSourceAdapter = this.dataSourceController.getAdapter();
const result = this._rowsScrollController?.setContentItemSizes(sizes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface VirtualScrollingDataControllerExtension {
viewportHeight: (height: number, scrollTop?: number) => void;
viewportItemSize: (size?: number) => number | undefined;
setViewportPosition: (position: number) => void;
isViewportPositionPending: () => boolean;
setContentItemSizes: (sizes: number[]) => void;
getItemSize: () => number | undefined;
getItemSizes: () => ItemSizes | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,22 @@ export const rowsView = (
this._scrollToCurrentPageOnResize();
}

public isViewportRowsRenderPending(): boolean {
const scrolling = this.option('scrolling');
const isVirtualRender = isVirtualPaging(scrolling) || isVirtualRowRendering(scrolling);

if (!isVirtualRender) {
return false;
}

const isViewportDataLoading = this.dataSourceController.isLoading()
&& !this.dataSourceController.getAdapter()?.customLoader.isLoading();

return isViewportDataLoading
|| this._dataController.isViewportPositionPending()
|| this.isWaitingForRowsLayout();
}

private _scrollToCurrentPageOnResize(): void {
if (this._dataController.pageIndex() > 0) {
const resizeHandler = (): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ class VirtualScrollController<TItem = unknown> {
this._dataLoader = new VirtualDataLoader(this, this._dataOptions);
}

private clearScrollTimeout(): void {
clearTimeout(this._scrollTimeoutID);
this._scrollTimeoutID = undefined;
}

public getItemSizes(): ItemSizes {
return this._itemSizes;
}
Expand Down Expand Up @@ -250,10 +255,12 @@ class VirtualScrollController<TItem = unknown> {
const result = Deferred<unknown>();
const scrollingTimeout = this.getScrollingTimeout();

clearTimeout(this._scrollTimeoutID);
this.clearScrollTimeout();

if (scrollingTimeout > 0) {
// eslint-disable-next-line no-restricted-globals -- delays rendering by scrolling.timeout
this._scrollTimeoutID = setTimeout(() => {
this._scrollTimeoutID = undefined;
this._setViewportPositionCore(position);
result.resolve();
}, scrollingTimeout);
Expand All @@ -265,6 +272,10 @@ class VirtualScrollController<TItem = unknown> {
return result.promise();
}

public isViewportPositionPending(): boolean {
return isDefined(this._scrollTimeoutID);
}

public getViewportPosition(): number {
return this._position;
}
Expand Down Expand Up @@ -439,7 +450,7 @@ class VirtualScrollController<TItem = unknown> {
}

public dispose(): void {
clearTimeout(this._scrollTimeoutID);
this.clearScrollTimeout();
this._windowScroll?.dispose();
this._windowScroll = null;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/devextreme/testing/helpers/gridBaseMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ module.exports = function($, gridCore, columnResizingReordering, domUtils, commo

setViewportPosition: function() {},

isViewportPositionPending: function() {
return false;
},

getVirtualContentSize: function() { return 0; },

getContentOffset: function(type) { return options.virtualItemsCount ? options.virtualItemsCount[type || 'begin'] * 20 : 0; },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5704,6 +5704,7 @@ QUnit.module('Virtual scrolling', {
rowsView._dataController.viewportItemSize = x.viewportItemSize;
rowsView._dataController.setContentItemSizes = x.setContentItemSizes;
rowsView._dataController.setViewportPosition = x.setViewportPosition;
rowsView._dataController.clearScrollTimeout = x.clearScrollTimeout;
rowsView._dataController.getItemIndexByPosition = x.getItemIndexByPosition;
rowsView._dataController._setViewportPositionCore = x._setViewportPositionCore;
rowsView._dataController.option = rowsView.option.bind(rowsView);
Expand Down
Loading