1
- import React from 'react' ;
1
+ import React , { useCallback , useMemo } from 'react' ;
2
2
import type { ForwardedRef } from 'react' ;
3
+ import useLocalize from '@hooks/useLocalize' ;
4
+ import { replaceAllDigits , replaceCommasWithPeriod , stripSpacesFromAmount } from '@libs/MoneyRequestUtils' ;
3
5
import CONST from '@src/CONST' ;
4
6
import TextInput from './TextInput' ;
5
7
import type { BaseTextInputProps , BaseTextInputRef } from './TextInput/BaseTextInput/types' ;
@@ -16,21 +18,46 @@ type AmountFormProps = {
16
18
} & Partial < BaseTextInputProps > ;
17
19
18
20
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 ,
20
22
ref : ForwardedRef < BaseTextInputRef > ,
21
23
) {
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
+
22
48
return (
23
49
< TextInput
24
50
inputID = { inputID }
25
51
name = { name }
26
52
label = { label }
53
+ onChangeText = { setNewAmount }
27
54
defaultValue = { defaultValue }
28
55
accessibilityLabel = { accessibilityLabel }
29
56
role = { role }
30
57
ref = { ref }
31
58
keyboardType = { ! shouldAllowNegative ? CONST . KEYBOARD_TYPE . DECIMAL_PAD : undefined }
32
59
type = "mask"
33
- mask = " [09999999]. [09]"
60
+ mask = { ` [09999999]${ separator } [09]` }
34
61
allowedKeys = "0123456789.,"
35
62
// On android autoCapitalize="words" is necessary when keyboardType="decimal-pad" or inputMode="decimal" to prevent input lag.
36
63
// See https://github.com/Expensify/App/issues/51868 for more information
0 commit comments