@inseefr/lunatic 3.5.2 → 3.5.3
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/components/InputNumber/InputNumberThousand.js +10 -5
- package/components/InputNumber/InputNumberThousand.js.map +1 -1
- package/esm/components/InputNumber/InputNumberThousand.js +10 -5
- package/esm/components/InputNumber/InputNumberThousand.js.map +1 -1
- package/esm/utils/number.d.ts +0 -4
- package/esm/utils/number.js +0 -10
- package/esm/utils/number.js.map +1 -1
- package/esm/utils/number.spec.js +1 -44
- package/esm/utils/number.spec.js.map +1 -1
- package/package.json +1 -1
- package/src/components/InputNumber/InputNumberThousand.tsx +9 -6
- package/src/utils/number.spec.ts +1 -45
- package/src/utils/number.ts +0 -18
- package/tsconfig.build.tsbuildinfo +1 -1
- package/utils/number.d.ts +0 -4
- package/utils/number.js +0 -11
- package/utils/number.js.map +1 -1
- package/utils/number.spec.js +0 -43
- package/utils/number.spec.js.map +1 -1
|
@@ -9,8 +9,7 @@ const classnames_1 = __importDefault(require("classnames"));
|
|
|
9
9
|
const react_1 = require("react");
|
|
10
10
|
const react_number_format_1 = require("react-number-format");
|
|
11
11
|
const i18n_1 = require("../../i18n");
|
|
12
|
-
const
|
|
13
|
-
const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required, labelId, min, max, decimals, invalid, unit, }) => {
|
|
12
|
+
const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required, labelId, min = -Infinity, max = Infinity, decimals, invalid, unit, }) => {
|
|
14
13
|
const handleChange = (0, react_1.useCallback)(function (e) {
|
|
15
14
|
const val = e.floatValue ?? null;
|
|
16
15
|
onChange(Number.isNaN(val) ? null : val);
|
|
@@ -20,8 +19,14 @@ const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required
|
|
|
20
19
|
// we accept empty value
|
|
21
20
|
if (floatValue === undefined)
|
|
22
21
|
return true;
|
|
23
|
-
//
|
|
24
|
-
|
|
22
|
+
// if both min & max are negative, accept only negative above min
|
|
23
|
+
if (min < 0 && max < 0)
|
|
24
|
+
return floatValue >= min && floatValue < 0;
|
|
25
|
+
// if both min & max are positive, accept only positive below max
|
|
26
|
+
if (min > 0 && max > 0)
|
|
27
|
+
return floatValue <= max && floatValue > 0;
|
|
28
|
+
// if min & max have different sign or equal to 0, check if value is within the min-max range
|
|
29
|
+
return floatValue >= min && floatValue <= max;
|
|
25
30
|
}, [min, max]);
|
|
26
31
|
// we want to display the user input and its unit on hover
|
|
27
32
|
let title;
|
|
@@ -29,7 +34,7 @@ const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required
|
|
|
29
34
|
const unitTitle = unit ? ` ${unit}` : '';
|
|
30
35
|
title = `${value.toLocaleString()}${unitTitle}`;
|
|
31
36
|
}
|
|
32
|
-
return ((0, jsx_runtime_1.jsx)(react_number_format_1.NumericFormat, { id: id, className: (0, classnames_1.default)({ disabled }), onValueChange: handleChange, value: value ?? '', title: title, "aria-labelledby": labelId, disabled: disabled, readOnly: readOnly, required: required, lang: "en", isAllowed: isAllowed, allowNegative: min
|
|
37
|
+
return ((0, jsx_runtime_1.jsx)(react_number_format_1.NumericFormat, { id: id, className: (0, classnames_1.default)({ disabled }), onValueChange: handleChange, value: value ?? '', title: title, "aria-labelledby": labelId, disabled: disabled, readOnly: readOnly, required: required, lang: "en", isAllowed: isAllowed, allowNegative: min < 0, allowedDecimalSeparators: i18n_1.inputNumberPropsI18N.allDecimalSeparators, decimalSeparator: i18n_1.inputNumberPropsI18N.decimalSeparator, decimalScale: decimals, allowLeadingZeros: true, thousandSeparator: i18n_1.inputNumberPropsI18N.thousandSeparator, inputMode: decimals ? 'decimal' : 'numeric', "aria-invalid": invalid, onBlur: (e) => {
|
|
33
38
|
e.target.setSelectionRange(0, 0);
|
|
34
39
|
} }));
|
|
35
40
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InputNumberThousand.js","sourceRoot":"","sources":["../../src/components/InputNumber/InputNumberThousand.tsx"],"names":[],"mappings":";;;;;;;AAAA,4DAAoC;AACpC,iCAAoC;AACpC,6DAI6B;AAC7B,qCAAkD;
|
|
1
|
+
{"version":3,"file":"InputNumberThousand.js","sourceRoot":"","sources":["../../src/components/InputNumber/InputNumberThousand.tsx"],"names":[],"mappings":";;;;;;;AAAA,4DAAoC;AACpC,iCAAoC;AACpC,6DAI6B;AAC7B,qCAAkD;AAiB3C,MAAM,mBAAmB,GAAG,CAAC,EACnC,EAAE,EACF,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,GAAG,GAAG,CAAC,QAAQ,EACf,GAAG,GAAG,QAAQ,EACd,QAAQ,EACR,OAAO,EACP,IAAI,GACG,EAAE,EAAE;IACX,MAAM,YAAY,GAAG,IAAA,mBAAW,EAC/B,UAAU,CAAC;QACV,MAAM,GAAG,GAAG,CAAC,CAAC,UAAU,IAAI,IAAI,CAAC;QACjC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC1C,CAAC,EACD,CAAC,QAAQ,CAAC,CACV,CAAC;IAEF,MAAM,SAAS,GAAG,IAAA,mBAAW,EAC5B,CAAC,MAA0B,EAAE,EAAE;QAC9B,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;QAC9B,wBAAwB;QACxB,IAAI,UAAU,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC;QAC1C,iEAAiE;QACjE,IAAI,GAAG,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC;YAAE,OAAO,UAAU,IAAI,GAAG,IAAI,UAAU,GAAG,CAAC,CAAC;QACnE,iEAAiE;QACjE,IAAI,GAAG,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC;YAAE,OAAO,UAAU,IAAI,GAAG,IAAI,UAAU,GAAG,CAAC,CAAC;QACnE,6FAA6F;QAC7F,OAAO,UAAU,IAA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 import classNames from 'classnames';
|
|
|
3
3
|
import { useCallback } from 'react';
|
|
4
4
|
import { NumericFormat, } from 'react-number-format';
|
|
5
5
|
import { inputNumberPropsI18N } from '../../i18n';
|
|
6
|
-
|
|
7
|
-
export const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required, labelId, min, max, decimals, invalid, unit, }) => {
|
|
6
|
+
export const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, required, labelId, min = -Infinity, max = Infinity, decimals, invalid, unit, }) => {
|
|
8
7
|
const handleChange = useCallback(function (e) {
|
|
9
8
|
var _a;
|
|
10
9
|
const val = (_a = e.floatValue) !== null && _a !== void 0 ? _a : null;
|
|
@@ -15,8 +14,14 @@ export const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, r
|
|
|
15
14
|
// we accept empty value
|
|
16
15
|
if (floatValue === undefined)
|
|
17
16
|
return true;
|
|
18
|
-
//
|
|
19
|
-
|
|
17
|
+
// if both min & max are negative, accept only negative above min
|
|
18
|
+
if (min < 0 && max < 0)
|
|
19
|
+
return floatValue >= min && floatValue < 0;
|
|
20
|
+
// if both min & max are positive, accept only positive below max
|
|
21
|
+
if (min > 0 && max > 0)
|
|
22
|
+
return floatValue <= max && floatValue > 0;
|
|
23
|
+
// if min & max have different sign or equal to 0, check if value is within the min-max range
|
|
24
|
+
return floatValue >= min && floatValue <= max;
|
|
20
25
|
}, [min, max]);
|
|
21
26
|
// we want to display the user input and its unit on hover
|
|
22
27
|
let title;
|
|
@@ -24,7 +29,7 @@ export const InputNumberThousand = ({ id, onChange, value, disabled, readOnly, r
|
|
|
24
29
|
const unitTitle = unit ? ` ${unit}` : '';
|
|
25
30
|
title = `${value.toLocaleString()}${unitTitle}`;
|
|
26
31
|
}
|
|
27
|
-
return (_jsx(NumericFormat, { id: id, className: classNames({ disabled }), onValueChange: handleChange, value: value !== null && value !== void 0 ? value : '', title: title, "aria-labelledby": labelId, disabled: disabled, readOnly: readOnly, required: required, lang: "en", isAllowed: isAllowed, allowNegative: min
|
|
32
|
+
return (_jsx(NumericFormat, { id: id, className: classNames({ disabled }), onValueChange: handleChange, value: value !== null && value !== void 0 ? value : '', title: title, "aria-labelledby": labelId, disabled: disabled, readOnly: readOnly, required: required, lang: "en", isAllowed: isAllowed, allowNegative: min < 0, allowedDecimalSeparators: inputNumberPropsI18N.allDecimalSeparators, decimalSeparator: inputNumberPropsI18N.decimalSeparator, decimalScale: decimals, allowLeadingZeros: true, thousandSeparator: inputNumberPropsI18N.thousandSeparator, inputMode: decimals ? 'decimal' : 'numeric', "aria-invalid": invalid, onBlur: (e) => {
|
|
28
33
|
e.target.setSelectionRange(0, 0);
|
|
29
34
|
} }));
|
|
30
35
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InputNumberThousand.js","sourceRoot":"","sources":["../../../src/components/InputNumber/InputNumberThousand.tsx"],"names":[],"mappings":";AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EACN,aAAa,GAGb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"InputNumberThousand.js","sourceRoot":"","sources":["../../../src/components/InputNumber/InputNumberThousand.tsx"],"names":[],"mappings":";AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EACN,aAAa,GAGb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAiBlD,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EACnC,EAAE,EACF,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,GAAG,GAAG,CAAC,QAAQ,EACf,GAAG,GAAG,QAAQ,EACd,QAAQ,EACR,OAAO,EACP,IA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U,CAAC,EAAE,QAAQ,EAAE,CAAC,EACnC,aAAa,EAAE,YAAY,EAC3B,KAAK,EAAE,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,EAClB,KAAK,EAAE,KAAK,qBACK,OAAO,EACxB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAC,IAAI,EACT,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,GAAG,GAAG,CAAC,EACtB,wBAAwB,EAAE,oBAAoB,CAAC,oBAAoB,EACnE,gBAAgB,EAAE,oBAAoB,CAAC,gBAAgB,EACvD,YAAY,EAAE,QAAQ,EACtB,iBAAiB,QACjB,iBAAiB,EAAE,oBAAoB,CAAC,iBAAiB,EACzD,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,kBAC7B,OAAO,EACrB,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE;YACb,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAClC,CAAC,GACA,CACF,CAAC;AACH,CAAC,CAAC"}
|
package/esm/utils/number.d.ts
CHANGED
|
@@ -7,7 +7,3 @@ export declare function between(n: number, min: number, max: number): number;
|
|
|
7
7
|
* Check if the value is a number
|
|
8
8
|
*/
|
|
9
9
|
export declare function isNumber(n: unknown): n is number;
|
|
10
|
-
/** Check if a value is within an interval.
|
|
11
|
-
* Both minimum and maximum can be undefined, then we just compare with the defined bounds.
|
|
12
|
-
*/
|
|
13
|
-
export declare function isNumberInInterval(value: number, min?: number, max?: number): boolean;
|
package/esm/utils/number.js
CHANGED
|
@@ -29,14 +29,4 @@ export function between(n, min, max) {
|
|
|
29
29
|
export function isNumber(n) {
|
|
30
30
|
return typeof n === 'number' && Number.isFinite(n);
|
|
31
31
|
}
|
|
32
|
-
/** Check if a value is within an interval.
|
|
33
|
-
* Both minimum and maximum can be undefined, then we just compare with the defined bounds.
|
|
34
|
-
*/
|
|
35
|
-
export function isNumberInInterval(value, min, max) {
|
|
36
|
-
if ((min !== undefined && value < min) ||
|
|
37
|
-
(max !== undefined && value > max)) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
32
|
//# sourceMappingURL=number.js.map
|
package/esm/utils/number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sourceRoot":"","sources":["../../src/utils/number.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,CAAU;IAClC,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3B,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC/B,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IACD,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,CAAC,CAAC;IACV,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,eAAe,OAAO,CAAC,SAAS,CAAC,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,CAAS,EAAE,GAAW,EAAE,GAAW;IAC1D,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC;IACZ,CAAC;IACD,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC;IACZ,CAAC;IACD,OAAO,CAAC,CAAC;AACV,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAU;IAClC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC
|
|
1
|
+
{"version":3,"file":"number.js","sourceRoot":"","sources":["../../src/utils/number.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,CAAU;IAClC,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3B,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC/B,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IACD,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,CAAC,CAAC;IACV,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,eAAe,OAAO,CAAC,SAAS,CAAC,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,CAAS,EAAE,GAAW,EAAE,GAAW;IAC1D,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC;IACZ,CAAC;IACD,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC;IACZ,CAAC;IACD,OAAO,CAAC,CAAC;AACV,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAU;IAClC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC"}
|
package/esm/utils/number.spec.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { between
|
|
2
|
+
import { between } from './number';
|
|
3
3
|
describe('between', () => {
|
|
4
4
|
it('should work', () => {
|
|
5
5
|
expect(between(0, -5, 100)).toBe(0);
|
|
@@ -7,47 +7,4 @@ describe('between', () => {
|
|
|
7
7
|
expect(between(-23.22, -5, 100)).toBe(-5);
|
|
8
8
|
});
|
|
9
9
|
});
|
|
10
|
-
describe('isNumberInInterval', () => {
|
|
11
|
-
it('allows value within range', () => {
|
|
12
|
-
expect(isNumberInInterval(25, 10, 50)).toBe(true);
|
|
13
|
-
});
|
|
14
|
-
it('disallows value above max', () => {
|
|
15
|
-
expect(isNumberInInterval(55, 10, 50)).toBe(false);
|
|
16
|
-
});
|
|
17
|
-
it('disallows value below min', () => {
|
|
18
|
-
expect(isNumberInInterval(5, 10, 50)).toBe(false);
|
|
19
|
-
});
|
|
20
|
-
it('allows negative within bounds', () => {
|
|
21
|
-
expect(isNumberInInterval(-4, -5, 0)).toBe(true);
|
|
22
|
-
});
|
|
23
|
-
it('disallows negative if min is 0 or positive', () => {
|
|
24
|
-
expect(isNumberInInterval(-1, 0, 10)).toBe(false);
|
|
25
|
-
expect(isNumberInInterval(-1, 5, 10)).toBe(false);
|
|
26
|
-
});
|
|
27
|
-
it('allows only value above min when max is undefined', () => {
|
|
28
|
-
expect(isNumberInInterval(999, 100)).toBe(true);
|
|
29
|
-
expect(isNumberInInterval(50, 100)).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
it('allows only value below max when min is undefined', () => {
|
|
32
|
-
expect(isNumberInInterval(50, undefined, 100)).toBe(true);
|
|
33
|
-
// accepts negative values
|
|
34
|
-
expect(isNumberInInterval(-50, undefined, 100)).toBe(true);
|
|
35
|
-
expect(isNumberInInterval(150, undefined, 100)).toBe(false);
|
|
36
|
-
});
|
|
37
|
-
it('allows decimal value within range', () => {
|
|
38
|
-
expect(isNumberInInterval(9.99, 0, 10)).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
it('disallows decimal value above max', () => {
|
|
41
|
-
expect(isNumberInInterval(10.01, 0, 10)).toBe(false);
|
|
42
|
-
});
|
|
43
|
-
it('disallows decimal value below min', () => {
|
|
44
|
-
expect(isNumberInInterval(-5.01, -5, 5)).toBe(false);
|
|
45
|
-
});
|
|
46
|
-
it('allows decimal value equal to max', () => {
|
|
47
|
-
expect(isNumberInInterval(10.0, 0, 10)).toBe(true);
|
|
48
|
-
});
|
|
49
|
-
it('allows decimal value equal to min', () => {
|
|
50
|
-
expect(isNumberInInterval(-5.0, -5, 5)).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
10
|
//# sourceMappingURL=number.spec.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.spec.js","sourceRoot":"","sources":["../../src/utils/number.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"number.spec.js","sourceRoot":"","sources":["../../src/utils/number.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;IACxB,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE;QACtB,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
type OnValueChange,
|
|
7
7
|
} from 'react-number-format';
|
|
8
8
|
import { inputNumberPropsI18N } from '../../i18n';
|
|
9
|
-
import { isNumberInInterval } from '../../utils/number';
|
|
10
9
|
|
|
11
10
|
type Props = {
|
|
12
11
|
id?: string;
|
|
@@ -31,8 +30,8 @@ export const InputNumberThousand = ({
|
|
|
31
30
|
readOnly,
|
|
32
31
|
required,
|
|
33
32
|
labelId,
|
|
34
|
-
min,
|
|
35
|
-
max,
|
|
33
|
+
min = -Infinity,
|
|
34
|
+
max = Infinity,
|
|
36
35
|
decimals,
|
|
37
36
|
invalid,
|
|
38
37
|
unit,
|
|
@@ -50,8 +49,12 @@ export const InputNumberThousand = ({
|
|
|
50
49
|
const { floatValue } = values;
|
|
51
50
|
// we accept empty value
|
|
52
51
|
if (floatValue === undefined) return true;
|
|
53
|
-
//
|
|
54
|
-
return
|
|
52
|
+
// if both min & max are negative, accept only negative above min
|
|
53
|
+
if (min < 0 && max < 0) return floatValue >= min && floatValue < 0;
|
|
54
|
+
// if both min & max are positive, accept only positive below max
|
|
55
|
+
if (min > 0 && max > 0) return floatValue <= max && floatValue > 0;
|
|
56
|
+
// if min & max have different sign or equal to 0, check if value is within the min-max range
|
|
57
|
+
return floatValue >= min && floatValue <= max;
|
|
55
58
|
},
|
|
56
59
|
[min, max]
|
|
57
60
|
);
|
|
@@ -76,7 +79,7 @@ export const InputNumberThousand = ({
|
|
|
76
79
|
required={required}
|
|
77
80
|
lang="en"
|
|
78
81
|
isAllowed={isAllowed}
|
|
79
|
-
allowNegative={min
|
|
82
|
+
allowNegative={min < 0}
|
|
80
83
|
allowedDecimalSeparators={inputNumberPropsI18N.allDecimalSeparators}
|
|
81
84
|
decimalSeparator={inputNumberPropsI18N.decimalSeparator}
|
|
82
85
|
decimalScale={decimals}
|
package/src/utils/number.spec.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { between
|
|
2
|
+
import { between } from './number';
|
|
3
3
|
|
|
4
4
|
describe('between', () => {
|
|
5
5
|
it('should work', () => {
|
|
@@ -8,47 +8,3 @@ describe('between', () => {
|
|
|
8
8
|
expect(between(-23.22, -5, 100)).toBe(-5);
|
|
9
9
|
});
|
|
10
10
|
});
|
|
11
|
-
|
|
12
|
-
describe('isNumberInInterval', () => {
|
|
13
|
-
it('allows value within range', () => {
|
|
14
|
-
expect(isNumberInInterval(25, 10, 50)).toBe(true);
|
|
15
|
-
});
|
|
16
|
-
it('disallows value above max', () => {
|
|
17
|
-
expect(isNumberInInterval(55, 10, 50)).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
it('disallows value below min', () => {
|
|
20
|
-
expect(isNumberInInterval(5, 10, 50)).toBe(false);
|
|
21
|
-
});
|
|
22
|
-
it('allows negative within bounds', () => {
|
|
23
|
-
expect(isNumberInInterval(-4, -5, 0)).toBe(true);
|
|
24
|
-
});
|
|
25
|
-
it('disallows negative if min is 0 or positive', () => {
|
|
26
|
-
expect(isNumberInInterval(-1, 0, 10)).toBe(false);
|
|
27
|
-
expect(isNumberInInterval(-1, 5, 10)).toBe(false);
|
|
28
|
-
});
|
|
29
|
-
it('allows only value above min when max is undefined', () => {
|
|
30
|
-
expect(isNumberInInterval(999, 100)).toBe(true);
|
|
31
|
-
expect(isNumberInInterval(50, 100)).toBe(false);
|
|
32
|
-
});
|
|
33
|
-
it('allows only value below max when min is undefined', () => {
|
|
34
|
-
expect(isNumberInInterval(50, undefined, 100)).toBe(true);
|
|
35
|
-
// accepts negative values
|
|
36
|
-
expect(isNumberInInterval(-50, undefined, 100)).toBe(true);
|
|
37
|
-
expect(isNumberInInterval(150, undefined, 100)).toBe(false);
|
|
38
|
-
});
|
|
39
|
-
it('allows decimal value within range', () => {
|
|
40
|
-
expect(isNumberInInterval(9.99, 0, 10)).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
it('disallows decimal value above max', () => {
|
|
43
|
-
expect(isNumberInInterval(10.01, 0, 10)).toBe(false);
|
|
44
|
-
});
|
|
45
|
-
it('disallows decimal value below min', () => {
|
|
46
|
-
expect(isNumberInInterval(-5.01, -5, 5)).toBe(false);
|
|
47
|
-
});
|
|
48
|
-
it('allows decimal value equal to max', () => {
|
|
49
|
-
expect(isNumberInInterval(10.0, 0, 10)).toBe(true);
|
|
50
|
-
});
|
|
51
|
-
it('allows decimal value equal to min', () => {
|
|
52
|
-
expect(isNumberInInterval(-5.0, -5, 5)).toBe(true);
|
|
53
|
-
});
|
|
54
|
-
});
|
package/src/utils/number.ts
CHANGED
|
@@ -31,21 +31,3 @@ export function between(n: number, min: number, max: number): number {
|
|
|
31
31
|
export function isNumber(n: unknown): n is number {
|
|
32
32
|
return typeof n === 'number' && Number.isFinite(n);
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
/** Check if a value is within an interval.
|
|
36
|
-
* Both minimum and maximum can be undefined, then we just compare with the defined bounds.
|
|
37
|
-
*/
|
|
38
|
-
export function isNumberInInterval(
|
|
39
|
-
value: number,
|
|
40
|
-
min?: number,
|
|
41
|
-
max?: number
|
|
42
|
-
): boolean {
|
|
43
|
-
if (
|
|
44
|
-
(min !== undefined && value < min) ||
|
|
45
|
-
(max !== undefined && value > max)
|
|
46
|
-
) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return true;
|
|
51
|
-
}
|