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 navigate to detail page on enter for highlighted item #54807

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ const CONST = {
SHIFT: {
DEFAULT: 'shift',
},
ENTER: {
DEFAULT: 'enter',
},
},
KEYBOARD_SHORTCUTS: {
SEARCH: {
Expand Down
15 changes: 13 additions & 2 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps)
}
if (reportForContextualSearch.isPolicyExpenseChat) {
roomType = CONST.SEARCH.DATA_TYPES.EXPENSE;
autocompleteID = reportForContextualSearch.policyID ?? '';
if (reportForContextualSearch.policyID) {
autocompleteID = reportForContextualSearch.policyID;
} else {
autocompleteID = '';
}
}

additionalSections.push({
Expand Down Expand Up @@ -283,7 +287,14 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps)
isFullWidth={shouldUseNarrowLayout}
onSearchQueryChange={onSearchQueryChange}
onSubmit={() => {
submitSearch(textInputValue);
const focusedOption = listRef.current?.getFocusedOption();

if (!focusedOption) {
submitSearch(textInputValue);
return;
}

onListItemPress(focusedOption);
}}
caretHidden={shouldHideInputCaret}
routerListRef={listRef}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Search/SearchRouter/SearchRouterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import handleKeyPress from '@libs/SearchInputOnKeyPress';
import shouldDelayFocus from '@libs/shouldDelayFocus';
import variables from '@styles/variables';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -123,6 +124,7 @@ function SearchRouterInput(
}}
isLoading={!!isSearchingForReports}
ref={ref}
onKeyPress={handleKeyPress(onSubmit)}
/>
</View>
{!!rightComponent && <View style={styles.pr3}>{rightComponent}</View>}
Expand Down
15 changes: 13 additions & 2 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,23 @@ function BaseSelectionList<TItem extends ListItem>(
}
};

const selectFocusedOption = () => {
const getFocusedOption = useCallback(() => {
const focusedOption = focusedIndex !== -1 ? flattenedSections.allOptions.at(focusedIndex) : undefined;

if (!focusedOption || (focusedOption.isDisabled && !focusedOption.isSelected)) {
return;
}

return focusedOption;
}, [flattenedSections.allOptions, focusedIndex]);

const selectFocusedOption = () => {
const focusedOption = getFocusedOption();

if (!focusedOption) {
return;
}

selectRow(focusedOption);
};

Expand Down Expand Up @@ -734,12 +744,13 @@ function BaseSelectionList<TItem extends ListItem>(
isTextInputFocusedRef.current = isTextInputFocused;
}, []);

useImperativeHandle(ref, () => ({scrollAndHighlightItem, clearInputAfterSelect, updateAndScrollToFocusedIndex, updateExternalTextInputFocus, scrollToIndex}), [
useImperativeHandle(ref, () => ({scrollAndHighlightItem, clearInputAfterSelect, updateAndScrollToFocusedIndex, updateExternalTextInputFocus, scrollToIndex, getFocusedOption}), [
scrollAndHighlightItem,
clearInputAfterSelect,
updateAndScrollToFocusedIndex,
updateExternalTextInputFocus,
scrollToIndex,
getFocusedOption,
]);

/** Selects row when pressing Enter */
Expand Down
1 change: 1 addition & 0 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ type SelectionListHandle = {
scrollToIndex: (index: number, animated?: boolean) => void;
updateAndScrollToFocusedIndex: (newFocusedIndex: number) => void;
updateExternalTextInputFocus: (isTextInputFocused: boolean) => void;
getFocusedOption: () => ListItem | undefined;
};

type ItemLayout = {
Expand Down
5 changes: 5 additions & 0 deletions src/libs/SearchInputOnKeyPress/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {NativeSyntheticEvent, TextInputKeyPressEventData} from 'react-native';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const handleKeyPress = (onSubmit: () => void) => (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {};
export default handleKeyPress;
16 changes: 16 additions & 0 deletions src/libs/SearchInputOnKeyPress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {NativeSyntheticEvent, TextInputKeyPressEventData} from 'react-native';
import CONST from '@src/CONST';

function handleKeyPress(onSubmit: () => void) {
return (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
const isEnterKey = event.nativeEvent.key.toLowerCase() === CONST.PLATFORM_SPECIFIC_KEYS.ENTER.DEFAULT;

if (!isEnterKey) {
return;
}

onSubmit();
};
}

export default handleKeyPress;
Loading