@grazziotin/react-components 1.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.
@@ -0,0 +1,381 @@
1
+ /**
2
+ * Função para formatar um CPF ou CNPJ, adicionando as pontuações adequadas.
3
+ *
4
+ * @param {string} v - O valor do CPF ou CNPJ a ser formatado (como string ou number).
5
+ * @returns {string} O valor formatado com as pontuações de CPF ou CNPJ.
6
+ *
7
+ * @example
8
+ * const cpfCnpjFormatado = mascaradorCPFCNPJ('12345678901'); // Retorna '123.456.789-01'
9
+ * @author Flaviasoz, Nicolasfmc
10
+ * @version 1.6.0
11
+ */
12
+ export var mascaradorCPFCNPJ = function (_a) {
13
+ var v = _a.v, autoComplete = _a.autoComplete, ignoreCNPJ = _a.ignoreCNPJ;
14
+ var value = v === null || v === void 0 ? void 0 : v.toString().replace(/\D/g, '');
15
+ if (!ignoreCNPJ) {
16
+ // Verifica se é um CPF (até 11 dígitos)
17
+ if ((value === null || value === void 0 ? void 0 : value.length) <= 11) {
18
+ var a = autoComplete ? value === null || value === void 0 ? void 0 : value.padStart(11, '0') : value;
19
+ return a
20
+ .replace(/(\d{3})(\d)/, '$1.$2')
21
+ .replace(/(\d{3})(\d)/, '$1.$2')
22
+ .replace(/(\d{3})(\d{1,2})$/, '$1-$2');
23
+ }
24
+ else {
25
+ // Se for um CNPJ
26
+ var a = autoComplete ? value === null || value === void 0 ? void 0 : value.padStart(14, '0') : value;
27
+ return a
28
+ .replace(/^(\d{2})(\d)/, '$1.$2')
29
+ .replace(/^(\d{2})\.(\d{3})(\d)/, '$1.$2.$3')
30
+ .replace(/\.(\d{3})(\d)/, '.$1/$2')
31
+ .replace(/(\d{4})(\d)/, '$1-$2');
32
+ }
33
+ }
34
+ else {
35
+ var a = autoComplete ? value === null || value === void 0 ? void 0 : value.padStart(11, '0') : value;
36
+ return a
37
+ .slice(0, 11)
38
+ .replace(/(\d{3})(\d)/, '$1.$2')
39
+ .replace(/(\d{3})(\d)/, '$1.$2')
40
+ .replace(/(\d{3})(\d{1,2})$/, '$1-$2');
41
+ }
42
+ };
43
+ /**
44
+ * Função para formatar um número de telefone, adicionando as pontuações adequadas.
45
+ *
46
+ * @param {string | number} v - O número de telefone a ser formatado (como string ou number).
47
+ * @returns {string} O número de telefone formatado com as pontuações adequadas.
48
+ *
49
+ * @example
50
+ * const telefoneFormatado = mascaradorTelefone('1234567890'); // Retorna '(12) 3456-7890'
51
+ * @author Flaviasoz
52
+ * @version 1.2.0
53
+ */
54
+ export var mascaradorTelefone = function (v) {
55
+ v = v === null || v === void 0 ? void 0 : v.toString().replace(/\D/g, '');
56
+ if (v.length === 8) {
57
+ return v.replace(/(\d{4})(\d{4})/, '$1-$2');
58
+ }
59
+ else if (v.length === 9) {
60
+ return v.replace(/(\d{5})(\d{4})/, '$1-$2');
61
+ }
62
+ else if (v.length === 10) {
63
+ return v.replace(/(\d{2})(\d{4})(\d{4})/, '($1) $2-$3');
64
+ }
65
+ else if (v.length === 11) {
66
+ return v.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3');
67
+ }
68
+ else if (v.length === 12) {
69
+ return v.replace(/(\d{2})(\d{2})(\d{4})(\d{4})/, '+$1 ($2) $3-$4');
70
+ }
71
+ else if (v.length === 13) {
72
+ return v.replace(/(\d{2})(\d{2})(\d{5})(\d{4})/, '+$1 ($2) $3-$4');
73
+ }
74
+ else {
75
+ return v;
76
+ }
77
+ };
78
+ /**
79
+ * Função para adicionar pontos de separação de milhar em um número.
80
+ *
81
+ * @param {string | number} valor - O valor numérico a ser formatado com pontos de separação de milhar.
82
+ * @returns {string} - O valor formatado com pontos de separação de milhar.
83
+ *
84
+ * @example
85
+ * const numeroFormatado = mascaradorMilhar(1234567); // Retorna '1.234.567'
86
+ * @author Flaviasoz
87
+ * @version 1.0.0
88
+ */
89
+ export var mascaradorMilhar = function (valor) {
90
+ var _a;
91
+ return (_a = valor === null || valor === void 0 ? void 0 : valor.toString()) === null || _a === void 0 ? void 0 : _a.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
92
+ };
93
+ /**
94
+ * Função para adicionar pontos de separação de milhar e decimais em um número.
95
+ *
96
+ * @param {string | number} v - O valor numérico a ser formatado com pontos de separação de milhar.
97
+ * @returns {string} - O valor formatado com pontos de separação de milhar e duas casas decimais após a vírgula.
98
+ *
99
+ * @example
100
+ * const numeroFormatado = mascaradorMilharDecimal(1234567); // Retorna '1.234.567,00'
101
+ * @author Nicolasfmc
102
+ * @version 1.0.0
103
+ */
104
+ export var mascaradorMilharDecimal = function (v) {
105
+ var res = '';
106
+ if (!v) {
107
+ v = 0;
108
+ }
109
+ res = v
110
+ .toLocaleString('pt-BR', {
111
+ style: 'currency',
112
+ currency: 'BRL',
113
+ })
114
+ .toString();
115
+ return res.replace('R$', '').trim();
116
+ };
117
+ /**
118
+ * Função para formatar um valor numérico como moeda brasileira (BRL).
119
+ *
120
+ * @param {string | number} v - O valor a ser formatado como moeda brasileira.
121
+ * @returns {string} O valor formatado como moeda brasileira (BRL), no formato de string.
122
+ * @example
123
+ * style: 'currency', // Estilo de formatação de moeda
124
+ * currency: 'BRL', // Tipo de moeda (Real brasileiro)
125
+ * const valorFormatado = mascaradorValorReal(12345.67); // Retorna 'R$ 12.345,67'
126
+ * @author Flaviasoz
127
+ * @version 1.0.0
128
+ */
129
+ export var mascaradorValorReal = function (v) {
130
+ // Convertendo para um número, se for uma string
131
+ var valorNumerico = typeof v === 'string' ? parseFloat(v) : v !== null && v !== void 0 ? v : 0;
132
+ // Verificando se a conversão foi bem-sucedida
133
+ if (!isNaN(valorNumerico)) {
134
+ // Formatando o número como moeda
135
+ var valorFormatado = valorNumerico.toLocaleString('pt-BR', {
136
+ style: 'currency',
137
+ currency: 'BRL',
138
+ });
139
+ return valorFormatado;
140
+ }
141
+ else {
142
+ // Tratamento para casos em que a conversão não é bem-sucedida
143
+ return 'R$ 0,00';
144
+ }
145
+ };
146
+ /**
147
+ * Função para formatar uma data no formato 'YYYY-MM-DD' para 'DD/MM/YYYY'.
148
+ *
149
+ * @param {Date | string} data - A data a ser formatada (como objeto Date ou string).
150
+ * @returns {string} A data formatada no formato 'DD/MM/YYYY' ou '' se a entrada for inválida.
151
+ *
152
+ * @example
153
+ * const dataFormatada = formatarDataBR('2021-09-26'); // Retorna '26/09/2021'
154
+ * @author Flaviasoz
155
+ * @version 1.0.0
156
+ */
157
+ export function formatarDataBR(data) {
158
+ var _a, _b, _c, _d;
159
+ var dataformata = (_d = (_c = (_b = (_a = data === null || data === void 0 ? void 0 : data.toString()) === null || _a === void 0 ? void 0 : _a.substring(0, 10)) === null || _b === void 0 ? void 0 : _b.split('-')) === null || _c === void 0 ? void 0 : _c.reverse()) === null || _d === void 0 ? void 0 : _d.join('/');
160
+ return dataformata || '';
161
+ }
162
+ /**
163
+ * Função para formatar uma data no formato 'yyyy-mm' e um dia para 'DD/MM/YYYY'.
164
+ *
165
+ * @param {string} anoMes - A string no formato 'yyyy-mm'.
166
+ * @param {string} [dia='01'] - A string representando o dia (opcional, padrão é '01' se não informado).
167
+ * @returns {string} A data formatada no formato 'DD/MM/YYYY' ou '' se a entrada for inválida.
168
+ *
169
+ * @example
170
+ * const dataFormatada = formatarMesAno('2023-01'); // Retorna '01/01/2023'
171
+ * const dataFormatadaComDia = formatarMesAno('2023-01', '10'); // Retorna '10/01/2023'
172
+ * @author Nicolasfmc
173
+ * @version 1.1.0
174
+ */
175
+ export function formatarMesAno(anoMes, dia) {
176
+ if (dia === void 0) { dia = '01'; }
177
+ var ano = anoMes.substring(0, 4);
178
+ var mes = anoMes.substring(5, 7);
179
+ var diaFormatado = dia.padStart(2, '0');
180
+ return "".concat(diaFormatado, "/").concat(mes, "/").concat(ano);
181
+ }
182
+ /**
183
+ * Formata um valor numérico para uma string no formato de moeda brasileira (BRL).
184
+ *
185
+ * @param {string} v - Valor numérico a ser formatado.
186
+ * @returns {string} - Valor formatado como moeda brasileira.
187
+ * @example
188
+ * const valorFormatado = mascaradorInputMoeda('123456'); // Resultado: 'R$ 1.234,56'
189
+ * @author Flaviasoz
190
+ * @version 1.0.0
191
+ */
192
+ export function mascaradorInputMoeda(v) {
193
+ var valor = v.replace(/\D/g, '');
194
+ // Formata para moeda
195
+ valor = (Number(valor) / 100).toLocaleString('pt-BR', {
196
+ style: 'currency',
197
+ currency: 'BRL',
198
+ });
199
+ return valor;
200
+ }
201
+ /**
202
+ * Converte uma string formatada como moeda brasileira para um número decimal.
203
+ *
204
+ * @param {string} valor - String no formato de moeda brasileira a ser convertida.
205
+ * @returns {number} - Valor convertido para número decimal.
206
+ * @example
207
+ * const valorDecimal = formatarInputMoedaFloat('R$1.234,56'); // Resultado: 1234.56
208
+ * @author Flaviasoz
209
+ * @version 1.0.0
210
+ */
211
+ export function formatarInputMoedaFloat(valor) {
212
+ return parseFloat(valor.replace('R$', '').replaceAll('.', '').replace(',', '.'));
213
+ }
214
+ /**
215
+ * Função para formatar uma string numérica no formato '00.00.00.0.000'.
216
+ *
217
+ * @param {string} v - A string numérica a ser formatada.
218
+ * @returns {string} A string formatada no formato '00.00.00.0.000'.
219
+ * @example
220
+ * const stringFormatada = formatarStringNumerica('1234567890'); // Retorna '12.34.56.7.890'
221
+ * @author Nicolasfmc
222
+ * @version 1.0.0
223
+ */
224
+ export function mascaradorCodItem(v) {
225
+ v = v === null || v === void 0 ? void 0 : v.replace(/\D/g, '');
226
+ if (v && (v === null || v === void 0 ? void 0 : v.length) < 10) {
227
+ v = v === null || v === void 0 ? void 0 : v.padEnd(10, '0');
228
+ }
229
+ return v === null || v === void 0 ? void 0 : v.replace(/(\d{2})(\d{2})(\d{2})(\d{1})(\d{3})/, '$1.$2.$3.$4.$5');
230
+ }
231
+ /**
232
+ * Função para formatar uma string numérica no formato dinâmico até '50.50.20.20.5.510'.
233
+ *
234
+ * @param {string} v - A string numérica a ser formatada.
235
+ * @returns {string} A string formatada dinamicamente conforme o número de dígitos.
236
+ * @example
237
+ * const stringFormatada = mascaradorCodItemRede('1010'); // Retorna '10.10'
238
+ * const stringFormatadaComNoveDigitos = mascaradorCodItemRede('505020205'); // Retorna '50.50.20.20.5'
239
+ * const stringFormatadaComDozeDigitos = mascaradorCodItemRede('505020205510'); // Retorna '50.50.20.20.5.510'
240
+ * @author Flaviasoz
241
+ * @version 1.0.1
242
+ */
243
+ export function mascaradorCodItemRede(v) {
244
+ v = v.replace(/\D/g, ''); // Remove caracteres não numéricos
245
+ // Formatação dinâmica conforme o comprimento da string
246
+ if (v.length <= 2) {
247
+ return v;
248
+ }
249
+ else if (v.length <= 4) {
250
+ return v.replace(/(\d{2})(\d+)/, '$1.$2');
251
+ }
252
+ else if (v.length <= 6) {
253
+ return v.replace(/(\d{2})(\d{2})(\d+)/, '$1.$2.$3');
254
+ }
255
+ else if (v.length <= 8) {
256
+ return v.replace(/(\d{2})(\d{2})(\d{2})(\d+)/, '$1.$2.$3.$4');
257
+ }
258
+ else if (v.length === 9) {
259
+ return v.replace(/(\d{2})(\d{2})(\d{2})(\d{2})(\d+)/, '$1.$2.$3.$4.$5');
260
+ }
261
+ else if (v.length <= 11) {
262
+ return v.replace(/(\d{2})(\d{2})(\d{2})(\d{2})(\d{1})(\d+)/, '$1.$2.$3.$4.$5.$6');
263
+ }
264
+ else {
265
+ return v.replace(/(\d{2})(\d{2})(\d{2})(\d{2})(\d{1})(\d{3})/, '$1.$2.$3.$4.$5.$6');
266
+ }
267
+ }
268
+ /**
269
+ * Formata uma data para uma string no formato 'dia de mês de ano, hora'.
270
+ *
271
+ * @param {Date} data - A data a ser formatada.
272
+ * @returns {string} A data formatada no formato 'dia de mês de ano, hora'.
273
+ * @author Flaviasoz
274
+ */
275
+ export function formatarDataParaString(data) {
276
+ var diasSemana = [
277
+ 'Domingo',
278
+ 'Segunda-feira',
279
+ 'Terça-feira',
280
+ 'Quarta-feira',
281
+ 'Quinta-feira',
282
+ 'Sexta-feira',
283
+ 'Sábado',
284
+ ];
285
+ var meses = [
286
+ 'Janeiro',
287
+ 'Fevereiro',
288
+ 'Março',
289
+ 'Abril',
290
+ 'Maio',
291
+ 'Junho',
292
+ 'Julho',
293
+ 'Agosto',
294
+ 'Setembro',
295
+ 'Outubro',
296
+ 'Novembro',
297
+ 'Dezembro',
298
+ ];
299
+ var diaSemana = diasSemana[data.getDay()];
300
+ var dia = data.getDate();
301
+ var mes = meses[data.getMonth()];
302
+ var ano = data.getFullYear();
303
+ var hora = data.toLocaleTimeString('pt-BR', {
304
+ hour: '2-digit',
305
+ minute: '2-digit',
306
+ });
307
+ var dataFormatada = "".concat(diaSemana, ", ").concat(dia, " de ").concat(mes, " de ").concat(ano, ", ").concat(hora);
308
+ return dataFormatada;
309
+ }
310
+ export function mascararCPF(_a) {
311
+ var cpfCnpj = _a.cpfCnpj, _b = _a.autoComplete, autoComplete = _b === void 0 ? false : _b;
312
+ var value = cpfCnpj === null || cpfCnpj === void 0 ? void 0 : cpfCnpj.toString().replace(/\D/g, '');
313
+ var mask = autoComplete ? value === null || value === void 0 ? void 0 : value.padStart(11, '0') : value;
314
+ return mask
315
+ .replace(/(\d{3})(\d)/, '$1.$2')
316
+ .replace(/(\d{3})(\d)/, '$1.$2')
317
+ .replace(/(\d{3})(\d{1,2})$/, '$1-$2');
318
+ }
319
+ /**
320
+ * Mascara um CNPJ, adicionando pontos, barra e traço.
321
+ *
322
+ * @param {string} cnpj - O CNPJ a ser mascarado.
323
+ * @param {boolean} autoComplete - Indica se deve completar com zeros à esquerda.
324
+ * @returns {string} O CNPJ mascarado.
325
+ */
326
+ export function mascararCNPJ(_a) {
327
+ var cpfCnpj = _a.cpfCnpj, _b = _a.autoComplete, autoComplete = _b === void 0 ? false : _b;
328
+ var value = cpfCnpj === null || cpfCnpj === void 0 ? void 0 : cpfCnpj.toString().replace(/\D/g, '');
329
+ var mask = autoComplete ? value === null || value === void 0 ? void 0 : value.padStart(14, '0') : value;
330
+ return mask
331
+ .replace(/^(\d{2})(\d)/, '$1.$2')
332
+ .replace(/^(\d{2})\.(\d{3})(\d)/, '$1.$2.$3')
333
+ .replace(/\.(\d{3})(\d)/, '.$1/$2')
334
+ .replace(/(\d{4})(\d)/, '$1-$2');
335
+ }
336
+ export function formatValuesToMoney(obj, fieldsToFormat) {
337
+ if (Array.isArray(obj)) {
338
+ return obj.map(function (item) { return formatValuesToMoney(item, fieldsToFormat); });
339
+ }
340
+ else if (typeof obj === 'object' && obj !== null) {
341
+ var newObj = {};
342
+ for (var key in obj) {
343
+ if (Object.hasOwn(obj, key)) {
344
+ if (fieldsToFormat.includes(key)) {
345
+ var value = obj[key];
346
+ if (typeof value === 'number') {
347
+ newObj[key] = mascaradorInputMoeda(String(value.toFixed(2)));
348
+ }
349
+ else if (typeof value === 'string') {
350
+ var numericValue = parseFloat(value.replace(/[^\d.-]+/g, ''));
351
+ newObj[key] = isNaN(numericValue)
352
+ ? value
353
+ : mascaradorInputMoeda(String(numericValue.toFixed(2)));
354
+ }
355
+ else {
356
+ newObj[key] = formatValuesToMoney(value, fieldsToFormat);
357
+ }
358
+ }
359
+ else {
360
+ newObj[key] = formatValuesToMoney(obj[key], fieldsToFormat);
361
+ }
362
+ }
363
+ }
364
+ return newObj;
365
+ }
366
+ else {
367
+ return obj;
368
+ }
369
+ }
370
+ export function limitDecimais(value, limit) {
371
+ value = value.replace(/[^\d.]/g, '').replace(',', '.');
372
+ var parts = value.split('.');
373
+ var oldVal = value;
374
+ if (parts.length > 2) {
375
+ return oldVal.slice(0, oldVal.length - 1);
376
+ }
377
+ if (parts.length > 1 && parts[1].length > limit) {
378
+ return oldVal.slice(0, oldVal.length - (parts[1].length - limit));
379
+ }
380
+ return value;
381
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Gancho personalizado para tornar os árbitros com estado
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const [inputRef, setInputRef] = useStateRef<HTMLInputElement | null>(null);
7
+ *
8
+ * <input type = "text" ref = {setInputRef} />
9
+ * ```
10
+ * @version 1.0.0
11
+ * @ViniciusCLoeblein
12
+ */
13
+ export declare function useStateRef<T>(initialValue: T): [T, (node: T) => void];
14
+ /**
15
+ * usa a dimensão passada por props para saber se a tela é menor, se for retorna True
16
+ * @ViniciusCLoeblein @Flaviasoz
17
+ * @version 1.2.0
18
+ */
19
+ export declare function useDimension(width: number): boolean;
20
+ export declare const useWindowWidth: () => number;
@@ -0,0 +1,49 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ /**
3
+ * Gancho personalizado para tornar os árbitros com estado
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const [inputRef, setInputRef] = useStateRef<HTMLInputElement | null>(null);
8
+ *
9
+ * <input type = "text" ref = {setInputRef} />
10
+ * ```
11
+ * @version 1.0.0
12
+ * @ViniciusCLoeblein
13
+ */
14
+ export function useStateRef(initialValue) {
15
+ var _a = useState(initialValue), state = _a[0], setState = _a[1];
16
+ var setRef = useCallback(function (node) {
17
+ setState(node);
18
+ }, []);
19
+ return [state, setRef];
20
+ }
21
+ /**
22
+ * usa a dimensão passada por props para saber se a tela é menor, se for retorna True
23
+ * @ViniciusCLoeblein @Flaviasoz
24
+ * @version 1.2.0
25
+ */
26
+ export function useDimension(width) {
27
+ var _a = useState(false), state = _a[0], setState = _a[1];
28
+ var updateMobileState = useCallback(function () {
29
+ var newMobile = window.innerWidth <= width;
30
+ if (newMobile !== state) {
31
+ setState(newMobile);
32
+ }
33
+ }, [width, state]);
34
+ useEffect(function () {
35
+ updateMobileState();
36
+ window.addEventListener('resize', updateMobileState);
37
+ return function () { return window.removeEventListener('resize', updateMobileState); };
38
+ }, [updateMobileState]);
39
+ return state;
40
+ }
41
+ export var useWindowWidth = function () {
42
+ var _a = useState(window.innerWidth), width = _a[0], setWidth = _a[1];
43
+ useEffect(function () {
44
+ var handleResize = function () { return setWidth(window.innerWidth); };
45
+ window.addEventListener('resize', handleResize);
46
+ return function () { return window.removeEventListener('resize', handleResize); };
47
+ }, []);
48
+ return width;
49
+ };
@@ -0,0 +1,2 @@
1
+ export { default as Input } from './components/Inputs/Input';
2
+ export type { InputProps } from './components/Inputs/Input';