@@ -14,7 +14,9 @@ import { ERROR_EXECUTION, FormatNumber } from '@/components/input/types/input';
14
14
import { InputTypeType } from '@/components/input/types/inputType' ;
15
15
import {
16
16
checkValidFormattedNumber ,
17
+ convertDecimalSeparator ,
17
18
formatNumber ,
19
+ getDecimalSeparator ,
18
20
getState ,
19
21
removeThousandSeparator ,
20
22
} from '@/components/input/utils' ;
@@ -28,7 +30,6 @@ import {
28
30
getPosition ,
29
31
isArrowDownPressed ,
30
32
isArrowUpPressed ,
31
- isKeyEnterPressed ,
32
33
maxCountBetweenChars ,
33
34
} from '@/utils' ;
34
35
import { matchInputValue } from '@/utils/maskUtility/mask.utility' ;
@@ -82,7 +83,7 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
82
83
if ( props . currentValue !== undefined ) {
83
84
let newValue = props . currentValue ;
84
85
// format with formatNumber configuration
85
- if ( firstRender . current && props . formatNumber ) {
86
+ if ( firstRender . current && props . formatNumber && ! focus ) {
86
87
// add thousand separator to the value
87
88
newValue = addThousandSeparator ( String ( props . currentValue ) , props . formatNumber ) ;
88
89
}
@@ -118,7 +119,7 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
118
119
119
120
// truncate the value with maximun of decimals
120
121
const truncateValue =
121
- props . truncate && props . maxDecimals
122
+ props . truncate && props . maxDecimals !== null && props . maxDecimals !== undefined
122
123
? truncatedValue ( String ( limitedValue ) , props . maxDecimals )
123
124
: limitedValue ;
124
125
handleSetValue ( truncateValue ) ;
@@ -127,9 +128,9 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
127
128
128
129
// add thousand separator to the value
129
130
const addThousandSeparator = ( value : string , format : FormatNumber ) => {
130
- if ( checkValidFormattedNumber ( value , format . locale ) ) {
131
+ if ( checkValidFormattedNumber ( value , props . locale || format . locale ) ) {
131
132
// remove existing thousand separator
132
- const newValue = removeThousandSeparator ( value , format . locale ) ;
133
+ const newValue = removeThousandSeparator ( value , props . locale || format . locale ) ;
133
134
// format value
134
135
const formattedValue = formatNumber ( Number ( newValue ) , format ) ;
135
136
return formattedValue ;
@@ -149,8 +150,12 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
149
150
const newMaskedValue = matchInputValue ( String ( value ) , event . target . value , props . regex ) ;
150
151
event . target . value = newMaskedValue ;
151
152
}
152
- if ( props . truncate && props . maxDecimals ) {
153
- event . target . value = truncatedValue ( String ( event . target . value ) , props . maxDecimals ) ;
153
+ if ( props . truncate && props . maxDecimals !== null && props . maxDecimals !== undefined ) {
154
+ event . target . value = truncatedValue (
155
+ String ( event . target . value ) ,
156
+ props . maxDecimals ,
157
+ props . locale || props . formatNumber ?. locale
158
+ ) ;
154
159
}
155
160
156
161
// key validation
@@ -186,6 +191,20 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
186
191
} ;
187
192
188
193
const handleFocusInternal : FocusEventHandler < HTMLInputElement > = event => {
194
+ if ( props . formatNumber ) {
195
+ // remove thousand separator to the value
196
+ event . target . value = removeThousandSeparator (
197
+ event . target . value ,
198
+ props . locale || props . formatNumber ?. locale ,
199
+ false
200
+ ) ;
201
+ handleSetValue ( event . target . value ) ;
202
+ }
203
+ // transform the string value to a number format with dot as decimal separator to avoid errors
204
+ event . target . value = convertDecimalSeparator (
205
+ event . target . value ,
206
+ getDecimalSeparator ( props . locale || props . formatNumber ?. locale )
207
+ ) ;
189
208
props . onFocus ?.( event ) ;
190
209
} ;
191
210
@@ -207,12 +226,6 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
207
226
) {
208
227
event . preventDefault ( ) ;
209
228
}
210
- // Check if the input has a formatNumber ans press the enter key
211
- if ( props . formatNumber && isKeyEnterPressed ( event . key ) && inputRef . current ) {
212
- // add thousand separator to the value
213
- const newValue = addThousandSeparator ( String ( value ) , props . formatNumber ) ;
214
- handleSetValue ( newValue ) ;
215
- }
216
229
217
230
const {
218
231
key,
0 commit comments