@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.
@@ -0,0 +1,7 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(npx tsc *)"
5
+ ]
6
+ }
7
+ }
@@ -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 handleChangeCurrency = (event) => {
7
- const { value } = event.currentTarget;
8
- const sanitize = numberHelper.currencySanitize(value, currency.locale, currency.format);
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
- const { fastField = true, ...restAttrs } = attrs;
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-br', format = 'BRL') {
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 sanitized = this.sanitize(value).replace(/\D/g, '');
33
- if (!sanitized || /^0+$/.test(sanitized)) {
32
+ const digits = value.replace(/\D/g, '');
33
+ if (!digits)
34
34
  return null;
35
- }
36
35
  if (fractionDigits === 0) {
37
- return String(Number(sanitized));
36
+ return String(Number.parseInt(digits, 10));
38
37
  }
39
- const paddedValue = sanitized.padStart(fractionDigits + 1, '0');
40
- const integer = paddedValue.slice(0, -fractionDigits);
41
- const decimal = paddedValue.slice(-fractionDigits);
42
- return `${Number(integer)}.${decimal}`;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dilicorp/ui",
3
- "version": "1.3.22",
3
+ "version": "2.0.0",
4
4
  "description": "A simple UI design for Dilicorp",
5
5
  "repository": {
6
6
  "type": "git",