@dilicorp/ui 1.3.22 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { FastField, Field } from 'formik';
|
|
2
|
-
import React from 'react';
|
|
2
|
+
import React, { useCallback } from 'react';
|
|
3
3
|
import numberHelper from '../../../utils/number-helper';
|
|
4
4
|
export const InputCurrency = ({ attrs, handleChange, currency, name }) => {
|
|
5
|
-
const { maxLength = 20 } = attrs;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const { maxLength = 20, fastField = true, ...restAttrs } = attrs;
|
|
6
|
+
const { format, locale } = currency;
|
|
7
|
+
const Tag = fastField ? FastField : Field;
|
|
8
|
+
const handleChangeCurrency = useCallback((event) => {
|
|
9
|
+
const rawValue = event.currentTarget.value;
|
|
10
|
+
const numericValue = numberHelper.currencySanitize(rawValue, locale, format);
|
|
11
|
+
const formatted = numericValue === null
|
|
12
|
+
? ''
|
|
13
|
+
: numberHelper.currency(Number(numericValue), locale, format);
|
|
9
14
|
handleChange({
|
|
10
|
-
currentTarget: {
|
|
11
|
-
name,
|
|
12
|
-
value: sanitize ? numberHelper.currency(Number(sanitize), currency.locale, currency.format) : ''
|
|
13
|
-
}
|
|
15
|
+
currentTarget: { name, value: formatted }
|
|
14
16
|
});
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const Tag = fastField ? FastField : Field;
|
|
18
|
-
return (React.createElement(Tag, { name: name }, ({ field }) => React.createElement("input", { ...field, ...restAttrs, onChange: handleChangeCurrency, min: "0", maxLength: maxLength })));
|
|
17
|
+
}, [locale, format, name, handleChange]);
|
|
18
|
+
return (React.createElement(Tag, { name: name }, ({ field }) => (React.createElement("input", { ...field, ...restAttrs, maxLength: maxLength, onChange: handleChangeCurrency }))));
|
|
19
19
|
};
|
|
@@ -8,7 +8,7 @@ class NumberHelper {
|
|
|
8
8
|
sanitize(string) {
|
|
9
9
|
return string.replace(/\D/g, '');
|
|
10
10
|
}
|
|
11
|
-
currency(value = 0, locale = 'pt-
|
|
11
|
+
currency(value = 0, locale = 'pt-BR', format = 'BRL') {
|
|
12
12
|
return Intl.NumberFormat(locale, {
|
|
13
13
|
currency: format,
|
|
14
14
|
style: 'currency'
|
|
@@ -29,17 +29,18 @@ class NumberHelper {
|
|
|
29
29
|
}
|
|
30
30
|
currencySanitize(value, locale = 'pt-BR', currency = 'BRL') {
|
|
31
31
|
const fractionDigits = this.getCurrencyFractionDigits(locale, currency);
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
32
|
+
const digits = value.replace(/\D/g, '');
|
|
33
|
+
if (!digits)
|
|
34
34
|
return null;
|
|
35
|
-
}
|
|
36
35
|
if (fractionDigits === 0) {
|
|
37
|
-
return String(Number(
|
|
36
|
+
return String(Number.parseInt(digits, 10));
|
|
38
37
|
}
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
38
|
+
const padded = digits.padStart(fractionDigits + 1, '0');
|
|
39
|
+
const intPart = Number.parseInt(padded.slice(0, -fractionDigits), 10);
|
|
40
|
+
const decPart = padded.slice(-fractionDigits);
|
|
41
|
+
if (intPart === 0 && Number.parseInt(decPart, 10) === 0)
|
|
42
|
+
return null;
|
|
43
|
+
return `${intPart}.${decPart}`;
|
|
43
44
|
}
|
|
44
45
|
percentageSanitize(value, decimals = 2) {
|
|
45
46
|
var _a;
|