-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 #8299 from mdneyazahmad/fix/7915-localize-currency…
…-symbol [Fix] Localize currency symbol
- Loading branch information
Showing
8 changed files
with
1,113 additions
and
42 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,52 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import TextInput from './TextInput'; | ||
import styles from '../styles/styles'; | ||
import CONST from '../CONST'; | ||
|
||
const propTypes = { | ||
/** Formatted amount in local currency */ | ||
formattedAmount: PropTypes.string.isRequired, | ||
|
||
/** A ref to forward to amount text input */ | ||
forwardedRef: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.shape({current: PropTypes.instanceOf(React.Component)}), | ||
]), | ||
|
||
/** Function to call when amount in text input is changed */ | ||
onChangeAmount: PropTypes.func.isRequired, | ||
|
||
/** Placeholder value for amount text input */ | ||
placeholder: PropTypes.string.isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
forwardedRef: undefined, | ||
}; | ||
|
||
function AmountTextInput(props) { | ||
return ( | ||
<TextInput | ||
disableKeyboard | ||
autoGrow | ||
hideFocusedState | ||
inputStyle={[styles.iouAmountTextInput, styles.p0, styles.noLeftBorderRadius, styles.noRightBorderRadius]} | ||
textInputContainerStyles={[styles.borderNone, styles.noLeftBorderRadius, styles.noRightBorderRadius]} | ||
onChangeText={props.onChangeAmount} | ||
ref={props.forwardedRef} | ||
value={props.formattedAmount} | ||
placeholder={props.placeholder} | ||
keyboardType={CONST.KEYBOARD_TYPE.NUMBER_PAD} | ||
/> | ||
); | ||
} | ||
|
||
AmountTextInput.propTypes = propTypes; | ||
AmountTextInput.defaultProps = defaultProps; | ||
AmountTextInput.displayName = 'AmountTextInput'; | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
<AmountTextInput {...props} forwardedRef={ref} /> | ||
)); |
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 React from 'react'; | ||
import {TouchableOpacity} from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import Text from './Text'; | ||
import styles from '../styles/styles'; | ||
|
||
const propTypes = { | ||
/** Currency symbol of selected currency */ | ||
currencySymbol: PropTypes.string.isRequired, | ||
|
||
/** Function to call when currency button is pressed */ | ||
onCurrencyButtonPress: PropTypes.func.isRequired, | ||
}; | ||
|
||
function CurrencySymbolButton(props) { | ||
return ( | ||
<TouchableOpacity onPress={props.onCurrencyButtonPress}> | ||
<Text style={styles.iouAmountText}>{props.currencySymbol}</Text> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
CurrencySymbolButton.propTypes = propTypes; | ||
CurrencySymbolButton.displayName = 'CurrencySymbolButton'; | ||
|
||
export default CurrencySymbolButton; |
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 PropTypes from 'prop-types'; | ||
import AmountTextInput from './AmountTextInput'; | ||
import CurrencySymbolButton from './CurrencySymbolButton'; | ||
import * as CurrencySymbolUtils from '../libs/CurrencySymbolUtils'; | ||
|
||
const propTypes = { | ||
/** A ref to forward to amount text input */ | ||
forwardedRef: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.shape({current: PropTypes.instanceOf(React.Component)}), | ||
]), | ||
|
||
/** Formatted amount in local currency */ | ||
formattedAmount: PropTypes.string.isRequired, | ||
|
||
/** Function to call when amount in text input is changed */ | ||
onChangeAmount: PropTypes.func, | ||
|
||
/** Function to call when currency button is pressed */ | ||
onCurrencyButtonPress: PropTypes.func, | ||
|
||
/** Placeholder value for amount text input */ | ||
placeholder: PropTypes.string.isRequired, | ||
|
||
/** Preferred locale of the user */ | ||
preferredLocale: PropTypes.string.isRequired, | ||
|
||
/** Currency code of user's selected currency */ | ||
selectedCurrencyCode: PropTypes.string.isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
forwardedRef: undefined, | ||
onChangeAmount: () => {}, | ||
onCurrencyButtonPress: () => {}, | ||
}; | ||
|
||
function TextInputWithCurrencySymbol(props) { | ||
const currencySymbol = CurrencySymbolUtils.getLocalizedCurrencySymbol(props.preferredLocale, props.selectedCurrencyCode); | ||
const isCurrencySymbolLTR = CurrencySymbolUtils.isCurrencySymbolLTR(props.preferredLocale, props.selectedCurrencyCode); | ||
|
||
const currencySymbolButton = ( | ||
<CurrencySymbolButton | ||
currencySymbol={currencySymbol} | ||
onCurrencyButtonPress={props.onCurrencyButtonPress} | ||
/> | ||
); | ||
|
||
const amountTextInput = ( | ||
<AmountTextInput | ||
formattedAmount={props.formattedAmount} | ||
onChangeAmount={props.onChangeAmount} | ||
placeholder={props.placeholder} | ||
ref={props.forwardedRef} | ||
/> | ||
); | ||
|
||
if (isCurrencySymbolLTR) { | ||
return ( | ||
<> | ||
{currencySymbolButton} | ||
{amountTextInput} | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{amountTextInput} | ||
{currencySymbolButton} | ||
</> | ||
); | ||
} | ||
|
||
TextInputWithCurrencySymbol.propTypes = propTypes; | ||
TextInputWithCurrencySymbol.defaultProps = defaultProps; | ||
TextInputWithCurrencySymbol.displayName = 'TextInputWithCurrencySymbol'; | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
<TextInputWithCurrencySymbol {...props} forwardedRef={ref} /> | ||
)); |
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,37 @@ | ||
import _ from 'underscore'; | ||
import * as NumberFormatUtils from './NumberFormatUtils'; | ||
|
||
/** | ||
* Get localized currency symbol for currency(ISO 4217) Code | ||
* @param {String} preferredLocale | ||
* @param {String} currencyCode | ||
* @returns {String} | ||
*/ | ||
function getLocalizedCurrencySymbol(preferredLocale, currencyCode) { | ||
const parts = NumberFormatUtils.formatToParts(preferredLocale, 0, { | ||
style: 'currency', | ||
currency: currencyCode, | ||
}); | ||
return _.find(parts, part => part.type === 'currency').value; | ||
} | ||
|
||
/** | ||
* Whether the currency symbol is left-to-right. | ||
* @param {String} preferredLocale | ||
* @param {String} currencyCode | ||
* @returns {Boolean} | ||
*/ | ||
function isCurrencySymbolLTR(preferredLocale, currencyCode) { | ||
const parts = NumberFormatUtils.formatToParts(preferredLocale, 0, { | ||
style: 'currency', | ||
currency: currencyCode, | ||
}); | ||
|
||
// Currency is LTR when the first part is of currency type. | ||
return parts[0].type === 'currency'; | ||
} | ||
|
||
export { | ||
getLocalizedCurrencySymbol, | ||
isCurrencySymbolLTR, | ||
}; |
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,38 @@ | ||
import _ from 'underscore'; | ||
import * as CurrencySymbolUtils from '../../src/libs/CurrencySymbolUtils'; | ||
|
||
// This file can get outdated. In that case, you can follow these steps to update it: | ||
// - in src/libs/API.js | ||
// - call: GetCurrencyList().then(data => console.log(data.currencyList)); | ||
// - copy the json from console and format it to valid json using some external tool | ||
// - update currencyList.json | ||
import currencyList from './currencyList.json'; | ||
|
||
const currencyCodeList = _.keys(currencyList); | ||
const AVAILABLE_LOCALES = ['en', 'es']; | ||
|
||
// Contains item [isLeft, locale, currencyCode] | ||
const symbolPositions = [ | ||
[true, 'en', 'USD'], | ||
[false, 'es', 'USD'], | ||
]; | ||
|
||
describe('CurrencySymbolUtils', () => { | ||
describe('getLocalizedCurrencySymbol', () => { | ||
test.each(AVAILABLE_LOCALES)('Returns non empty string for all currencyCode with preferredLocale %s', (prefrredLocale) => { | ||
_.forEach(currencyCodeList, (currencyCode) => { | ||
const localizedSymbol = CurrencySymbolUtils.getLocalizedCurrencySymbol(prefrredLocale, currencyCode); | ||
|
||
expect(localizedSymbol).toBeTruthy(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('isCurrencySymbolLTR', () => { | ||
test.each(symbolPositions)('Returns %s for preferredLocale %s and currencyCode %s', (isLeft, locale, currencyCode) => { | ||
const isSymbolLeft = CurrencySymbolUtils.isCurrencySymbolLTR(locale, currencyCode); | ||
expect(isSymbolLeft).toBe(isLeft); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.