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
21 changes: 10 additions & 11 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Log from '@libs/Log';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getEmptyArray from '@src/types/utils/getEmptyArray';
import arraysEqual from '@src/utils/arraysEqual';
import BaseSelectionListItemRenderer from './BaseSelectionListItemRenderer';
import FocusAwareCellRendererComponent from './FocusAwareCellRendererComponent';
Expand Down Expand Up @@ -144,7 +145,7 @@ function BaseSelectionList<TItem extends ListItem>({
loaderSpeed,
errorText,
shouldUseDefaultRightHandSideCheckmark,
selectedItems = [],
selectedItems = getEmptyArray<string>(),
isSelected,
canShowProductTrainingTooltip,
renderScrollComponent,
Expand Down Expand Up @@ -191,7 +192,7 @@ function BaseSelectionList<TItem extends ListItem>({
const [currentPage, setCurrentPage] = useState(() => calculateInitialCurrentPage());
const isTextInputFocusedRef = useRef<boolean>(false);
const {singleExecution} = useSingleExecution();
const [itemHeights, setItemHeights] = useState<Record<string, number>>({});
const itemHeights = useRef<Record<string, number>>({});
const pendingScrollIndexRef = useRef<number | null>(null);

const onItemLayout = (event: LayoutChangeEvent, itemKey: string | null | undefined) => {
Expand All @@ -200,11 +201,10 @@ function BaseSelectionList<TItem extends ListItem>({
}

const {height} = event.nativeEvent.layout;

setItemHeights((prevHeights) => ({
...prevHeights,
itemHeights.current = {
...itemHeights.current,
[itemKey]: height,
}));
};
};

const canShowProductTrainingTooltipMemo = useMemo(() => {
Expand Down Expand Up @@ -257,7 +257,7 @@ function BaseSelectionList<TItem extends ListItem>({
disabledIndex += 1;

// Account for the height of the item in getItemLayout
const fullItemHeight = item?.keyForList && itemHeights[item.keyForList] ? itemHeights[item.keyForList] : getItemHeight(item);
const fullItemHeight = item?.keyForList && itemHeights.current[item.keyForList] ? itemHeights.current[item.keyForList] : getItemHeight(item);
itemLayouts.push({length: fullItemHeight, offset});
offset += fullItemHeight;

Expand All @@ -281,7 +281,6 @@ function BaseSelectionList<TItem extends ListItem>({
);
}
const totalSelectable = allOptions.length - disabledOptionsIndexes.length;

return {
allOptions,
selectedOptions,
Expand All @@ -291,7 +290,7 @@ function BaseSelectionList<TItem extends ListItem>({
allSelected: selectedOptions.length > 0 && selectedOptions.length === totalSelectable,
someSelected: selectedOptions.length > 0 && selectedOptions.length < totalSelectable,
};
}, [customListHeader, customListHeaderHeight, sections, canSelectMultiple, isItemSelected, itemHeights, getItemHeight]);
}, [customListHeader, customListHeaderHeight, sections, canSelectMultiple, isItemSelected, getItemHeight]);

const incrementPage = useCallback(() => {
if (flattenedSections.allOptions.length <= CONST.MAX_SELECTION_LIST_PAGE_LENGTH * currentPage) {
Expand Down Expand Up @@ -352,9 +351,9 @@ function BaseSelectionList<TItem extends ListItem>({
// the top of the viewable area at all times by adjusting the viewOffset.
if (shouldKeepFocusedItemAtTopOfViewableArea) {
const firstPreviousItem = index > 0 ? flattenedSections.allOptions.at(index - 1) : undefined;
const firstPreviousItemHeight = firstPreviousItem && firstPreviousItem.keyForList ? itemHeights[firstPreviousItem.keyForList] : 0;
const firstPreviousItemHeight = firstPreviousItem && firstPreviousItem.keyForList ? itemHeights.current[firstPreviousItem.keyForList] : 0;
const secondPreviousItem = index > 1 ? flattenedSections.allOptions.at(index - 2) : undefined;
const secondPreviousItemHeight = secondPreviousItem && secondPreviousItem?.keyForList ? itemHeights[secondPreviousItem.keyForList] : 0;
const secondPreviousItemHeight = secondPreviousItem && secondPreviousItem?.keyForList ? itemHeights.current[secondPreviousItem.keyForList] : 0;
viewOffsetToKeepFocusedItemAtTopOfViewableArea = firstPreviousItemHeight + secondPreviousItemHeight;
}

Expand Down
Loading