hi-tm 0.19.5 → 0.19.6

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.
@@ -54,11 +54,35 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
54
54
  const isErrorVisible = () => {
55
55
  return !(textError && textError.trim() !== "");
56
56
  };
57
+ const formatWithCommas = (value) => {
58
+ // Remove any non-digit characters except decimal point
59
+ const numericValue = value.replace(/[^\d.]/g, '');
60
+ if (numericValue === '')
61
+ return '';
62
+ // Split the number into integer and decimal parts (for currencies that might have decimals)
63
+ const [integerPart, decimalPart] = numericValue.split('.');
64
+ // Format the integer part with commas
65
+ const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
66
+ // Return the formatted value with decimal part if it exists
67
+ return decimalPart !== undefined ? `${formattedInteger}.${decimalPart}` : formattedInteger;
68
+ };
57
69
  const handleChange = (event) => {
58
70
  const newValue = event.target.value;
59
- const validValue = newValue.replace(/[^\d۰-۹]/g, "");
60
- setInputValue(validValue);
61
- onChangeValueHandler(event);
71
+ // Remove all commas from the input to get the raw numeric value
72
+ const rawValue = newValue.replace(/,/g, '');
73
+ // Validate that the raw value contains only digits and optionally a decimal point
74
+ if (/^\d*\.?\d*$/.test(rawValue)) {
75
+ setInputValue(rawValue);
76
+ // Create a new event with the raw numeric value for the handler
77
+ const syntheticEvent = {
78
+ ...event,
79
+ target: {
80
+ ...event.target,
81
+ value: rawValue
82
+ }
83
+ };
84
+ onChangeValueHandler(syntheticEvent);
85
+ }
62
86
  };
63
87
  const handleKeyDown = (event) => {
64
88
  const allowedKeys = [
@@ -67,9 +91,17 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
67
91
  "ArrowRight",
68
92
  "Delete",
69
93
  "Tab",
94
+ ".",
70
95
  ];
71
- const isNumber = /[0-9۰-۹]/.test(event.key);
72
- if (!isNumber && !allowedKeys.includes(event.key)) {
96
+ const isNumber = /[0-9]/.test(event.key);
97
+ // Allow decimal point only if it's not already in the value
98
+ if (event.key === '.') {
99
+ const currentValue = event.target.value;
100
+ if (currentValue.includes('.')) {
101
+ event.preventDefault();
102
+ }
103
+ }
104
+ else if (!isNumber && !allowedKeys.includes(event.key)) {
73
105
  event.preventDefault();
74
106
  }
75
107
  if (onKeyDownHandler)
@@ -83,7 +115,7 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
83
115
  react_1.default.createElement("div", { className: `w-full h-9 flex items-center justify-between bg-inherit outline-none border rounded-lg text-sm px-2 ${isErrorVisible()
84
116
  ? "border-gray-tertiary focus:border-gray-quaternary"
85
117
  : "border-red-primary"}` },
86
- react_1.default.createElement("input", { ref: internalRef, type: "text", id: label, disabled: disabled, value: Number(inputValue).toLocaleString(), placeholder: placeholder, autoComplete: autoComplete ? "on" : "off", autoFocus: autoFocus, onChange: handleChange, onBlur: onBlurHandler, onFocus: onFocusHandler, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, className: "outline-none bg-inherit w-full h-full", required: required }),
118
+ react_1.default.createElement("input", { ref: internalRef, type: "text", id: label, disabled: disabled, value: formatWithCommas(inputValue), placeholder: placeholder, autoComplete: autoComplete ? "on" : "off", autoFocus: autoFocus, onChange: handleChange, onBlur: onBlurHandler, onFocus: onFocusHandler, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, className: "outline-none bg-inherit w-full h-full", required: required }),
87
119
  react_1.default.createElement("p", { className: "text-sm text-text-color-primary border-r border-gray-tertiary px-2" }, currencyUnit)),
88
120
  !isErrorVisible() && (react_1.default.createElement("p", { className: "text-sm mt-1 text-red-primary" }, textError?.trim())))));
89
121
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.19.5",
3
+ "version": "0.19.6",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {