From 4c2f2e1918d6f9199a7692bd38cda0244095e3a8 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 17 May 2024 14:06:43 +0700 Subject: [PATCH] Clear search input in multiple selection list --- src/components/SelectionList/BaseSelectionList.tsx | 10 +++++++++- src/components/SelectionList/types.ts | 1 + src/pages/NewChatPage.tsx | 8 ++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 77b296740f2c..2512d845be02 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -240,6 +240,10 @@ function BaseSelectionList( isFocused, }); + const clearInputAfterSelect = useCallback(() => { + onChangeText?.(''); + }, [onChangeText]); + /** * Logic to run when a row is selected, either with click/press or keyboard hotkeys. * @@ -259,6 +263,10 @@ function BaseSelectionList( scrollToIndex(Math.max(selectedOptionsCount - 1, 0), true); } } + + if (shouldShowTextInput) { + clearInputAfterSelect(); + } } onSelectRow(item); @@ -486,7 +494,7 @@ function BaseSelectionList( [flattenedSections.allOptions, setFocusedIndex, updateAndScrollToFocusedIndex], ); - useImperativeHandle(ref, () => ({scrollAndHighlightItem}), [scrollAndHighlightItem]); + useImperativeHandle(ref, () => ({scrollAndHighlightItem, clearInputAfterSelect}), [scrollAndHighlightItem, clearInputAfterSelect]); /** Selects row when pressing Enter */ useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, { diff --git a/src/components/SelectionList/types.ts b/src/components/SelectionList/types.ts index 0a4b0532b581..2059442661d8 100644 --- a/src/components/SelectionList/types.ts +++ b/src/components/SelectionList/types.ts @@ -380,6 +380,7 @@ type BaseSelectionListProps = Partial & { type SelectionListHandle = { scrollAndHighlightItem?: (items: string[], timeout: number) => void; + clearInputAfterSelect?: () => void; }; type ItemLayout = { diff --git a/src/pages/NewChatPage.tsx b/src/pages/NewChatPage.tsx index 555a7c855d42..c8681c5a1dc2 100755 --- a/src/pages/NewChatPage.tsx +++ b/src/pages/NewChatPage.tsx @@ -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'; @@ -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'; @@ -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(null); const {headerMessage, searchTerm, debouncedSearchTerm, setSearchTerm, selectedOptions, setSelectedOptions, recentReports, personalDetails, userToInvite, areOptionsInitialized} = useOptions({ @@ -223,6 +224,8 @@ function NewChatPage({isGroupChat}: NewChatPageProps) { newSelectedOptions = [...selectedOptions, {...option, isSelected: true, selected: true, reportID: option.reportID ?? ''}]; } + selectionListRef?.current?.clearInputAfterSelect?.(); + setSelectedOptions(newSelectedOptions); } @@ -301,6 +304,7 @@ function NewChatPage({isGroupChat}: NewChatPageProps) { keyboardVerticalOffset={variables.contentHeaderHeight + (insets?.top ?? 0) + variables.tabSelectorButtonHeight + variables.tabSelectorButtonPadding} > + ref={selectionListRef} ListItem={UserListItem} sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY} textInputValue={searchTerm}