Skip to content

Commit

Permalink
Merge pull request #42336 from nkdengineer/fix/41049
Browse files Browse the repository at this point in the history
Clear search input in multiple selection list
  • Loading branch information
MonilBhavsar authored Jun 3, 2024
2 parents bd43fe7 + e9ce166 commit 8777511
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ function BaseSelectionList<TItem extends ListItem>(
isFocused,
});

const clearInputAfterSelect = useCallback(() => {
onChangeText?.('');
}, [onChangeText]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const debouncedOnSelectRow = useCallback(lodashDebounce(onSelectRow, 1000, {leading: true}), [onSelectRow]);

Expand All @@ -287,6 +291,10 @@ function BaseSelectionList<TItem extends ListItem>(
scrollToIndex(Math.max(selectedOptionsCount - 1, 0), true);
}
}

if (shouldShowTextInput) {
clearInputAfterSelect();
}
}

if (shouldDebounceRowSelect) {
Expand Down Expand Up @@ -584,7 +592,7 @@ function BaseSelectionList<TItem extends ListItem>(
[flattenedSections.allOptions, setFocusedIndex, updateAndScrollToFocusedIndex],
);

useImperativeHandle(ref, () => ({scrollAndHighlightItem}), [scrollAndHighlightItem]);
useImperativeHandle(ref, () => ({scrollAndHighlightItem, clearInputAfterSelect}), [scrollAndHighlightItem, clearInputAfterSelect]);

/** Selects row when pressing Enter */
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, shouldDebounceRowSelect ? debouncedSelectFocusedOption : selectFocusedOption, {
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 @@ -430,6 +430,7 @@ type BaseSelectionListProps<TItem extends ListItem> = Partial<ChildrenProps> & {

type SelectionListHandle = {
scrollAndHighlightItem?: (items: string[], timeout: number) => void;
clearInputAfterSelect?: () => void;
};

type ItemLayout = {
Expand Down
8 changes: 6 additions & 2 deletions src/pages/NewChatPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import reject from 'lodash/reject';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
Expand All @@ -11,7 +11,7 @@ import ReferralProgramCTA from '@components/ReferralProgramCTA';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectCircle from '@components/SelectCircle';
import SelectionList from '@components/SelectionList';
import type {ListItem} from '@components/SelectionList/types';
import type {ListItem, SelectionListHandle} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebouncedState from '@hooks/useDebouncedState';
Expand Down Expand Up @@ -128,6 +128,7 @@ function NewChatPage({isGroupChat}: NewChatPageProps) {
const personalData = useCurrentUserPersonalDetails();
const {insets} = useStyledSafeAreaInsets();
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false});
const selectionListRef = useRef<SelectionListHandle>(null);

const {headerMessage, searchTerm, debouncedSearchTerm, setSearchTerm, selectedOptions, setSelectedOptions, recentReports, personalDetails, userToInvite, areOptionsInitialized} =
useOptions({
Expand Down Expand Up @@ -223,6 +224,8 @@ function NewChatPage({isGroupChat}: NewChatPageProps) {
newSelectedOptions = [...selectedOptions, {...option, isSelected: true, selected: true, reportID: option.reportID ?? ''}];
}

selectionListRef?.current?.clearInputAfterSelect?.();

setSelectedOptions(newSelectedOptions);
}

Expand Down Expand Up @@ -301,6 +304,7 @@ function NewChatPage({isGroupChat}: NewChatPageProps) {
keyboardVerticalOffset={variables.contentHeaderHeight + (insets?.top ?? 0) + variables.tabSelectorButtonHeight + variables.tabSelectorButtonPadding}
>
<SelectionList<OptionsListUtils.Option & ListItem>
ref={selectionListRef}
ListItem={UserListItem}
sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY}
textInputValue={searchTerm}
Expand Down

0 comments on commit 8777511

Please sign in to comment.