Skip to content
Merged
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
38 changes: 20 additions & 18 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,33 @@ function BaseSelectionList({
* @param {Number} index - the index of the item to scroll to
* @param {Boolean} animated - whether to animate the scroll
*/
const scrollToIndex = useCallback((index, animated = true) => {
const item = flattenedSections.allOptions[index];
const scrollToIndex = useCallback(
(index, animated = true) => {
const item = flattenedSections.allOptions[index];

if (!listRef.current || !item) {
return;
}
if (!listRef.current || !item) {
return;
}

const itemIndex = item.index;
const sectionIndex = item.sectionIndex;
const itemIndex = item.index;
const sectionIndex = item.sectionIndex;

// Note: react-native's SectionList automatically strips out any empty sections.
// So we need to reduce the sectionIndex to remove any empty sections in front of the one we're trying to scroll to.
// Otherwise, it will cause an index-out-of-bounds error and crash the app.
let adjustedSectionIndex = sectionIndex;
for (let i = 0; i < sectionIndex; i++) {
if (_.isEmpty(lodashGet(sections, `[${i}].data`))) {
adjustedSectionIndex--;
// Note: react-native's SectionList automatically strips out any empty sections.
// So we need to reduce the sectionIndex to remove any empty sections in front of the one we're trying to scroll to.
// Otherwise, it will cause an index-out-of-bounds error and crash the app.
let adjustedSectionIndex = sectionIndex;
for (let i = 0; i < sectionIndex; i++) {
if (_.isEmpty(lodashGet(sections, `[${i}].data`))) {
adjustedSectionIndex--;
}
}
}

listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight});
listRef.current.scrollToLocation({sectionIndex: adjustedSectionIndex, itemIndex, animated, viewOffset: variables.contentHeaderHeight});
},

// If we don't disable dependencies here, we would need to make sure that the `sections` prop is stable in every usage of this component.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
[flattenedSections.allOptions],
);

/**
* Logic to run when a row is selected, either with click/press or keyboard hotkeys.
Expand Down