-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #47343 from software-mansion-labs/@szymczak/amount…
…-filter Create amount filter
- Loading branch information
Showing
13 changed files
with
248 additions
and
29 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
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
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,66 @@ | ||
import React, {useCallback, useMemo} from 'react'; | ||
import type {ForwardedRef} from 'react'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import {addLeadingZero, replaceAllDigits, replaceCommasWithPeriod, stripSpacesFromAmount, validateAmount} from '@libs/MoneyRequestUtils'; | ||
import CONST from '@src/CONST'; | ||
import TextInput from './TextInput'; | ||
import type {BaseTextInputProps, BaseTextInputRef} from './TextInput/BaseTextInput/types'; | ||
|
||
type AmountFormProps = { | ||
/** Amount supplied by the FormProvider */ | ||
value?: string; | ||
|
||
/** Callback to update the amount in the FormProvider */ | ||
onInputChange?: (value: string) => void; | ||
} & Partial<BaseTextInputProps>; | ||
|
||
function AmountWithoutCurrencyForm( | ||
{value: amount, onInputChange, inputID, name, defaultValue, accessibilityLabel, role, label, ...rest}: AmountFormProps, | ||
ref: ForwardedRef<BaseTextInputRef>, | ||
) { | ||
const {toLocaleDigit} = useLocalize(); | ||
|
||
const currentAmount = useMemo(() => (typeof amount === 'string' ? amount : ''), [amount]); | ||
|
||
/** | ||
* Sets the selection and the amount accordingly to the value passed to the input | ||
* @param newAmount - Changed amount from user input | ||
*/ | ||
const setNewAmount = useCallback( | ||
(newAmount: string) => { | ||
// Remove spaces from the newAmount value because Safari on iOS adds spaces when pasting a copied value | ||
// More info: https://github.com/Expensify/App/issues/16974 | ||
const newAmountWithoutSpaces = stripSpacesFromAmount(newAmount); | ||
const replacedCommasAmount = replaceCommasWithPeriod(newAmountWithoutSpaces); | ||
const withLeadingZero = addLeadingZero(replacedCommasAmount); | ||
if (!validateAmount(withLeadingZero, 2)) { | ||
return; | ||
} | ||
onInputChange?.(withLeadingZero); | ||
}, | ||
[onInputChange], | ||
); | ||
|
||
const formattedAmount = replaceAllDigits(currentAmount, toLocaleDigit); | ||
|
||
return ( | ||
<TextInput | ||
value={formattedAmount} | ||
onChangeText={setNewAmount} | ||
inputID={inputID} | ||
name={name} | ||
label={label} | ||
defaultValue={defaultValue} | ||
accessibilityLabel={accessibilityLabel} | ||
role={role} | ||
ref={ref} | ||
keyboardType={CONST.KEYBOARD_TYPE.DECIMAL_PAD} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...rest} | ||
/> | ||
); | ||
} | ||
|
||
AmountWithoutCurrencyForm.displayName = 'AmountWithoutCurrencyForm'; | ||
|
||
export default React.forwardRef(AmountWithoutCurrencyForm); |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import AmountWithoutCurrencyForm from '@components/AmountWithoutCurrencyForm'; | ||
import FormProvider from '@components/Form/FormProvider'; | ||
import InputWrapper from '@components/Form/InputWrapper'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import useAutoFocusInput from '@hooks/useAutoFocusInput'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import {updateAdvancedFilters} from '@libs/actions/Search'; | ||
import {convertToBackendAmount, convertToFrontendAmountAsString} from '@libs/CurrencyUtils'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import INPUT_IDS from '@src/types/form/SearchAdvancedFiltersForm'; | ||
|
||
function SearchFiltersAmountPage() { | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
|
||
const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); | ||
const greaterThan = searchAdvancedFiltersForm?.[INPUT_IDS.GREATER_THAN]; | ||
const greaterThanFormattedAmount = greaterThan ? convertToFrontendAmountAsString(Number(greaterThan)) : undefined; | ||
const lessThan = searchAdvancedFiltersForm?.[INPUT_IDS.LESS_THAN]; | ||
const lessThanFormattedAmount = lessThan ? convertToFrontendAmountAsString(Number(lessThan)) : undefined; | ||
const {inputCallbackRef} = useAutoFocusInput(); | ||
|
||
const updateAmountFilter = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM>) => { | ||
const greater = values[INPUT_IDS.GREATER_THAN]; | ||
const greaterThanBackendAmount = greater ? convertToBackendAmount(Number(greater)) : ''; | ||
const less = values[INPUT_IDS.LESS_THAN]; | ||
const lessThanBackendAmount = less ? convertToBackendAmount(Number(less)) : ''; | ||
updateAdvancedFilters({greaterThan: greaterThanBackendAmount?.toString(), lessThan: lessThanBackendAmount?.toString()}); | ||
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); | ||
}; | ||
|
||
return ( | ||
<ScreenWrapper | ||
testID={SearchFiltersAmountPage.displayName} | ||
shouldShowOfflineIndicatorInWideScreen | ||
offlineIndicatorStyle={styles.mtAuto} | ||
includeSafeAreaPaddingBottom={false} | ||
> | ||
<HeaderWithBackButton | ||
title={translate('common.total')} | ||
onBackButtonPress={() => { | ||
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); | ||
}} | ||
/> | ||
<FormProvider | ||
style={[styles.flex1, styles.ph5]} | ||
formID={ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM} | ||
onSubmit={updateAmountFilter} | ||
submitButtonText={translate('common.save')} | ||
enabledWhenOffline | ||
> | ||
<View style={styles.mb5}> | ||
<InputWrapper | ||
InputComponent={AmountWithoutCurrencyForm} | ||
inputID={INPUT_IDS.GREATER_THAN} | ||
name={INPUT_IDS.GREATER_THAN} | ||
defaultValue={greaterThanFormattedAmount} | ||
label={translate('search.filters.amount.greaterThan')} | ||
accessibilityLabel={translate('search.filters.amount.greaterThan')} | ||
role={CONST.ROLE.PRESENTATION} | ||
ref={inputCallbackRef} | ||
/> | ||
</View> | ||
<View style={styles.mb5}> | ||
<InputWrapper | ||
InputComponent={AmountWithoutCurrencyForm} | ||
inputID={INPUT_IDS.LESS_THAN} | ||
name={INPUT_IDS.LESS_THAN} | ||
defaultValue={lessThanFormattedAmount} | ||
label={translate('search.filters.amount.lessThan')} | ||
accessibilityLabel={translate('search.filters.amount.lessThan')} | ||
role={CONST.ROLE.PRESENTATION} | ||
/> | ||
</View> | ||
</FormProvider> | ||
</ScreenWrapper> | ||
); | ||
} | ||
|
||
SearchFiltersAmountPage.displayName = 'SearchFiltersAmountPage'; | ||
|
||
export default SearchFiltersAmountPage; |
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