-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 #36497 from shubham1206agra/fix-currency-ui
Fixed currency pressable ui and padding issues (cherry picked from commit af1026e)
- Loading branch information
1 parent
385550f
commit a38d265
Showing
6 changed files
with
141 additions
and
3 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
83 changes: 83 additions & 0 deletions
83
src/components/TextInputWithCurrencySymbol/BaseTextInputWithCurrencySymbol.tsx
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,83 @@ | ||
import React from 'react'; | ||
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native'; | ||
import AmountTextInput from '@components/AmountTextInput'; | ||
import CurrencySymbolButton from '@components/CurrencySymbolButton'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as CurrencyUtils from '@libs/CurrencyUtils'; | ||
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils'; | ||
import type {BaseTextInputRef} from '@src/components/TextInput/BaseTextInput/types'; | ||
import type TextInputWithCurrencySymbolProps from './types'; | ||
|
||
function BaseTextInputWithCurrencySymbol( | ||
{ | ||
selectedCurrencyCode, | ||
onCurrencyButtonPress = () => {}, | ||
onChangeAmount = () => {}, | ||
formattedAmount, | ||
placeholder, | ||
selection, | ||
onSelectionChange = () => {}, | ||
onKeyPress = () => {}, | ||
isCurrencyPressable = true, | ||
}: TextInputWithCurrencySymbolProps, | ||
ref: React.ForwardedRef<BaseTextInputRef>, | ||
) { | ||
const {fromLocaleDigit} = useLocalize(); | ||
const currencySymbol = CurrencyUtils.getLocalizedCurrencySymbol(selectedCurrencyCode); | ||
const isCurrencySymbolLTR = CurrencyUtils.isCurrencySymbolLTR(selectedCurrencyCode); | ||
const styles = useThemeStyles(); | ||
|
||
const currencySymbolButton = ( | ||
<CurrencySymbolButton | ||
currencySymbol={currencySymbol ?? ''} | ||
onCurrencyButtonPress={onCurrencyButtonPress} | ||
isCurrencyPressable={isCurrencyPressable} | ||
/> | ||
); | ||
|
||
/** | ||
* Set a new amount value properly formatted | ||
* | ||
* @param text - Changed text from user input | ||
*/ | ||
const setFormattedAmount = (text: string) => { | ||
const newAmount = MoneyRequestUtils.addLeadingZero(MoneyRequestUtils.replaceAllDigits(text, fromLocaleDigit)); | ||
onChangeAmount(newAmount); | ||
}; | ||
|
||
const amountTextInput = ( | ||
<AmountTextInput | ||
formattedAmount={formattedAmount} | ||
onChangeAmount={setFormattedAmount} | ||
placeholder={placeholder} | ||
ref={ref} | ||
selection={selection} | ||
onSelectionChange={(event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => { | ||
onSelectionChange(event); | ||
}} | ||
onKeyPress={onKeyPress} | ||
style={styles.pr1} | ||
/> | ||
); | ||
|
||
if (isCurrencySymbolLTR) { | ||
return ( | ||
<> | ||
{currencySymbolButton} | ||
{amountTextInput} | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{amountTextInput} | ||
{currencySymbolButton} | ||
</> | ||
); | ||
} | ||
|
||
BaseTextInputWithCurrencySymbol.displayName = 'BaseTextInputWithCurrencySymbol'; | ||
|
||
export default React.forwardRef(BaseTextInputWithCurrencySymbol); |
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,33 @@ | ||
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native'; | ||
import type {TextSelection} from '@components/Composer/types'; | ||
|
||
type TextInputWithCurrencySymbolProps = { | ||
/** Formatted amount in local currency */ | ||
formattedAmount: string; | ||
|
||
/** Function to call when amount in text input is changed */ | ||
onChangeAmount?: (value: string) => void; | ||
|
||
/** Function to call when currency button is pressed */ | ||
onCurrencyButtonPress?: () => void; | ||
|
||
/** Placeholder value for amount text input */ | ||
placeholder: string; | ||
|
||
/** Currency code of user's selected currency */ | ||
selectedCurrencyCode: string; | ||
|
||
/** Selection Object */ | ||
selection?: TextSelection; | ||
|
||
/** Function to call when selection in text input is changed */ | ||
onSelectionChange?: (event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => void; | ||
|
||
/** Function to call to handle key presses in the text input */ | ||
onKeyPress?: (event: NativeSyntheticEvent<KeyboardEvent>) => void; | ||
|
||
/** Whether the currency symbol is pressable */ | ||
isCurrencyPressable: boolean; | ||
}; | ||
|
||
export default TextInputWithCurrencySymbolProps; |
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