Skip to content

Commit

Permalink
Merge pull request #30438 from abzokhattab/focus-on-first-option-when…
Browse files Browse the repository at this point in the history
…-user-changes-the-search-input-in-selectionlist

Focus on the first item of the selection list when user changes the search input
  • Loading branch information
MonilBhavsar committed Oct 31, 2023
2 parents cbd3001 + 73847d6 commit deb267f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import ArrowKeyFocusManager from '@components/ArrowKeyFocusManager';
Expand Down Expand Up @@ -339,6 +339,19 @@ function BaseSelectionList({
}, [shouldShowTextInput]),
);

useEffect(() => {
// do not change focus on the first render, as it should focus on the selected item
if (firstLayoutRef.current) {
return;
}

// 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]);

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

0 comments on commit deb267f

Please sign in to comment.