-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30889 from tsa321/curr-slist
- Loading branch information
Showing
4 changed files
with
110 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
import Str from 'expensify-common/lib/str'; | ||
import React, {useMemo, useState} from 'react'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import SelectionList from '@components/SelectionList'; | ||
import RadioListItem from '@components/SelectionList/RadioListItem'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import * as CurrencyUtils from '@libs/CurrencyUtils'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {CurrencyListItem, CurrencySelectionListOnyxProps, CurrencySelectionListProps} from './types'; | ||
|
||
function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, onSelect, currencyList}: CurrencySelectionListProps) { | ||
const [searchValue, setSearchValue] = useState(''); | ||
const {translate} = useLocalize(); | ||
|
||
const {sections, headerMessage} = useMemo(() => { | ||
const currencyOptions: CurrencyListItem[] = Object.entries(currencyList ?? {}).map(([currencyCode, currencyInfo]) => { | ||
const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; | ||
return { | ||
currencyName: currencyInfo?.name ?? '', | ||
text: `${currencyCode} - ${CurrencyUtils.getCurrencySymbol(currencyCode)}`, | ||
currencyCode, | ||
keyForList: currencyCode, | ||
isSelected: isSelectedCurrency, | ||
}; | ||
}); | ||
|
||
const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim()), 'i'); | ||
const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName)); | ||
const isEmpty = searchValue.trim() && !filteredCurrencies.length; | ||
|
||
return { | ||
sections: isEmpty | ||
? [] | ||
: [ | ||
{ | ||
data: filteredCurrencies, | ||
}, | ||
], | ||
headerMessage: isEmpty ? translate('common.noResultsFound') : '', | ||
}; | ||
}, [currencyList, searchValue, translate, initiallySelectedCurrencyCode]); | ||
|
||
return ( | ||
<SelectionList | ||
sections={sections} | ||
ListItem={RadioListItem} | ||
textInputLabel={searchInputLabel} | ||
textInputValue={searchValue} | ||
onChangeText={setSearchValue} | ||
onSelectRow={onSelect} | ||
headerMessage={headerMessage} | ||
initiallyFocusedOptionKey={initiallySelectedCurrencyCode} | ||
showScrollIndicator | ||
/> | ||
); | ||
} | ||
|
||
CurrencySelectionList.displayName = 'CurrencySelectionList'; | ||
|
||
const CurrencySelectionListWithOnyx = withOnyx<CurrencySelectionListProps, CurrencySelectionListOnyxProps>({ | ||
currencyList: {key: ONYXKEYS.CURRENCY_LIST}, | ||
})(CurrencySelectionList); | ||
|
||
export default CurrencySelectionListWithOnyx; |
This file contains 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,26 @@ | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import type {ListItem} from '@components/SelectionList/types'; | ||
import type {CurrencyList} from '@src/types/onyx'; | ||
|
||
type CurrencyListItem = ListItem & { | ||
currencyName: string; | ||
currencyCode: string; | ||
}; | ||
|
||
type CurrencySelectionListOnyxProps = { | ||
/** List of available currencies */ | ||
currencyList: OnyxEntry<CurrencyList>; | ||
}; | ||
|
||
type CurrencySelectionListProps = CurrencySelectionListOnyxProps & { | ||
/** Label for the search text input */ | ||
searchInputLabel: string; | ||
|
||
/** Currency item to be selected initially */ | ||
initiallySelectedCurrencyCode?: string; | ||
|
||
/** Callback to fire when a currency is selected */ | ||
onSelect: (item: CurrencyListItem) => void; | ||
}; | ||
|
||
export type {CurrencyListItem, CurrencySelectionListProps, CurrencySelectionListOnyxProps}; |
This file contains 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 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