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

Fix selection list focus #34196

Merged
merged 21 commits into from
Feb 15, 2024
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d733aa2
fix selection list focus
abzokhattab Jan 9, 2024
2a50e43
Using memo on the sorted sections
abzokhattab Jan 12, 2024
3920a4f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 12, 2024
0e2f487
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 14, 2024
10f06b7
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 17, 2024
f702796
Convert changes to typescript
abzokhattab Jan 17, 2024
d53c12f
Minor
abzokhattab Jan 17, 2024
8ab6576
migrating to ts
abzokhattab Jan 17, 2024
216793f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 20, 2024
54cdcc3
Merge branch 'main' into fix-base-selection-list-focus
abzokhattab Jan 25, 2024
6d9a69f
fix arrow selection
abzokhattab Jan 26, 2024
a123043
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Jan 26, 2024
17c2f7e
Update src/components/SelectionList/BaseSelectionList.tsx
abzokhattab Jan 27, 2024
a015eb5
comments changes
abzokhattab Jan 27, 2024
b8f58a2
minor
abzokhattab Jan 27, 2024
b43f5c4
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Feb 5, 2024
322d554
Removing the sortedsections logic
abzokhattab Feb 5, 2024
ec03b9a
Update src/components/SelectionList/BaseSelectionList.tsx
abzokhattab Feb 6, 2024
86d242d
Adjusting useEffect dependencies
abzokhattab Feb 6, 2024
3dab35f
Merge remote-tracking branch 'origin/main' into fix-base-selection-li…
abzokhattab Feb 12, 2024
f4393b1
minor edit
abzokhattab Feb 14, 2024
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
19 changes: 10 additions & 9 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,18 @@ 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 !== '' ? flattenedSections.disabledOptionsIndexes.length : -1;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved

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

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