@dilicorp/ui 1.3.23 → 2.0.1
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,33 @@
|
|
|
1
1
|
import { FastField, Field } from 'formik';
|
|
2
|
-
import React from 'react';
|
|
2
|
+
import React, { useCallback, useRef } 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
|
-
const { value } = event.currentTarget;
|
|
8
|
-
const sanitize = numberHelper.currencySanitize(value, currency.locale, currency.format);
|
|
9
|
-
handleChange({
|
|
10
|
-
currentTarget: {
|
|
11
|
-
name,
|
|
12
|
-
value: sanitize ? numberHelper.currency(Number(sanitize), currency.locale, currency.format) : ''
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
const { fastField = true, ...restAttrs } = attrs;
|
|
5
|
+
const { maxLength = 20, fastField = true, ...restAttrs } = attrs;
|
|
6
|
+
const { format, locale } = currency;
|
|
17
7
|
const Tag = fastField ? FastField : Field;
|
|
18
|
-
|
|
8
|
+
// Rastreia se o evento de mudança foi originado por uma tecla de deleção
|
|
9
|
+
const isDeletingRef = useRef(false);
|
|
10
|
+
const handleKeyDown = useCallback((event) => {
|
|
11
|
+
isDeletingRef.current = event.key === 'Backspace' || event.key === 'Delete';
|
|
12
|
+
}, []);
|
|
13
|
+
const handleChangeCurrency = useCallback((event) => {
|
|
14
|
+
const rawValue = event.currentTarget.value;
|
|
15
|
+
const numericValue = numberHelper.currencySanitize(rawValue, locale, format);
|
|
16
|
+
// Nenhum dígito restante — campo foi completamente limpo
|
|
17
|
+
if (numericValue === null) {
|
|
18
|
+
handleChange({ currentTarget: { name, value: null } });
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
// Usuário está apagando e o valor resultante seria zero — limpa o campo
|
|
22
|
+
if (isDeletingRef.current && Number(numericValue) === 0) {
|
|
23
|
+
handleChange({ currentTarget: { name, value: null } });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const formatted = numberHelper.currency(Number(numericValue), locale, format);
|
|
27
|
+
handleChange({ currentTarget: { name, value: formatted } });
|
|
28
|
+
}, [locale, format, name, handleChange]);
|
|
29
|
+
return (React.createElement(Tag, { name: name }, ({ field }) => {
|
|
30
|
+
var _a;
|
|
31
|
+
return (React.createElement("input", { ...field, ...restAttrs, value: (_a = field.value) !== null && _a !== void 0 ? _a : '', maxLength: maxLength, onKeyDown: handleKeyDown, onChange: handleChangeCurrency }));
|
|
32
|
+
}));
|
|
19
33
|
};
|
|
@@ -29,17 +29,16 @@ 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
|
-
return `${
|
|
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
|
+
return `${intPart}.${decPart}`;
|
|
43
42
|
}
|
|
44
43
|
percentageSanitize(value, decimals = 2) {
|
|
45
44
|
var _a;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dilicorp/ui",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "A simple UI design for Dilicorp",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -84,6 +84,10 @@
|
|
|
84
84
|
"react-dom": ">=17.0.2",
|
|
85
85
|
"react-router-dom": ">=6.0.2"
|
|
86
86
|
},
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"access": "public",
|
|
89
|
+
"registry": "https://registry.npmjs.org/"
|
|
90
|
+
},
|
|
87
91
|
"scripts": {
|
|
88
92
|
"sass:pluxee": "sass src/assets/sass/themes/pluxee.scss:src/assets/css/styles.css",
|
|
89
93
|
"sass:gopass": "sass src/assets/sass/themes/gopass.scss:src/assets/css/styles.css",
|