Skip to content

Commit

Permalink
Revert "Call scrollToIndex only when ref is set and index is in range (
Browse files Browse the repository at this point in the history
…mattermost#3282)" (mattermost#3284)

This reverts commit ecbbb3f.
  • Loading branch information
sudheerDev authored and migbot committed Sep 19, 2019
1 parent ecbbb3f commit 0f67b47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
31 changes: 14 additions & 17 deletions app/components/post_list/post_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,33 +299,30 @@ export default class PostList extends PureComponent {

scrollToBottom = () => {
setTimeout(() => {
if (this.flatListRef?.current) {
if (this.flatListRef && this.flatListRef.current) {
this.flatListRef.current.scrollToOffset({offset: 0, animated: true});
}
}, 250);
};

flatListScrollToIndex = (index) => {
this.flatListRef.current.scrollToIndex({
animated: false,
index,
viewOffset: 0,
viewPosition: 1, // 0 is at bottom
});
}

scrollToIndex = (index) => {
this.animationFrameInitialIndex = requestAnimationFrame(() => {
if (this.flatListRef?.current && index > 0 && index <= this.getItemCount()) {
this.flatListScrollToIndex(index);
}
});
if (this.flatListRef?.current) {
this.animationFrameInitialIndex = requestAnimationFrame(() => {
this.flatListRef.current.scrollToIndex({
animated: false,
index,
viewOffset: 0,
viewPosition: 1, // 0 is at bottom
});
});
}
};

scrollToInitialIndexIfNeeded = (index, count = 0) => {
if (!this.hasDoneInitialScroll) {
if (!this.hasDoneInitialScroll && this.flatListRef?.current) {
this.hasDoneInitialScroll = true;

if (index > 0 && index <= this.getItemCount()) {
this.hasDoneInitialScroll = true;
this.scrollToIndex(index);
} else if (count < 3) {
setTimeout(() => {
Expand Down
26 changes: 0 additions & 26 deletions app/components/post_list/post_list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,4 @@ describe('PostList', () => {
expect(baseProps.actions.handleSelectChannelByName).toHaveBeenCalled();
expect(wrapper.getElement()).toMatchSnapshot();
});

test('should call flatListScrollToIndex only when ref is set and index is in range', () => {
jest.spyOn(global, 'requestAnimationFrame').mockImplementation((cb) => cb());

const instance = wrapper.instance();
const flatListScrollToIndex = jest.spyOn(instance, 'flatListScrollToIndex');
const indexInRange = baseProps.postIds.length;
const indexOutOfRange = [-1, indexInRange + 1];

instance.flatListRef = {};
instance.scrollToIndex(indexInRange);
expect(flatListScrollToIndex).not.toHaveBeenCalled();

instance.flatListRef = {
current: {
scrollToIndex: jest.fn(),
},
};
for (const index of indexOutOfRange) {
instance.scrollToIndex(index);
expect(flatListScrollToIndex).not.toHaveBeenCalled();
}

instance.scrollToIndex(indexInRange);
expect(flatListScrollToIndex).toHaveBeenCalled();
});
});

0 comments on commit 0f67b47

Please sign in to comment.