Skip to content

Commit 61ffca4

Browse files
luacmartinsOSBotify
authored andcommitted
Merge pull request #56340 from margelo/fix/respect-comma-as-separator-for-spanish-locale
[CP Staging] fix: use locale-specific separators (cherry picked from commit b90c4c6) (CP triggered by luacmartins)
1 parent d3b8303 commit 61ffca4

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/components/AmountWithoutCurrencyInput.tsx

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import React from 'react';
1+
import React, {useCallback, useMemo} from 'react';
22
import type {ForwardedRef} from 'react';
3+
import useLocalize from '@hooks/useLocalize';
4+
import {replaceAllDigits, replaceCommasWithPeriod, stripSpacesFromAmount} from '@libs/MoneyRequestUtils';
35
import CONST from '@src/CONST';
46
import TextInput from './TextInput';
57
import type {BaseTextInputProps, BaseTextInputRef} from './TextInput/BaseTextInput/types';
@@ -16,21 +18,46 @@ type AmountFormProps = {
1618
} & Partial<BaseTextInputProps>;
1719

1820
function AmountWithoutCurrencyInput(
19-
{value: amount, shouldAllowNegative = false, inputID, name, defaultValue, accessibilityLabel, role, label, ...rest}: AmountFormProps,
21+
{value: amount, shouldAllowNegative = false, inputID, name, defaultValue, accessibilityLabel, role, label, onInputChange, ...rest}: AmountFormProps,
2022
ref: ForwardedRef<BaseTextInputRef>,
2123
) {
24+
const {toLocaleDigit} = useLocalize();
25+
const separator = useMemo(
26+
() =>
27+
replaceAllDigits('1.1', toLocaleDigit)
28+
.split('')
29+
.filter((char) => char !== '1')
30+
.join(''),
31+
[toLocaleDigit],
32+
);
33+
/**
34+
* Sets the selection and the amount accordingly to the value passed to the input
35+
* @param newAmount - Changed amount from user input
36+
*/
37+
const setNewAmount = useCallback(
38+
(newAmount: string) => {
39+
// Remove spaces from the newAmount value because Safari on iOS adds spaces when pasting a copied value
40+
// More info: https://github.com/Expensify/App/issues/16974
41+
const newAmountWithoutSpaces = stripSpacesFromAmount(newAmount);
42+
const replacedCommasAmount = replaceCommasWithPeriod(newAmountWithoutSpaces);
43+
onInputChange?.(replacedCommasAmount);
44+
},
45+
[onInputChange],
46+
);
47+
2248
return (
2349
<TextInput
2450
inputID={inputID}
2551
name={name}
2652
label={label}
53+
onChangeText={setNewAmount}
2754
defaultValue={defaultValue}
2855
accessibilityLabel={accessibilityLabel}
2956
role={role}
3057
ref={ref}
3158
keyboardType={!shouldAllowNegative ? CONST.KEYBOARD_TYPE.DECIMAL_PAD : undefined}
3259
type="mask"
33-
mask="[09999999].[09]"
60+
mask={`[09999999]${separator}[09]`}
3461
allowedKeys="0123456789.,"
3562
// On android autoCapitalize="words" is necessary when keyboardType="decimal-pad" or inputMode="decimal" to prevent input lag.
3663
// See https://github.com/Expensify/App/issues/51868 for more information

0 commit comments

Comments
 (0)