-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix: add search bar to lists with items above 15 #60488
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
Merged
+617
−298
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
9fe9f0b
Add search/filter text input to lists with items
daledah ad2f688
Merge branch 'main' into fix/59864
daledah 80ec5da
fix: apply search bar to other pages
daledah 96eea58
fix: lint
daledah 099938c
Merge branch 'main' into fix/59864
daledah 9d9a33a
fix: handle selection for search results
daledah 02fb491
Merge branch 'main' into fix/59864
daledah 3e097a1
fix: change button icon behavior
daledah 0999cf8
fix: lint
daledah f53defa
Merge branch 'main' into fix/59864
daledah 2788e70
feat: add search bar to distance rate and tax page
daledah 9c2d0f1
Merge branch 'main' into fix/59864
daledah f0db512
fix: correct search condition in company card page
daledah 5e36639
Merge branch 'main' into fix/59864
daledah 211e323
Merge branch 'main' into fix/59864
daledah f8c7941
fix: change component styles and placement
daledah 76203ab
fix: lint
daledah dfe5e1f
Merge branch 'main' into fix/59864
daledah d69d154
fix: add search bar to per diem page
daledah 7da3d7d
Merge branch 'main' into fix/59864
daledah c2c3383
feat: add useTransition usage to pages
daledah a310f85
Merge branch 'main' into fix/59864
daledah 3629041
fix: migrate useTransition to a new hook
daledah e6a8d9c
Merge branch 'main' into fix/59864
daledah a39f5c9
refactor: create const, fix delete error
daledah d202a45
Merge branch 'main' into fix/59864
daledah be10c79
Merge branch 'main' into fix/59864
daledah e9f968e
Merge branch 'main' into fix/59864
daledah 1dc7cc2
fix: test
daledah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React from 'react'; | ||
| import type {StyleProp, ViewStyle} from 'react-native'; | ||
| import {View} from 'react-native'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useResponsiveLayout from '@hooks/useResponsiveLayout'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import CONST from '@src/CONST'; | ||
| import type IconAsset from '@src/types/utils/IconAsset'; | ||
| import {MagnifyingGlass} from './Icon/Expensicons'; | ||
| import Text from './Text'; | ||
| import TextInput from './TextInput'; | ||
|
|
||
| type SearchBarProps = { | ||
| label: string; | ||
| icon?: IconAsset; | ||
| inputValue: string; | ||
| onChangeText?: (text: string) => void; | ||
| onSubmitEditing?: (text: string) => void; | ||
| style?: StyleProp<ViewStyle>; | ||
| shouldShowEmptyState?: boolean; | ||
| }; | ||
|
|
||
| function SearchBar({label, style, icon = MagnifyingGlass, inputValue, onChangeText, onSubmitEditing, shouldShowEmptyState}: SearchBarProps) { | ||
| const styles = useThemeStyles(); | ||
| const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
| const {translate} = useLocalize(); | ||
|
|
||
| return ( | ||
| <> | ||
| <View style={[styles.getSearchBarStyle(shouldUseNarrowLayout), style]}> | ||
| <TextInput | ||
| label={label} | ||
| accessibilityLabel={label} | ||
| role={CONST.ROLE.PRESENTATION} | ||
| value={inputValue} | ||
| onChangeText={onChangeText} | ||
| inputMode={CONST.INPUT_MODE.TEXT} | ||
| selectTextOnFocus | ||
| spellCheck={false} | ||
| icon={inputValue?.length ? undefined : icon} | ||
| iconContainerStyle={styles.p0} | ||
| onSubmitEditing={() => onSubmitEditing?.(inputValue)} | ||
| shouldShowClearButton | ||
| shouldHideClearButton={!inputValue?.length} | ||
| /> | ||
| </View> | ||
| {!!shouldShowEmptyState && inputValue.length !== 0 && ( | ||
| <View style={[styles.ph5, styles.pt3, styles.pb5]}> | ||
| <Text style={[styles.textNormal, styles.colorMuted]}>{translate('common.noResultsFoundMatching', {searchString: inputValue})}</Text> | ||
| </View> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| SearchBar.displayName = 'SearchBar'; | ||
| export default SearchBar; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import {useEffect, useState, useTransition} from 'react'; | ||
| import CONST from '@src/CONST'; | ||
| import usePrevious from './usePrevious'; | ||
|
|
||
| /** | ||
| * This hook filters (and optionally sorts) a dataset based on a search parameter. | ||
| * It utilizes `useTransition` to allow the searchQuery to change rapidly, while more expensive renders that occur using | ||
| * the result of the filtering and sorting are deprioritized, allowing them to happen in the background. | ||
| */ | ||
| function useSearchResults<TValue>(data: TValue[], filterData: (datum: TValue, searchInput: string) => boolean, sortData: (data: TValue[]) => TValue[] = (d) => d) { | ||
| const [inputValue, setInputValue] = useState(''); | ||
| const [result, setResult] = useState(data); | ||
| const [, startTransition] = useTransition(); | ||
| const prevData = usePrevious(data); | ||
| useEffect(() => { | ||
| startTransition(() => { | ||
| const normalizedSearchQuery = inputValue.trim().toLowerCase(); | ||
| const filtered = normalizedSearchQuery.length ? data.filter((item) => filterData(item, normalizedSearchQuery)) : data; | ||
| const sorted = sortData(filtered); | ||
| setResult(sorted); | ||
| }); | ||
| }, [data, filterData, inputValue, sortData]); | ||
|
|
||
| useEffect(() => { | ||
| if (prevData.length <= CONST.SEARCH_ITEM_LIMIT || data.length > CONST.SEARCH_ITEM_LIMIT) { | ||
| return; | ||
| } | ||
| setInputValue(''); | ||
| }, [data, prevData]); | ||
|
|
||
| return [inputValue, setInputValue, result] as const; | ||
| } | ||
|
|
||
| export default useSearchResults; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The company cards array was sorted in place, so React didn't re-render because the reference didn't change. See issue #62202 for details. Fixed in PR #65806.