Skip to content

Prevent toggling between min and max scroll offsets at end of grid #267

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

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
6 changes: 6 additions & 0 deletions src/FixedSizeGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const FixedSizeGrid = createGridComponent({
default:
if (scrollLeft >= minOffset && scrollLeft <= maxOffset) {
return scrollLeft;
} else if (minOffset > maxOffset) {
return minOffset;
Copy link
Owner

Choose a reason for hiding this comment

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

I think this needs an explanatory comment, because it looks like a bug at first glance 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point! Now added.

Copy link
Owner

Choose a reason for hiding this comment

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

Looks like you added other places but missed this one 😆

} else if (scrollLeft - minOffset < maxOffset - scrollLeft) {
return minOffset;
} else {
Expand Down Expand Up @@ -115,6 +117,10 @@ const FixedSizeGrid = createGridComponent({
default:
if (scrollTop >= minOffset && scrollTop <= maxOffset) {
return scrollTop;
// Because we only take into account the scrollbar size when calculating minOffset
// this value can be larger than maxOffset when at the end of the list
} else if (minOffset > maxOffset) {
return minOffset;
} else if (scrollTop - minOffset < maxOffset - scrollTop) {
return minOffset;
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/VariableSizeGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ const getOffsetForIndexAndAlignment = (
default:
if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
return scrollOffset;
// Because we only take into account the scrollbar size when calculating minOffset
// this value can be larger than maxOffset when at the end of the list
} else if (minOffset > maxOffset) {
return minOffset;
} else if (scrollOffset - minOffset < maxOffset - scrollOffset) {
return minOffset;
} else {
Expand Down
75 changes: 75 additions & 0 deletions src/__tests__/FixedSizeGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,81 @@ describe('FixedSizeGrid', () => {
expect(onItemsRendered.mock.calls).toMatchSnapshot();
});

it('should scroll to the correct item for align = "auto" at the bottom of the grid', () => {
getScrollbarSize.mockImplementation(() => 20);

const rendered = ReactTestRenderer.create(
<FixedSizeGrid {...defaultProps} rowCount={20} rowHeight={30} />
);
onItemsRendered.mockClear();

// Scroll down to the last row in the list.
rendered
.getInstance()
.scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });

expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleRowStartIndex: 17,
visibleRowStopIndex: 19,
})
);
// Repeat the previous scrollToItem call.
rendered
.getInstance()
.scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });

// Shouldn't have been called again
expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleRowStartIndex: 17,
visibleRowStopIndex: 19,
})
);
});

it('should scroll to the correct item for align = "auto" at the end of the grid', () => {
getScrollbarSize.mockImplementation(() => 20);

const rendered = ReactTestRenderer.create(
<FixedSizeGrid
{...defaultProps}
columnCount={20}
columnWidth={50}
width={120}
/>
);
onItemsRendered.mockClear();

// Scroll across to the last row in the list.
rendered
.getInstance()
.scrollToItem({ columnIndex: 19, rowIndex: 19, align: 'auto' });

expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleColumnStartIndex: 18,
visibleColumnStopIndex: 19,
})
);
// Repeat the previous scrollToItem call.
rendered
.getInstance()
.scrollToItem({ columnIndex: 19, rowIndex: 19, align: 'auto' });

// Shouldn't have been called again
expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleColumnStartIndex: 18,
visibleColumnStopIndex: 19,
})
);
});

it('should scroll to the correct item for align = "start"', () => {
const rendered = ReactTestRenderer.create(
<FixedSizeGrid {...defaultProps} />
Expand Down
70 changes: 70 additions & 0 deletions src/__tests__/VariableSizeGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,76 @@ describe('VariableSizeGrid', () => {
expect(onItemsRendered.mock.calls).toMatchSnapshot();
});

it('should scroll to the correct item for align = "auto" at the bottom of the grid', () => {
getScrollbarSize.mockImplementation(() => 20);

const rendered = ReactTestRenderer.create(
<VariableSizeGrid {...defaultProps} />
);
onItemsRendered.mockClear();

// Scroll down to the last row in the list.
rendered
.getInstance()
.scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });

expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleRowStartIndex: 18,
visibleRowStopIndex: 19,
})
);
// Repeat the previous scrollToItem call.
rendered
.getInstance()
.scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });

// Shouldn't have been called again
expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleRowStartIndex: 18,
visibleRowStopIndex: 19,
})
);
});

it('should scroll to the correct item for align = "auto" at the end of the grid', () => {
getScrollbarSize.mockImplementation(() => 20);

const rendered = ReactTestRenderer.create(
<VariableSizeGrid {...defaultProps} width={120} />
);
onItemsRendered.mockClear();

// Scroll scross to the last row in the list.
rendered
.getInstance()
.scrollToItem({ columnIndex: 9, rowIndex: 10, align: 'auto' });

expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleColumnStartIndex: 8,
visibleColumnStopIndex: 9,
})
);
// Repeat the previous scrollToItem call.
rendered
.getInstance()
.scrollToItem({ columnIndex: 9, rowIndex: 10, align: 'auto' });

// Shouldn't have been called again
expect(onItemsRendered).toHaveBeenCalledTimes(1);
expect(onItemsRendered).toHaveBeenLastCalledWith(
expect.objectContaining({
visibleColumnStartIndex: 8,
visibleColumnStopIndex: 9,
})
);
});

it('should scroll to the correct item for align = "start"', () => {
const rendered = ReactTestRenderer.create(
<VariableSizeGrid {...defaultProps} />
Expand Down