-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Create amount filter #47343
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
Create amount filter #47343
Changes from all commits
449f22a
afa7e1c
4885ee5
1ca1423
3ceeb4a
e231cc8
8cdfe96
d568ed6
7adcd4b
eed752a
a7cbfe5
070731e
52ab72e
c205d92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); |
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]; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
const greaterThanFormattedAmount = greaterThan ? convertToFrontendAmountAsString(Number(greaterThan)) : undefined; | ||
const lessThan = searchAdvancedFiltersForm?.[INPUT_IDS.LESS_THAN]; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
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')} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor issue |
||
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 comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.