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

Scroll when container contracted to hide current scroll #798

Merged
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
72 changes: 72 additions & 0 deletions source/Grid/Grid.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,78 @@ describe("Grid", () => {
expect(rendered.textContent).not.toContain("column:0");
expect(rendered.textContent).not.toContain("row:0");
});

it("should scroll to the last existing point when rows are removed", () => {
const grid = render(
getMarkup({
rowCount: 15
})
);

simulateScroll({
grid,
scrollTop: 200
});

const updatedGrid = render(
getMarkup({
rowCount: 10
})
);

expect(updatedGrid.state.scrollTop).toEqual(100);
});

it("should scroll to the last existing point when columns are removed", () => {
const grid = render(
getMarkup({
columnCount: 12
})
);

simulateScroll({
grid,
scrollLeft: 400
});

const updatedGrid = render(
getMarkup({
columnCount: 8
})
);

expect(updatedGrid.state.scrollLeft).toEqual(200);
});

it("should not scroll unseen rows are removed", () => {
render(
getMarkup({
rowCount: 15
})
);
const updatedGrid = render(
getMarkup({
rowCount: 10
})
);

expect(updatedGrid.state.scrollTop).toEqual(0);
});

it("should not scroll when unseen columns are removed", () => {
render(
getMarkup({
columnCount: 12
})
);
const updatedGrid = render(
getMarkup({
columnCount: 8
})
);

expect(updatedGrid.state.scrollLeft).toEqual(0);
});
});

describe("shows and hides scrollbars based on rendered content", () => {
Expand Down
29 changes: 17 additions & 12 deletions source/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,19 @@ export default class Grid extends React.PureComponent {

// Don't adjust scroll offset for single-column grids (eg List, Table).
// This can cause a funky scroll offset because of the vertical scrollbar width.
if (columnCount > 1) {
if (columnCount > 1 && columnIndex !== undefined) {
this._updateScrollLeftForScrollToColumn({
...props,
scrollToColumn: columnIndex
});
}

this._updateScrollTopForScrollToRow({
...props,
scrollToRow: rowIndex
});
if (rowIndex !== undefined) {
this._updateScrollTopForScrollToRow({
...props,
scrollToRow: rowIndex
});
}
}

componentDidMount() {
Expand Down Expand Up @@ -1261,11 +1263,12 @@ export default class Grid extends React.PureComponent {
} = props;
const { scrollLeft } = state;

if (scrollToColumn >= 0 && columnCount > 0) {
const targetIndex = Math.max(
0,
Math.min(columnCount - 1, scrollToColumn)
);
if (columnCount > 0) {
const finalColumn = columnCount - 1;
const targetIndex =
scrollToColumn < 0
? finalColumn
: Math.min(finalColumn, scrollToColumn);
const totalRowsHeight = this._rowSizeAndPositionManager.getTotalSize();
const scrollBarSize = totalRowsHeight > height ? this._scrollbarSize : 0;

Expand Down Expand Up @@ -1304,8 +1307,10 @@ export default class Grid extends React.PureComponent {
const { height, rowCount, scrollToAlignment, scrollToRow, width } = props;
const { scrollTop } = state;

if (scrollToRow >= 0 && rowCount > 0) {
const targetIndex = Math.max(0, Math.min(rowCount - 1, scrollToRow));
if (rowCount > 0) {
const finalRow = rowCount - 1;
const targetIndex =
scrollToRow < 0 ? finalRow : Math.min(finalRow, scrollToRow);
const totalColumnsWidth = this._columnSizeAndPositionManager.getTotalSize();
const scrollBarSize = totalColumnsWidth > width ? this._scrollbarSize : 0;

Expand Down