Repository navigation
DataGrid: Fix PageUp/PageDown focusing a row from the previous scroll position when rows render asynchronously #36298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
958d9f3
5e6d19c
46b2649
270250e
514dd48
287f030
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
|
@@ -333,6 +335,8 @@ export class KeyboardNavigationController extends KeyboardNavigationControllerCo | |
| return; | ||
| } | ||
|
|
||
| this.completePageNavigation(); | ||
|
|
||
| const isFullUpdate = !e || e.changeType === 'refresh'; | ||
| const isFocusedViewCorrect = this._focusedView?.name === this._rowsView.name; | ||
| const isAppend = e && (e.changeType === 'append' || e.changeType === 'prepend'); | ||
|
|
@@ -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; | ||
|
|
@@ -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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this condition, every key in 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Setting the flag when the PageUp/PageDown scroll starts would close most of that window. If no
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only runs from Example: remote store, 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow-up to the
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 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, | ||
| ); | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // #endregion Focusing | ||
|
|
@@ -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(); | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.