Skip to content

Commit

Permalink
Merge pull request Expensify#34196 from abzokhattab/fix-base-selectio…
Browse files Browse the repository at this point in the history
…n-list-focus

Fix selection list focus
  • Loading branch information
MonilBhavsar authored Feb 15, 2024
2 parents 634f766 + f4393b1 commit abe77e9
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {ForwardedRef} from 'react';
import {View} from 'react-native';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {LayoutChangeEvent, SectionList as RNSectionList, TextInput as RNTextInput, SectionListRenderItemInfo} from 'react-native';
import {View} from 'react-native';
import ArrowKeyFocusManager from '@components/ArrowKeyFocusManager';
import Button from '@components/Button';
import Checkbox from '@components/Checkbox';
Expand All @@ -16,6 +16,7 @@ import TextInput from '@components/TextInput';
import useActiveElementRole from '@hooks/useActiveElementRole';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import Log from '@libs/Log';
import variables from '@styles/variables';
Expand Down Expand Up @@ -347,18 +348,17 @@ function BaseSelectionList<TItem extends User | RadioItem>(
}, [shouldShowTextInput]),
);

const prevTextInputValue = usePrevious(textInputValue);
useEffect(() => {
// do not change focus on the first render, as it should focus on the selected item
if (isInitialSectionListRender) {
// Avoid changing focus if the textInputValue remains unchanged.
if (prevTextInputValue === textInputValue || flattenedSections.allOptions.length === 0) {
return;
}
// Remove the focus if the search input is empty else focus on the first non disabled item
const newSelectedIndex = textInputValue === '' ? -1 : 0;

// set the focus on the first item when the sections list is changed
if (sections.length > 0) {
updateAndScrollToFocusedIndex(0);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sections]);
updateAndScrollToFocusedIndex(newSelectedIndex);
}, [canSelectMultiple, flattenedSections.allOptions.length, prevTextInputValue, textInputValue, updateAndScrollToFocusedIndex]);

/** Selects row when pressing Enter */
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, {
Expand Down

0 comments on commit abe77e9

Please sign in to comment.