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

fix: stuck to bottom on filter clear #1579

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
57 changes: 46 additions & 11 deletions packages/grid/src/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1181,26 +1181,28 @@ class Grid extends PureComponent<GridProps, GridState> {
* @param deltaColumn Number of columns to move the cursor
* @param deltaRow Number of rows to move the cursor
* @param extendSelection True if the current selection should be extended, false to start a new selection
* @param event The event that triggered the update.
*/
moveCursor(
deltaColumn: number,
deltaRow: number,
extendSelection: boolean
extendSelection: boolean,
event?: GridKeyboardEvent
): void {
const { cursorRow, cursorColumn, selectionEndColumn, selectionEndRow } =
this.state;
const column = extendSelection ? selectionEndColumn : cursorColumn;
const row = extendSelection ? selectionEndRow : cursorRow;
if (row === null || column === null) {
const { left, top } = this.state;
this.moveCursorToPosition(left, top, extendSelection);
this.moveCursorToPosition(left, top, extendSelection, true, false, event);
} else {
const { model } = this.props;
const { columnCount, rowCount } = model;

const left = clamp(column + deltaColumn, 0, columnCount - 1);
const top = clamp(row + deltaRow, 0, rowCount - 1);
this.moveCursorToPosition(left, top, extendSelection);
this.moveCursorToPosition(left, top, extendSelection, true, false, event);
}
}

Expand Down Expand Up @@ -1260,13 +1262,15 @@ class Grid extends PureComponent<GridProps, GridState> {
* @param extendSelection Whether to extend the current selection (eg. holding Shift)
* @param keepCursorInView Whether to move the viewport so that the cursor is in view
* @param maximizePreviousRange With this and `extendSelection` true, it will maximize/add to the previous range only, ignoring where the selection was started
* @param event The event that triggered the update.
*/
moveCursorToPosition(
column: GridRangeIndex,
row: GridRangeIndex,
extendSelection = false,
keepCursorInView = true,
maximizePreviousRange = false
maximizePreviousRange = false,
event?: GridKeyboardEvent
): void {
if (!extendSelection) {
this.beginSelection(column, row);
Expand All @@ -1275,7 +1279,7 @@ class Grid extends PureComponent<GridProps, GridState> {
this.moveSelection(column, row, extendSelection, maximizePreviousRange);

if (keepCursorInView) {
this.moveViewToCell(column, row);
this.moveViewToCell(column, row, event);
}
}

Expand All @@ -1284,8 +1288,13 @@ class Grid extends PureComponent<GridProps, GridState> {
*
* @param column The column index to bring into view
* @param row The row index to bring into view
* @param event The event that triggered the update.
*/
moveViewToCell(column: GridRangeIndex, row: GridRangeIndex): void {
moveViewToCell(
column: GridRangeIndex,
row: GridRangeIndex,
event?: GridKeyboardEvent
): void {
if (!this.metrics) throw new Error('metrics not set');

const { metricCalculator } = this;
Expand Down Expand Up @@ -1314,26 +1323,52 @@ class Grid extends PureComponent<GridProps, GridState> {
}
}

this.setViewState({ top, left, topOffset, leftOffset });
this.setViewState({ top, left, topOffset, leftOffset }, false, event);
}

/**
* Checks the `top` and `left` properties that are set and updates the isStuckToBottom/Right properties
* Should be called when user interaction occurs
* @param viewState New state properties to set.
* @param forceUpdate Whether to force an update.
* @param event The event that triggered the update.
*/
setViewState(viewState: Partial<GridState>, forceUpdate = false): void {

setViewState(
viewState: Partial<GridState>,
forceUpdate = false,
event?: WheelEvent | GridKeyboardEvent
): void {
if (!this.metrics) throw new Error('metrics not set');

const { isStickyBottom, isStickyRight } = this.props;
const { top, left } = viewState;
const { lastTop, lastLeft } = this.metrics;
let isUserInputDown = false;
let isUserInputRight = false;

if (event instanceof WheelEvent) {
isUserInputDown = event.deltaY > 0;
isUserInputRight = event.deltaX > 0;
} else if (
event instanceof KeyboardEvent ||
(event && 'nativeEvent' in event) // used to catch the case that a synthetic react keyboard event is passed
) {
isUserInputDown =
event.key === 'ArrowDown' ||
event.key === 'End' ||
event.key === 'PageDown';
isUserInputRight = event.key === 'ArrowRight';
}
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
if (top != null) {
this.setState({ isStuckToBottom: isStickyBottom && top >= lastTop });
this.setState({
isStuckToBottom: isStickyBottom && top >= lastTop && isUserInputDown,
});
}
if (left != null) {
this.setState({ isStuckToRight: isStickyRight && left >= lastLeft });
this.setState({
isStuckToRight: isStickyRight && left >= lastLeft && isUserInputRight,
});
}

this.setState(viewState as GridState);
Expand Down Expand Up @@ -1978,7 +2013,7 @@ class Grid extends PureComponent<GridProps, GridState> {
}
}

this.setViewState({ top, left, leftOffset, topOffset });
this.setViewState({ top, left, leftOffset, topOffset }, false, event);

event.stopPropagation();
event.preventDefault();
Expand Down
8 changes: 4 additions & 4 deletions packages/grid/src/key-handlers/SelectionKeyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class SelectionKeyHandler extends KeyHandler {

grid.moveCursorToPosition(left, cursorRow, isShiftKey, false);

grid.setViewState({ left });
grid.setViewState({ left }, false, event);
} else if (autoSelectColumn && deltaRow !== 0) {
const { lastTop } = grid.metrics;
let { top } = grid.state;
Expand All @@ -194,9 +194,9 @@ class SelectionKeyHandler extends KeyHandler {

grid.moveCursorToPosition(top, cursorColumn, isShiftKey, false);

grid.setViewState({ top });
grid.setViewState({ top }, false, event);
} else {
grid.moveCursor(deltaColumn, deltaRow, isShiftKey);
grid.moveCursor(deltaColumn, deltaRow, isShiftKey, event);
}
}
return true;
Expand Down Expand Up @@ -279,7 +279,7 @@ class SelectionKeyHandler extends KeyHandler {
isShiftKey,
false
);
grid.setViewState({ top: viewportPosition });
grid.setViewState({ top: viewportPosition }, false, e);

return true;
}
Expand Down
Loading