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

Web - Profile - When selecting the last country or state extra space is displayed for a while #31737

Merged
Show file tree
Hide file tree
Changes from 5 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
31 changes: 23 additions & 8 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function BaseSelectionList({
disableKeyboardShortcuts = false,
children,
shouldStopPropagation = false,
shouldUseDynamicMaxToRenderPerBatch = false,
}) {
const theme = useTheme();
const styles = useThemeStyles();
Expand All @@ -71,6 +72,8 @@ function BaseSelectionList({
const shouldShowSelectAll = Boolean(onSelectAll);
const activeElement = useActiveElement();
const isFocused = useIsFocused();
const [maxToRenderPerBatch, setMaxToRenderPerBatch] = useState(shouldUseDynamicMaxToRenderPerBatch ? 0 : 5);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could extract this 5 to some constant with meaningful name.


/**
* Iterates through the sections and items inside each section, and builds 3 arrays along the way:
* - `allOptions`: Contains all the items in the list, flattened, regardless of section
Expand Down Expand Up @@ -301,6 +304,7 @@ function BaseSelectionList({
item={item}
isFocused={isItemFocused}
isDisabled={isDisabled}
isHide={!maxToRenderPerBatch}
showTooltip={showTooltip}
canSelectMultiple={canSelectMultiple}
onSelectRow={() => selectRow(item, true)}
Expand All @@ -310,13 +314,23 @@ function BaseSelectionList({
);
};

const scrollToFocusedIndexOnFirstRender = useCallback(() => {
if (!firstLayoutRef.current) {
return;
}
scrollToIndex(focusedIndex, false);
firstLayoutRef.current = false;
}, [focusedIndex, scrollToIndex]);
const scrollToFocusedIndexOnFirstRender = useCallback(
({nativeEvent}) => {
if (shouldUseDynamicMaxToRenderPerBatch) {
const listHeight = lodashGet(nativeEvent, 'layout.height', 0);
const itemHeight = lodashGet(nativeEvent, 'layout.y', 0);

setMaxToRenderPerBatch((Math.ceil(listHeight / itemHeight) || 0) + 5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this 5 here the same as the one I've mentioned in my previous comment? If so the constant has even more sense I think.

}

if (!firstLayoutRef.current) {
return;
}
scrollToIndex(focusedIndex, false);
firstLayoutRef.current = false;
},
[focusedIndex, scrollToIndex, shouldUseDynamicMaxToRenderPerBatch],
);

const updateAndScrollToFocusedIndex = useCallback(
(newFocusedIndex) => {
Expand Down Expand Up @@ -451,11 +465,12 @@ function BaseSelectionList({
keyboardShouldPersistTaps="always"
showsVerticalScrollIndicator={showScrollIndicator}
initialNumToRender={12}
maxToRenderPerBatch={5}
maxToRenderPerBatch={maxToRenderPerBatch}
windowSize={5}
viewabilityConfig={{viewAreaCoveragePercentThreshold: 95}}
testID="selection-list"
onLayout={scrollToFocusedIndexOnFirstRender}
style={!maxToRenderPerBatch && styles.opacity0}
/>
{children}
</>
Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectionList/selectionListPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ const propTypes = {

/** Custom content to display in the footer */
footerContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),

/** Whether to use dynamic maxToRenderPerBatch depending on the visible number of elements */
shouldUseDynamicMaxToRenderPerBatch: PropTypes.bool,
};

export {propTypes, baseListItemPropTypes, radioListItemPropTypes, userListItemPropTypes};
1 change: 1 addition & 0 deletions src/components/StatePicker/StateSelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function StateSelectorModal({currentState, isVisible, onClose, onStateSelected,
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentState}
shouldStopPropagation
shouldUseDynamicMaxToRenderPerBatch
/>
</ScreenWrapper>
</Modal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function CountrySelectionPage({route, navigation}) {
onSelectRow={selectCountry}
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentCountry}
shouldUseDynamicMaxToRenderPerBatch
/>
</ScreenWrapper>
);
Expand Down
Loading