@grapadigital/shared-app-modules 0.0.54 → 0.0.55
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.
- package/dist/events.d.ts +52 -0
- package/dist/events.js +16 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.js +27 -16
- package/package.json +5 -1
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ChangeEvent } from 'react';
|
|
2
|
+
import { KeyboardEvent as KeyboardEvent_2 } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lida com a alteração de um input de porcentagem, convertendo o valor do input
|
|
6
|
+
* em um número decimal entre 0 e 1 e chamando o callback com esse valor.
|
|
7
|
+
*
|
|
8
|
+
* Esta função utiliza `preFormatPercentage` para:
|
|
9
|
+
* - Remover o símbolo de porcentagem ("%").
|
|
10
|
+
* - Converter o valor em decimal (ex: "50%" → 0.5).
|
|
11
|
+
* - Limitar valores acima de 100% a 1.
|
|
12
|
+
* - Retornar `false` (não chama o callback) se o valor não for numérico.
|
|
13
|
+
*
|
|
14
|
+
* @param {ChangeEvent<HTMLInputElement>} event - O evento de mudança do input.
|
|
15
|
+
* @param {(value: number) => void} callback - Função chamada com o valor decimal
|
|
16
|
+
* após a formatação.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* <nput
|
|
20
|
+
* type="text"
|
|
21
|
+
* value={`${percentage * 100}%`}
|
|
22
|
+
* onChange={(event) => onChangePercentage(event, setPercentage)}
|
|
23
|
+
* />
|
|
24
|
+
*
|
|
25
|
+
* // Se o usuário digitar "75%", o callback receberá 0.75
|
|
26
|
+
* // Se o usuário digitar "150%", o callback receberá 1
|
|
27
|
+
* // Se o usuário digitar "abc", o callback não será chamado
|
|
28
|
+
*/
|
|
29
|
+
export declare const onChangePercentage: (event: ChangeEvent<HTMLInputElement>, callback: (value: number) => void) => void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Lida com o evento de teclado em um input de porcentagem, garantindo que
|
|
33
|
+
* ao pressionar "Backspace" o número seja removido corretamente, sem excluir
|
|
34
|
+
* o símbolo de porcentagem (%).
|
|
35
|
+
*
|
|
36
|
+
* Antes dessa função, ao pressionar "Backspace", a porcentagem era removida
|
|
37
|
+
* em vez do valor numérico.
|
|
38
|
+
*
|
|
39
|
+
* @param {KeyboardEvent<HTMLInputElement>} event - O evento de teclado do input.
|
|
40
|
+
* @param {(value: number) => void} callback - Função chamada com o novo valor numérico
|
|
41
|
+
* após a remoção do último dígito.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* <input
|
|
45
|
+
* type="text"
|
|
46
|
+
* value={`${percentage}%`}
|
|
47
|
+
* onKeyDown={(event) => onKeyDownPercentage(event, setPercentage)}
|
|
48
|
+
* />
|
|
49
|
+
*/
|
|
50
|
+
export declare const onKeyDownPercentage: (event: KeyboardEvent_2<HTMLInputElement>, callback: (value: number) => void) => void;
|
|
51
|
+
|
|
52
|
+
export { }
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { preFormatPercentage as s } from "./utils.js";
|
|
2
|
+
const i = (t, a) => {
|
|
3
|
+
const e = t.target.value, n = s(e);
|
|
4
|
+
n !== !1 && a(n);
|
|
5
|
+
}, f = (t, a) => {
|
|
6
|
+
const e = t.target, n = e.value, c = e.selectionStart, r = e.selectionEnd;
|
|
7
|
+
if (c === r && t.key === "Backspace") {
|
|
8
|
+
const u = n.slice(0, -2), o = s(u);
|
|
9
|
+
if (o === !1) return;
|
|
10
|
+
a(o);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
i as onChangePercentage,
|
|
15
|
+
f as onKeyDownPercentage
|
|
16
|
+
};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
export declare function formatCurrency(value: number): string;
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Formata um valor numérico ou string para uma representação de porcentagem inteira.
|
|
5
|
+
* Multiplica o valor por 100 e adiciona o símbolo "%".
|
|
6
|
+
*
|
|
7
|
+
* @param {string | number} value - O valor original (ex: 0.5 ou "0.5").
|
|
8
|
+
* @returns {string} O valor formatado como string (ex: "50%").
|
|
9
|
+
*/
|
|
10
|
+
export declare const formatPercentage: (value: string | number) => string;
|
|
11
|
+
|
|
3
12
|
/**
|
|
4
13
|
* Verifica se o valor fornecido é um número ou uma string que pode ser convertida para número.
|
|
5
14
|
*
|
|
@@ -62,6 +71,16 @@ export declare function maskZipCode(value: string): string;
|
|
|
62
71
|
|
|
63
72
|
export declare function preFormatCurrency(value: string): number;
|
|
64
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Prepara um valor de porcentagem (string ou número) para formato decimal (0 a 1).
|
|
76
|
+
* Remove o símbolo "%", converte para decimal e limita o resultado a no máximo 1.
|
|
77
|
+
*
|
|
78
|
+
* @param {string | number} value - O valor com ou sem símbolo de porcentagem (ex: "85%", 85).
|
|
79
|
+
* @returns {number | boolean} Retorna o valor decimal (ex: 0.85), um número 1 se for maior que 100%,
|
|
80
|
+
* ou `false` se a conversão não for um número válido.
|
|
81
|
+
*/
|
|
82
|
+
export declare const preFormatPercentage: (value: string | number) => number | false;
|
|
83
|
+
|
|
65
84
|
/**
|
|
66
85
|
* Remove qualquer formatação de uma string, deixando apenas os números.
|
|
67
86
|
*
|
package/dist/utils.js
CHANGED
|
@@ -1,41 +1,52 @@
|
|
|
1
|
+
function t(e) {
|
|
2
|
+
return !isNaN(Number(e));
|
|
3
|
+
}
|
|
1
4
|
function u(e) {
|
|
2
5
|
const r = e.replace(/[^0-9]/g, "");
|
|
3
6
|
return Number(r) / 100;
|
|
4
7
|
}
|
|
5
|
-
function
|
|
8
|
+
function a(e) {
|
|
6
9
|
return e.toLocaleString("pt-BR", {
|
|
7
10
|
style: "currency",
|
|
8
11
|
currency: "BRL"
|
|
9
12
|
});
|
|
10
13
|
}
|
|
11
|
-
|
|
14
|
+
const i = (e) => {
|
|
15
|
+
const r = t(e) ? e : Number(e);
|
|
16
|
+
return `${parseInt(String(r * 100))}%`;
|
|
17
|
+
}, l = (e) => {
|
|
18
|
+
const r = +String(e).replace("%", "");
|
|
19
|
+
if (!t(r)) return !1;
|
|
20
|
+
const n = r / 100;
|
|
21
|
+
return +String(n).split(".")[0] >= 1 ? 1 : n;
|
|
22
|
+
};
|
|
23
|
+
function o(e) {
|
|
12
24
|
return e.replace(/\D/g, "").replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, "$1.$2.$3-$4");
|
|
13
25
|
}
|
|
14
|
-
function
|
|
26
|
+
function m(e) {
|
|
15
27
|
return e.replace(/\D/g, "").replace(/^(\d{2})(\d{3})(\d{3})(\d{1})$/, "$1.$2.$3-$4");
|
|
16
28
|
}
|
|
17
|
-
function
|
|
29
|
+
function s(e) {
|
|
18
30
|
return e.replace(/\D/g, "").replace(
|
|
19
31
|
/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/,
|
|
20
32
|
"$1.$2.$3/$4-$5"
|
|
21
33
|
);
|
|
22
34
|
}
|
|
23
|
-
function
|
|
35
|
+
function d(e) {
|
|
24
36
|
return e.replace(/\D/g, "").replace(/^(\d{5})(\d{3})$/, "$1-$2");
|
|
25
37
|
}
|
|
26
|
-
function
|
|
38
|
+
function p(e) {
|
|
27
39
|
return e.replace(/\D/g, "");
|
|
28
40
|
}
|
|
29
|
-
function m(e) {
|
|
30
|
-
return !isNaN(Number(e));
|
|
31
|
-
}
|
|
32
41
|
export {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
a as formatCurrency,
|
|
43
|
+
i as formatPercentage,
|
|
44
|
+
t as isNumber,
|
|
45
|
+
s as maskCNPJ,
|
|
46
|
+
o as maskCPF,
|
|
47
|
+
m as maskRG,
|
|
48
|
+
d as maskZipCode,
|
|
39
49
|
u as preFormatCurrency,
|
|
40
|
-
|
|
50
|
+
l as preFormatPercentage,
|
|
51
|
+
p as unmask
|
|
41
52
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grapadigital/shared-app-modules",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.55",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
7
7
|
],
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"import": "./dist/hooks.js",
|
|
35
35
|
"types": "./dist/hooks.d.ts"
|
|
36
36
|
},
|
|
37
|
+
"./events": {
|
|
38
|
+
"import": "./dist/events.js",
|
|
39
|
+
"types": "./dist/events.d.ts"
|
|
40
|
+
},
|
|
37
41
|
"./interfaces/*": {
|
|
38
42
|
"types": "./dist/*.d.ts",
|
|
39
43
|
"import": "./dist/*.d.ts"
|