Skip to content
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
21 changes: 19 additions & 2 deletions src/components/CurrencySelectionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import React, {useState} from 'react';

import type {CurrencyListItem, CurrencySelectionListProps} from './types';

const ANY_CURRENCY_OPTION_KEY = 'any-currency';

function CurrencySelectionList({
searchInputLabel,
initiallySelectedCurrencyCode,
Expand All @@ -23,12 +25,15 @@ function CurrencySelectionList({
selectedCurrencies = getEmptyArray<string>(),
recentlyUsedCurrencies,
excludedCurrencies = [],
shouldAllowEmptySelection = false,
...restProps
}: CurrencySelectionListProps) {
const {currencyList} = useCurrencyListState();
const {getCurrencySymbol} = useCurrencyListActions();
const [searchValue, setSearchValue] = useState('');
const selectedCurrencyCodes = [initiallySelectedCurrencyCode, ...selectedCurrencies].filter(Boolean);
const selectedCurrencyCodes = [initiallySelectedCurrencyCode, ...selectedCurrencies].filter(
(currencyCode) => currencyCode !== undefined && (currencyCode !== '' || shouldAllowEmptySelection),
);
const initiallyPinnedCurrencyCodes = useInitialSelection(selectedCurrencyCodes, {resetOnFocus: true});
const {translate} = useLocalize();
const initiallyPinnedCurrencyCodeSet = new Set(initiallyPinnedCurrencyCodes);
Expand Down Expand Up @@ -67,6 +72,17 @@ function CurrencySelectionList({
.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName))
.sort((currency1, currency2) => getMatchScore(currency2.text ?? '', searchValue) - getMatchScore(currency1.text ?? '', searchValue));

const anyCurrencyLabel = translate('common.anyCurrency');
if (shouldAllowEmptySelection && searchRegex.test(anyCurrencyLabel)) {
filteredCurrencies.unshift({
currencyName: anyCurrencyLabel,
text: anyCurrencyLabel,
currencyCode: '',
keyForList: ANY_CURRENCY_OPTION_KEY,
isSelected: initiallySelectedCurrencyCode === '',
});
}

const isEmpty = searchValue.trim() && !filteredCurrencies.length;
const shouldDisplayRecentlyOptions = !isEmptyObject(recentlyUsedCurrencyOptions) && !searchValue;
const initiallyPinnedOptions = filteredCurrencies.filter((option) => initiallyPinnedCurrencyCodeSet.has(option.currencyCode));
Expand Down Expand Up @@ -95,6 +111,7 @@ function CurrencySelectionList({
}
} else if (!isEmpty) {
sections.push({
title: shouldAllowEmptySelection ? translate('common.all') : undefined,
data: shouldDisplayInitiallyPinnedOptionsOnTop ? unpinnedOptions : filteredCurrencies,
sectionIndex: 3,
});
Expand All @@ -116,7 +133,7 @@ function CurrencySelectionList({
shouldShowTextInput={!!searchInputLabel}
shouldSingleExecuteRowSelect
shouldUpdateFocusedIndex
initiallyFocusedItemKey={initiallySelectedCurrencyCode}
initiallyFocusedItemKey={shouldAllowEmptySelection && initiallySelectedCurrencyCode === '' ? ANY_CURRENCY_OPTION_KEY : initiallySelectedCurrencyCode}
shouldShowLoadingPlaceholder={!didScreenTransitionEnd}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/CurrencySelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type CurrencySelectionListProps = Partial<SelectionListWithSectionsProps<Currenc
/** List of excluded currency codes */
excludedCurrencies?: string[];

/** Whether Any currency is offered at the top of the All section. */
shouldAllowEmptySelection?: boolean;

didScreenTransitionEnd?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import AmountWithoutCurrencyInput from '@components/AmountWithoutCurrencyInput';
import Button from '@components/Button';
import ScrollView from '@components/ScrollView';
import AmountFilterInput from '@components/Search/FilterComponents/AmountFilterInput';
import type {SearchAmountFilterKeys, SearchAmountValues} from '@components/Search/types';
import SingleSelectListItem from '@components/SelectionList/ListItem/SingleSelectListItem';
import type {ListItem} from '@components/SelectionList/types';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';

import useAmountFilterCurrency from '@hooks/useAmountFilterCurrency';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useScreenWrapperTransitionStatus from '@hooks/useScreenWrapperTransitionStatus';
import useShouldFooterBeInsideList from '@hooks/useShouldFooterBeInsideList';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';

import {convertToBackendAmount, convertToFrontendAmountAsString} from '@libs/CurrencyUtils';
import {formatAmountFilterValue, getAmountFilterInputValue} from '@libs/SearchAmountFilterUtils';
import {FILTER_VIEW_MAP} from '@libs/SearchUIUtils';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -50,24 +52,18 @@ type AmountInputProps = {
autoFocus?: boolean;
};

function getBackendAmount(amount: string) {
return amount ? convertToBackendAmount(Number(amount)).toString() : '';
}

function getFrontendAmount(amount: string | undefined) {
return amount ? convertToFrontendAmountAsString(Number(amount), CONST.DEFAULT_CURRENCY_DECIMALS) : '';
}

function AmountInput({ref, baseFilterKey, modifier, value, label, autoFocus}: AmountInputProps) {
const styles = useThemeStyles();
const [amount, setAmount] = useState(value);
const initialValue = getAmountFilterInputValue(value);
const [amount, setAmount] = useState(initialValue.amount);
const {currency, setCurrency} = useAmountFilterCurrency(baseFilterKey, value);
const {didScreenTransitionEnd} = useScreenWrapperTransitionStatus();
const inputRef = useRef<BaseTextInputRef>(null);

useImperativeHandle(ref, () => ({
getValue: () => {
const key = `${baseFilterKey}${modifier}`;
return {[key]: getBackendAmount(amount)};
return {[key]: formatAmountFilterValue(amount, currency)};
},
}));

Expand All @@ -79,16 +75,15 @@ function AmountInput({ref, baseFilterKey, modifier, value, label, autoFocus}: Am
}, [didScreenTransitionEnd, autoFocus]);

return (
<AmountWithoutCurrencyInput
<AmountFilterInput
ref={inputRef}
baseFilterKey={baseFilterKey}
containerStyles={[styles.ph4, styles.mv2]}
defaultValue={value}
onInputChange={setAmount}
amount={amount}
currency={currency}
onAmountChange={setAmount}
onCurrencyChange={setCurrency}
label={label}
accessibilityLabel={label}
role={CONST.ROLE.PRESENTATION}
inputMode={CONST.INPUT_MODE.DECIMAL}
Comment thread
luacmartins marked this conversation as resolved.
Resolved
shouldAllowNegative
/>
);
}
Expand All @@ -103,17 +98,25 @@ type AmountBetweenInputProps = {

function AmountBetweenInput({ref, baseFilterKey, greaterThanValue, lessThanValue, autoFocus}: AmountBetweenInputProps) {
const {translate} = useLocalize();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const styles = useThemeStyles();
const [greaterThanAmount, setGreaterThanAmount] = useState(greaterThanValue);
const [lessThanAmount, setLessThanAmount] = useState(lessThanValue);
const initialGreaterThan = getAmountFilterInputValue(greaterThanValue);
const initialLessThan = getAmountFilterInputValue(lessThanValue);
const [greaterThanAmount, setGreaterThanAmount] = useState(initialGreaterThan.amount);
const [lessThanAmount, setLessThanAmount] = useState(initialLessThan.amount);
const {currency: greaterThanCurrency, setCurrency: setGreaterThanCurrency} = useAmountFilterCurrency(baseFilterKey, greaterThanValue);
const {currency: lessThanCurrency, setCurrency: setLessThanCurrency} = useAmountFilterCurrency(baseFilterKey, lessThanValue);
const {didScreenTransitionEnd} = useScreenWrapperTransitionStatus();
const inputRef = useRef<BaseTextInputRef>(null);

useImperativeHandle(ref, () => ({
getValue: () => {
const greaterThanKey = `${baseFilterKey}${CONST.SEARCH.AMOUNT_MODIFIERS.GREATER_THAN}`;
const lessThanKey = `${baseFilterKey}${CONST.SEARCH.AMOUNT_MODIFIERS.LESS_THAN}`;
return {[greaterThanKey]: getBackendAmount(greaterThanAmount), [lessThanKey]: getBackendAmount(lessThanAmount)};
return {
[greaterThanKey]: formatAmountFilterValue(greaterThanAmount, greaterThanCurrency),
[lessThanKey]: formatAmountFilterValue(lessThanAmount, lessThanCurrency),
};
},
}));

Expand All @@ -128,27 +131,25 @@ function AmountBetweenInput({ref, baseFilterKey, greaterThanValue, lessThanValue
const lessThanLabel = translate('search.filters.amount.lessThan');

return (
<View style={[styles.flexRow, styles.gap1, styles.ph4, styles.mv2]}>
<AmountWithoutCurrencyInput
<View style={[shouldUseNarrowLayout ? styles.flexColumn : styles.flexRow, styles.gap1, styles.ph4, styles.mv2]}>
<AmountFilterInput
ref={inputRef}
baseFilterKey={baseFilterKey}
containerStyles={styles.flex1}
defaultValue={greaterThanValue}
onInputChange={setGreaterThanAmount}
amount={greaterThanAmount}
currency={greaterThanCurrency}
onAmountChange={setGreaterThanAmount}
onCurrencyChange={setGreaterThanCurrency}
label={greaterThanLabel}
accessibilityLabel={greaterThanLabel}
role={CONST.ROLE.PRESENTATION}
inputMode={CONST.INPUT_MODE.DECIMAL}
shouldAllowNegative
/>
<AmountWithoutCurrencyInput
<AmountFilterInput
baseFilterKey={baseFilterKey}
containerStyles={styles.flex1}
defaultValue={lessThanValue}
onInputChange={setLessThanAmount}
amount={lessThanAmount}
currency={lessThanCurrency}
onAmountChange={setLessThanAmount}
onCurrencyChange={setLessThanCurrency}
label={lessThanLabel}
accessibilityLabel={lessThanLabel}
role={CONST.ROLE.PRESENTATION}
inputMode={CONST.INPUT_MODE.DECIMAL}
shouldAllowNegative
/>
</View>
);
Expand Down Expand Up @@ -241,16 +242,16 @@ function AmountFilterContent({baseFilterKey, value, autoFocus, buttonSize, style
<AmountBetweenInput
ref={inputRef}
baseFilterKey={baseFilterKey}
greaterThanValue={getFrontendAmount(value?.[CONST.SEARCH.AMOUNT_MODIFIERS.GREATER_THAN])}
lessThanValue={getFrontendAmount(value?.[CONST.SEARCH.AMOUNT_MODIFIERS.LESS_THAN])}
greaterThanValue={value?.[CONST.SEARCH.AMOUNT_MODIFIERS.GREATER_THAN] ?? ''}
lessThanValue={value?.[CONST.SEARCH.AMOUNT_MODIFIERS.LESS_THAN] ?? ''}
autoFocus={autoFocus}
/>
) : (
<AmountInput
ref={inputRef}
baseFilterKey={baseFilterKey}
modifier={config.keyForList}
value={getFrontendAmount(value?.[config.keyForList])}
value={value?.[config.keyForList] ?? ''}
label={label}
autoFocus={autoFocus}
/>
Expand Down
102 changes: 102 additions & 0 deletions src/components/Search/FilterComponents/AmountFilterInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Reuses money-request amount entry and currency selection without requiring a filter currency.
import AmountWithoutCurrencyInput from '@components/AmountWithoutCurrencyInput';
import NumberWithSymbolForm from '@components/NumberWithSymbolForm';
import type {SearchAmountFilterKeys} from '@components/Search/types';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';

import useLocalize from '@hooks/useLocalize';

import {getLocalizedCurrencySymbol} from '@libs/CurrencyUtils';
import getAmountFilterInputKeyboard from '@libs/getAmountFilterInputKeyboard';
import {supportsAmountFilterCurrency} from '@libs/SearchAmountFilterUtils';

import IOURequestStepCurrencyModal from '@pages/iou/request/step/IOURequestStepCurrencyModal';

import CONST from '@src/CONST';

import type {ForwardedRef} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';

import React, {useState} from 'react';
import {View} from 'react-native';

type AmountFilterInputProps = {
ref?: ForwardedRef<BaseTextInputRef>;
baseFilterKey: SearchAmountFilterKeys;

/** Amount in major units, regardless of the saved operand's units. */
amount: string;

/** Empty or omitted means Any currency instead of a currency comparison. */
currency?: string;
label: string;
name?: string;
autoFocus?: boolean;
containerStyles?: StyleProp<ViewStyle>;
onAmountChange: (amount: string) => void;
onCurrencyChange: (currency: string) => void;
};

function AmountFilterInput({ref, baseFilterKey, amount, currency, label, name, autoFocus, containerStyles, onAmountChange, onCurrencyChange}: AmountFilterInputProps) {
const {translate, preferredLocale} = useLocalize();
const [isCurrencyPickerVisible, setIsCurrencyPickerVisible] = useState(false);
const [initialAmount] = useState(amount);

if (!supportsAmountFilterCurrency(baseFilterKey)) {
return (
<AmountWithoutCurrencyInput
ref={ref}
containerStyles={containerStyles}
defaultValue={initialAmount}
onInputChange={onAmountChange}
label={label}
accessibilityLabel={label}
name={name}
role={CONST.ROLE.PRESENTATION}
inputMode={CONST.INPUT_MODE.DECIMAL}
shouldAllowNegative
autoFocus={autoFocus}
/>
);
}

const hideCurrencyPicker = () => setIsCurrencyPickerVisible(false);
const {keyboardType, shouldShowFlipButton} = getAmountFilterInputKeyboard();

return (
<View style={containerStyles}>
<NumberWithSymbolForm
ref={ref}
displayAsTextInput
value={amount}
currency={currency}
symbol={currency ? (getLocalizedCurrencySymbol(preferredLocale, currency) ?? '') : ''}
hideSymbol={!currency}
decimals={CONST.DEFAULT_CURRENCY_DECIMALS}
label={label}
testID={name}
onInputChange={onAmountChange}
allowNegativeInput
keyboardType={keyboardType}
shouldShowFlipButton={shouldShowFlipButton}
shouldShowCurrencyButton
currencyButtonLabel={currency || translate('common.any')}
currencyButtonAccessibilityLabel={[label.trim(), translate('common.selectCurrency'), currency || translate('common.anyCurrency')].join(', ')}
onCurrencyButtonPress={() => setIsCurrencyPickerVisible(true)}
shouldShowBigNumberPad={false}
shouldUseBorderlessButtons
autoFocus={autoFocus}
/>
<IOURequestStepCurrencyModal
isPickerVisible={isCurrencyPickerVisible}
hidePickerModal={hideCurrencyPicker}
headerText={translate('common.selectCurrency')}
value={currency}
onInputChange={onCurrencyChange}
shouldAllowEmptySelection
/>
Comment thread
luacmartins marked this conversation as resolved.
Resolved
</View>
);
}

export default AmountFilterInput;
Loading
Loading