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 input on screen focus #26415

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion src/components/CountryPicker/CountrySelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ function CountrySelectorModal({currentCountry, isVisible, onClose, onCountrySele
sections={[{data: searchResults, indexOffset: 0}]}
onSelectRow={onCountrySelected}
onChangeText={setSearchValue}
shouldDelayFocus
initiallyFocusedOptionKey={currentCountry}
/>
</ScreenWrapper>
Expand Down
31 changes: 14 additions & 17 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {useEffect, useMemo, useRef, useState} from 'react';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {useFocusEffect} from '@react-navigation/native';
import SectionList from '../SectionList';
import Text from '../Text';
import styles from '../../styles/styles';
Expand Down Expand Up @@ -42,7 +43,6 @@ function BaseSelectionList({
keyboardType = CONST.KEYBOARD_TYPE.DEFAULT,
onChangeText,
initiallyFocusedOptionKey = '',
shouldDelayFocus = false,
onScroll,
onScrollBeginDrag,
headerMessage = '',
Expand Down Expand Up @@ -266,23 +266,20 @@ function BaseSelectionList({
);
};

/** Focuses the text input when the component mounts. If `props.shouldDelayFocus` is true, we wait for the animation to finish */
useEffect(() => {
if (shouldShowTextInput) {
if (shouldDelayFocus) {
/** Focuses the text input when the component comes into focus and after any navigation animations finish. */
useFocusEffect(
useCallback(() => {
if (shouldShowTextInput) {
focusTimeoutRef.current = setTimeout(() => textInputRef.current.focus(), CONST.ANIMATED_TRANSITION);
thiagobrez marked this conversation as resolved.
Show resolved Hide resolved
} else {
textInputRef.current.focus();
}
}

return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, [shouldDelayFocus, shouldShowTextInput]);
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, [shouldShowTextInput]),
);

/** Selects row when pressing Enter */
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, {
Expand Down
3 changes: 0 additions & 3 deletions src/components/SelectionList/selectionListPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ const propTypes = {
/** Item `keyForList` to focus initially */
initiallyFocusedOptionKey: PropTypes.string,

/** Whether to delay focus on the text input when mounting. Used for a smoother animation on Android */
shouldDelayFocus: PropTypes.bool,

/** Callback to fire when the list is scrolled */
onScroll: PropTypes.func,

Expand Down
1 change: 0 additions & 1 deletion src/components/StatePicker/StateSelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ function StateSelectorModal({currentState, isVisible, onClose, onStateSelected,
sections={[{data: searchResults, indexOffset: 0}]}
onSelectRow={onStateSelected}
onChangeText={setSearchValue}
shouldDelayFocus
initiallyFocusedOptionKey={currentState}
/>
</ScreenWrapper>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Profile/PronounsPage.js
Copy link
Contributor

@huzaifa-99 huzaifa-99 Sep 12, 2023

Choose a reason for hiding this comment

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

Just confirming if this change was intentional? @thiagobrez

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @huzaifa-99, yeah it was intentional. The regression came in due to the fact that I wasn't aware that a pronoun could be un-selected. I already sent a comment in the issue

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you!

Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function PronounsPage({currentUserPersonalDetails}) {
onSelectRow={updatePronouns}
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentPronounsKey}
shouldDelayFocus
/>
/>

</ScreenWrapper>
);
}
Expand Down
1 change: 0 additions & 1 deletion src/pages/settings/Profile/TimezoneSelectPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function TimezoneSelectPage(props) {
onSelectRow={saveSelectedTimezone}
sections={[{data: timezoneOptions, indexOffset: 0, isDisabled: timezone.automatic}]}
initiallyFocusedOptionKey={_.get(_.filter(timezoneOptions, (tz) => tz.text === timezone.selected)[0], 'keyForList')}
shouldDelayFocus
showScrollIndicator
/>
</ScreenWrapper>
Expand Down
1 change: 0 additions & 1 deletion src/pages/workspace/WorkspaceInvitePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ function WorkspaceInvitePage(props) {
onSelectRow={toggleOption}
onConfirm={inviteUser}
showScrollIndicator
shouldDelayFocus
showLoadingPlaceholder={!didScreenTransitionEnd || !OptionsListUtils.isPersonalDetailsReady(props.personalDetails)}
/>
<View style={[styles.flexShrink0]}>
Expand Down
1 change: 0 additions & 1 deletion src/pages/workspace/WorkspaceMembersPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ function WorkspaceMembersPage(props) {
onSelectAll={() => toggleAllUsers(data)}
onDismissError={dismissError}
showLoadingPlaceholder={!OptionsListUtils.isPersonalDetailsReady(props.personalDetails) || _.isEmpty(props.policyMembers)}
shouldDelayFocus
showScrollIndicator
/>
</View>
Expand Down
5 changes: 5 additions & 0 deletions tests/perf-test/SelectionList.perf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jest.mock('../../src/components/withKeyboardState', () => (Component) => (props)
/>
));

jest.mock('@react-navigation/native', () => ({
useFocusEffect: () => {},
createNavigationContainerRef: jest.fn(),
}));

function SelectionListWrapper(args) {
const [selectedIds, setSelectedIds] = useState([]);

Expand Down