Skip to content

Commit

Permalink
Merge pull request #31262 from fedirjh/revert-30972-fix/29780/change-…
Browse files Browse the repository at this point in the history
…implementation-of-initial-focused-item

(cherry picked from commit 2bf29e5)
  • Loading branch information
cead22 authored and OSBotify committed Nov 14, 2023
1 parent c8d56be commit 50eb67f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ function BaseSelectionList({

// If `initiallyFocusedOptionKey` is not passed, we fall back to `-1`, to avoid showing the highlight on the first member
const [focusedIndex, setFocusedIndex] = useState(() => _.findIndex(flattenedSections.allOptions, (option) => option.keyForList === initiallyFocusedOptionKey));
// initialFocusedIndex is needed only on component did mount event, don't need to update value
// eslint-disable-next-line react-hooks/exhaustive-deps
const initialFocusedIndex = useMemo(() => (focusedIndex > -1 ? focusedIndex : undefined), []);

// Disable `Enter` shortcut if the active element is a button or checkbox
const disableEnterShortcut = activeElement && [CONST.ACCESSIBILITY_ROLE.BUTTON, CONST.ACCESSIBILITY_ROLE.CHECKBOX].includes(activeElement.role);
Expand Down Expand Up @@ -310,9 +307,14 @@ function BaseSelectionList({
/>
);
};
const handleFirstLayout = useCallback(() => {

const scrollToFocusedIndexOnFirstRender = useCallback(() => {
if (!firstLayoutRef.current) {
return;
}
scrollToIndex(focusedIndex, false);
firstLayoutRef.current = false;
}, []);
}, [focusedIndex, scrollToIndex]);

const updateAndScrollToFocusedIndex = useCallback(
(newFocusedIndex) => {
Expand Down Expand Up @@ -452,8 +454,7 @@ function BaseSelectionList({
viewabilityConfig={{viewAreaCoveragePercentThreshold: 95}}
testID="selection-list"
style={[styles.flexGrow0]}
onLayout={handleFirstLayout}
initialScrollIndex={initialFocusedIndex}
onLayout={scrollToFocusedIndexOnFirstRender}
/>
{children}
</>
Expand Down

0 comments on commit 50eb67f

Please sign in to comment.