@steroidsjs/core 3.1.7 → 3.1.8
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/package.json
CHANGED
|
@@ -26,6 +26,7 @@ var fieldWrapper_1 = __importDefault(require("../Field/fieldWrapper"));
|
|
|
26
26
|
var DEFAULT_STEP = 1;
|
|
27
27
|
var DECIMAL_SEPARATOR = '.';
|
|
28
28
|
var MINUS_SIGN = '-';
|
|
29
|
+
var NUMBER_PATTERN = /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
|
|
29
30
|
var isNotEmptyValue = function (rawValue) { return !(0, isNil_1["default"])(rawValue) && rawValue !== ''; };
|
|
30
31
|
function NumberField(props) {
|
|
31
32
|
var _a;
|
|
@@ -60,14 +61,32 @@ function NumberField(props) {
|
|
|
60
61
|
}
|
|
61
62
|
}, [currentInputRef, maskedInputRef]);
|
|
62
63
|
(0, react_2.useEffect)(function () {
|
|
64
|
+
var _a, _b;
|
|
63
65
|
var input = currentInputRef.current;
|
|
64
66
|
var currentValue = props.input.value;
|
|
65
67
|
var hasValue = isNotEmptyValue(currentValue);
|
|
66
|
-
var
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
var stringValue = String(currentValue);
|
|
69
|
+
// Remove visual formatting before checking the numeric value.
|
|
70
|
+
var normalizedValue = props.thousandSeparator
|
|
71
|
+
? stringValue.split(props.thousandSeparator).join('')
|
|
72
|
+
: stringValue;
|
|
73
|
+
var numberValue = Number(normalizedValue);
|
|
74
|
+
var decimalPart = (_a = normalizedValue.split(DECIMAL_SEPARATOR)[1]) !== null && _a !== void 0 ? _a : '';
|
|
75
|
+
var maxPrecision = (_b = props.decimal) !== null && _b !== void 0 ? _b : 0;
|
|
76
|
+
// By default only integers are valid; decimal defines the maximum precision.
|
|
77
|
+
var hasValidPrecision = maxPrecision > 0
|
|
78
|
+
? decimalPart.length <= maxPrecision
|
|
79
|
+
: !normalizedValue.includes(DECIMAL_SEPARATOR);
|
|
80
|
+
// Required only controls empty values. Every filled field must pass all numeric checks.
|
|
81
|
+
var isValid = hasValue
|
|
82
|
+
? NUMBER_PATTERN.test(normalizedValue)
|
|
83
|
+
&& ((0, isNil_1["default"])(props.min) || numberValue >= props.min)
|
|
84
|
+
&& ((0, isNil_1["default"])(props.max) || numberValue <= props.max)
|
|
85
|
+
&& hasValidPrecision
|
|
86
|
+
&& (props.isCanBeNegative || numberValue >= 0)
|
|
87
|
+
: !props.required;
|
|
69
88
|
input.setCustomValidity(isValid ? '' : __('The number is not valid.'));
|
|
70
|
-
}, [currentInputRef, props.required, props.min, props.max, props.
|
|
89
|
+
}, [currentInputRef, props.required, props.min, props.max, props.decimal, props.isCanBeNegative, props.thousandSeparator, props.input.value]);
|
|
71
90
|
var clampToMinMax = (0, react_2.useCallback)(function (rawValue) {
|
|
72
91
|
var val = rawValue;
|
|
73
92
|
if (val < props.min) {
|