forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
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 Expensify#25815 from akinwale/task-25578
fix: handle forward-delete properly in the amount text input
- Loading branch information
Showing
7 changed files
with
206 additions
and
120 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 was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
src/components/TextInputWithCurrencySymbol/BaseTextInputWithCurrencySymbol.js
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,72 @@ | ||
import React from 'react'; | ||
import AmountTextInput from '../AmountTextInput'; | ||
import CurrencySymbolButton from '../CurrencySymbolButton'; | ||
import * as CurrencyUtils from '../../libs/CurrencyUtils'; | ||
import useLocalize from '../../hooks/useLocalize'; | ||
import * as MoneyRequestUtils from '../../libs/MoneyRequestUtils'; | ||
import * as textInputWithCurrencySymbolPropTypes from './textInputWithCurrencySymbolPropTypes'; | ||
|
||
function BaseTextInputWithCurrencySymbol(props) { | ||
const {fromLocaleDigit} = useLocalize(); | ||
const currencySymbol = CurrencyUtils.getLocalizedCurrencySymbol(props.selectedCurrencyCode); | ||
const isCurrencySymbolLTR = CurrencyUtils.isCurrencySymbolLTR(props.selectedCurrencyCode); | ||
|
||
const currencySymbolButton = ( | ||
<CurrencySymbolButton | ||
currencySymbol={currencySymbol} | ||
onCurrencyButtonPress={props.onCurrencyButtonPress} | ||
/> | ||
); | ||
|
||
/** | ||
* Set a new amount value properly formatted | ||
* | ||
* @param {String} text - Changed text from user input | ||
*/ | ||
const setFormattedAmount = (text) => { | ||
const newAmount = MoneyRequestUtils.addLeadingZero(MoneyRequestUtils.replaceAllDigits(text, fromLocaleDigit)); | ||
props.onChangeAmount(newAmount); | ||
}; | ||
|
||
const amountTextInput = ( | ||
<AmountTextInput | ||
formattedAmount={props.formattedAmount} | ||
onChangeAmount={setFormattedAmount} | ||
placeholder={props.placeholder} | ||
ref={props.forwardedRef} | ||
selection={props.selection} | ||
onSelectionChange={(e) => { | ||
props.onSelectionChange(e); | ||
}} | ||
onKeyPress={props.onKeyPress} | ||
/> | ||
); | ||
|
||
if (isCurrencySymbolLTR) { | ||
return ( | ||
<> | ||
{currencySymbolButton} | ||
{amountTextInput} | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{amountTextInput} | ||
{currencySymbolButton} | ||
</> | ||
); | ||
} | ||
|
||
BaseTextInputWithCurrencySymbol.propTypes = textInputWithCurrencySymbolPropTypes.propTypes; | ||
BaseTextInputWithCurrencySymbol.defaultProps = textInputWithCurrencySymbolPropTypes.defaultProps; | ||
BaseTextInputWithCurrencySymbol.displayName = 'BaseTextInputWithCurrencySymbol'; | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
<BaseTextInputWithCurrencySymbol | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
forwardedRef={ref} | ||
/> | ||
)); |
37 changes: 37 additions & 0 deletions
37
src/components/TextInputWithCurrencySymbol/index.android.js
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 React, {useState, useEffect} from 'react'; | ||
import BaseTextInputWithCurrencySymbol from './BaseTextInputWithCurrencySymbol'; | ||
import * as textInputWithCurrencySymbolPropTypes from './textInputWithCurrencySymbolPropTypes'; | ||
|
||
function TextInputWithCurrencySymbol(props) { | ||
const [skipNextSelectionChange, setSkipNextSelectionChange] = useState(false); | ||
|
||
useEffect(() => { | ||
setSkipNextSelectionChange(true); | ||
}, [props.formattedAmount]); | ||
|
||
return ( | ||
<BaseTextInputWithCurrencySymbol | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
onSelectionChange={(e) => { | ||
if (skipNextSelectionChange) { | ||
setSkipNextSelectionChange(false); | ||
return; | ||
} | ||
props.onSelectionChange(e); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
TextInputWithCurrencySymbol.propTypes = textInputWithCurrencySymbolPropTypes.propTypes; | ||
TextInputWithCurrencySymbol.defaultProps = textInputWithCurrencySymbolPropTypes.defaultProps; | ||
TextInputWithCurrencySymbol.displayName = 'TextInputWithCurrencySymbol'; | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
<TextInputWithCurrencySymbol | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...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,24 @@ | ||
import React from 'react'; | ||
import BaseTextInputWithCurrencySymbol from './BaseTextInputWithCurrencySymbol'; | ||
import * as textInputWithCurrencySymbolPropTypes from './textInputWithCurrencySymbolPropTypes'; | ||
|
||
function TextInputWithCurrencySymbol(props) { | ||
return ( | ||
<BaseTextInputWithCurrencySymbol | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
TextInputWithCurrencySymbol.propTypes = textInputWithCurrencySymbolPropTypes.propTypes; | ||
TextInputWithCurrencySymbol.defaultProps = textInputWithCurrencySymbolPropTypes.defaultProps; | ||
TextInputWithCurrencySymbol.displayName = 'TextInputWithCurrencySymbol'; | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
<TextInputWithCurrencySymbol | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
forwardedRef={ref} | ||
/> | ||
)); |
45 changes: 45 additions & 0 deletions
45
src/components/TextInputWithCurrencySymbol/textInputWithCurrencySymbolPropTypes.js
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,45 @@ | ||
import PropTypes from 'prop-types'; | ||
import refPropTypes from '../refPropTypes'; | ||
|
||
const propTypes = { | ||
/** A ref to forward to amount text input */ | ||
forwardedRef: refPropTypes, | ||
|
||
/** 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, | ||
|
||
/** Currency code of user's selected currency */ | ||
selectedCurrencyCode: PropTypes.string.isRequired, | ||
|
||
/** Selection Object */ | ||
selection: PropTypes.shape({ | ||
start: PropTypes.number, | ||
end: PropTypes.number, | ||
}), | ||
|
||
/** Function to call when selection in text input is changed */ | ||
onSelectionChange: PropTypes.func, | ||
|
||
/** Function to call to handle key presses in the text input */ | ||
onKeyPress: PropTypes.func, | ||
}; | ||
|
||
const defaultProps = { | ||
forwardedRef: undefined, | ||
onChangeAmount: () => {}, | ||
onCurrencyButtonPress: () => {}, | ||
selection: undefined, | ||
onSelectionChange: () => {}, | ||
onKeyPress: () => {}, | ||
}; | ||
|
||
export {propTypes, defaultProps}; |
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