hi-tm 0.20.0 → 0.20.2
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.
|
@@ -38,11 +38,13 @@ const react_1 = __importStar(require("react"));
|
|
|
38
38
|
const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, currencyUnit, textError, label, autoComplete, autoFocus, inlineCss, onChangeValueHandler, onBlurHandler, onFocusHandler, onKeyUpHandler, onKeyDownHandler, disabled = false, }, ref) => {
|
|
39
39
|
const [inputValue, setInputValue] = (0, react_1.useState)(value?.toString() || "");
|
|
40
40
|
const internalRef = (0, react_1.useRef)(null);
|
|
41
|
-
// Combine the external ref with the internal ref
|
|
42
41
|
react_1.default.useImperativeHandle(ref, () => internalRef.current);
|
|
43
42
|
(0, react_1.useEffect)(() => {
|
|
44
|
-
if (value !== null) {
|
|
45
|
-
setInputValue(value
|
|
43
|
+
if (value !== null && value !== undefined) {
|
|
44
|
+
setInputValue(value.toString());
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
setInputValue('');
|
|
46
48
|
}
|
|
47
49
|
}, [value]);
|
|
48
50
|
(0, react_1.useEffect)(() => {
|
|
@@ -51,29 +53,19 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
|
|
|
51
53
|
internalRef.current.selectionEnd = internalRef.current.value.length;
|
|
52
54
|
}
|
|
53
55
|
}, [value]);
|
|
54
|
-
const isErrorVisible = () => {
|
|
55
|
-
return !(textError && textError.trim() !== "");
|
|
56
|
-
};
|
|
57
56
|
const formatWithCommas = (value) => {
|
|
58
|
-
// Remove any non-digit characters except decimal point
|
|
59
57
|
const numericValue = value.replace(/[^\d.]/g, '');
|
|
60
58
|
if (numericValue === '')
|
|
61
59
|
return '';
|
|
62
|
-
// Split the number into integer and decimal parts (for currencies that might have decimals)
|
|
63
60
|
const [integerPart, decimalPart] = numericValue.split('.');
|
|
64
|
-
// Format the integer part with commas
|
|
65
61
|
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
66
|
-
// Return the formatted value with decimal part if it exists
|
|
67
62
|
return decimalPart !== undefined ? `${formattedInteger}.${decimalPart}` : formattedInteger;
|
|
68
63
|
};
|
|
69
64
|
const handleChange = (event) => {
|
|
70
65
|
const newValue = event.target.value;
|
|
71
|
-
// Remove all commas from the input to get the raw numeric value
|
|
72
66
|
const rawValue = newValue.replace(/,/g, '');
|
|
73
|
-
// Validate that the raw value contains only digits and optionally a decimal point
|
|
74
67
|
if (/^\d*\.?\d*$/.test(rawValue)) {
|
|
75
68
|
setInputValue(rawValue);
|
|
76
|
-
// Create a new event with the raw numeric value for the handler
|
|
77
69
|
const syntheticEvent = {
|
|
78
70
|
...event,
|
|
79
71
|
target: {
|
|
@@ -81,20 +73,14 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
|
|
|
81
73
|
value: rawValue
|
|
82
74
|
}
|
|
83
75
|
};
|
|
84
|
-
onChangeValueHandler
|
|
76
|
+
if (onChangeValueHandler) {
|
|
77
|
+
onChangeValueHandler(syntheticEvent);
|
|
78
|
+
}
|
|
85
79
|
}
|
|
86
80
|
};
|
|
87
81
|
const handleKeyDown = (event) => {
|
|
88
|
-
const allowedKeys = [
|
|
89
|
-
"Backspace",
|
|
90
|
-
"ArrowLeft",
|
|
91
|
-
"ArrowRight",
|
|
92
|
-
"Delete",
|
|
93
|
-
"Tab",
|
|
94
|
-
".",
|
|
95
|
-
];
|
|
82
|
+
const allowedKeys = ["Backspace", "ArrowLeft", "ArrowRight", "Delete", "Tab", "."];
|
|
96
83
|
const isNumber = /[0-9]/.test(event.key);
|
|
97
|
-
// Allow decimal point only if it's not already in the value
|
|
98
84
|
if (event.key === '.') {
|
|
99
85
|
const currentValue = event.target.value;
|
|
100
86
|
if (currentValue.includes('.')) {
|
|
@@ -111,8 +97,8 @@ const InputCurrency = (0, react_1.forwardRef)(({ placeholder, value, required, c
|
|
|
111
97
|
react_1.default.createElement("div", { className: "w-full relative", style: inlineCss },
|
|
112
98
|
react_1.default.createElement("div", { className: "w-full flex items-start justify-start gap-1 pb-1" },
|
|
113
99
|
react_1.default.createElement("p", { className: "text-sm text-text-color-primary font-medium" }, label),
|
|
114
|
-
react_1.default.createElement("span", { className: `text-red-primary ${required ? 'opacity-100' : 'opacity-0'}
|
|
115
|
-
react_1.default.createElement("div", { className: `input-style w-full h-full min-h-[40px] inline-flex relative items-center justify-between gap-x-1.5 text-sm font-semibold text-text-color-primary ${textError ===
|
|
100
|
+
react_1.default.createElement("span", { className: `text-red-primary ${required ? 'opacity-100' : 'opacity-0'}` }, "*")),
|
|
101
|
+
react_1.default.createElement("div", { className: `input-style w-full h-full min-h-[40px] inline-flex relative items-center justify-between gap-x-1.5 text-sm font-semibold text-text-color-primary ${!textError || textError.trim() === ''
|
|
116
102
|
? 'border-[2px] border-solid border-gray-tertiary'
|
|
117
103
|
: 'border-[2.5px] border-solid border-red-primary'}` },
|
|
118
104
|
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 pr-2", required: required }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const formatWithCommas: (
|
|
1
|
+
export declare const formatWithCommas: (value: string) => string;
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatWithCommas = void 0;
|
|
4
|
-
const formatWithCommas = (
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const formatWithCommas = (value) => {
|
|
5
|
+
if (!value)
|
|
6
|
+
return "";
|
|
7
|
+
// Remove any non-digit characters except decimal point
|
|
8
|
+
const numericValue = value.replace(/[^\d.]/g, "");
|
|
9
|
+
if (numericValue === "")
|
|
10
|
+
return "";
|
|
11
|
+
// Split the number into integer and decimal parts
|
|
12
|
+
const [integerPart, decimalPart] = numericValue.split(".");
|
|
13
|
+
// Format the integer part with commas
|
|
14
|
+
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
15
|
+
// Return the formatted value with decimal part if it exists
|
|
16
|
+
return decimalPart !== undefined
|
|
17
|
+
? `${formattedInteger}.${decimalPart}`
|
|
18
|
+
: formattedInteger;
|
|
7
19
|
};
|
|
8
20
|
exports.formatWithCommas = formatWithCommas;
|