@homebound/beam 2.258.0 → 2.259.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.
|
@@ -35,6 +35,7 @@ export interface NumberFieldProps extends Pick<PresentationFieldProps, "labelSty
|
|
|
35
35
|
borderless?: boolean;
|
|
36
36
|
sizeToContent?: boolean;
|
|
37
37
|
alwaysShowHelperText?: boolean;
|
|
38
|
+
positiveOnly?: boolean;
|
|
38
39
|
}
|
|
39
40
|
export declare function NumberField(props: NumberFieldProps): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
40
|
-
export declare function formatValue(value: number, factor: number, numFractionDigits: number | undefined, numIntegerDigits: number | undefined): number | undefined;
|
|
41
|
+
export declare function formatValue(value: number, factor: number, numFractionDigits: number | undefined, numIntegerDigits: number | undefined, positiveOnly?: boolean): number | undefined;
|
|
@@ -16,7 +16,7 @@ function NumberField(props) {
|
|
|
16
16
|
// Determine default alignment based on presentation context
|
|
17
17
|
const { fieldProps } = (0, PresentationContext_1.usePresentationContext)();
|
|
18
18
|
const alignment = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberAlignment) === "right" ? Css_1.Css.tr.jcfe.$ : Css_1.Css.tl.jcfs.$;
|
|
19
|
-
const { disabled, required, readOnly, type, label, onBlur, onFocus, errorMsg, helperText, value, onChange, xss, displayDirection = false, numFractionDigits = type === "dollars" ? 2 : undefined, truncate = false, onEnter, numberFormatOptions, numIntegerDigits, useGrouping = true, sizeToContent = false, ...otherProps } = props;
|
|
19
|
+
const { disabled, required, readOnly, type, label, onBlur, onFocus, errorMsg, helperText, value, onChange, xss, displayDirection = false, numFractionDigits = type === "dollars" ? 2 : undefined, truncate = false, onEnter, numberFormatOptions, numIntegerDigits, useGrouping = true, sizeToContent = false, positiveOnly = false, ...otherProps } = props;
|
|
20
20
|
const isDisabled = !!disabled;
|
|
21
21
|
const isReadOnly = !!readOnly;
|
|
22
22
|
const factor = type === "percent" || type === "cents" ? 100 : type === "basisPoints" ? 10000 : 1;
|
|
@@ -56,7 +56,7 @@ function NumberField(props) {
|
|
|
56
56
|
value: valueRef.current.wip ? valueRef.current.value : value === undefined ? Number.NaN : value / factor,
|
|
57
57
|
// // This is called on blur with the final/committed value.
|
|
58
58
|
onChange: (value) => {
|
|
59
|
-
onChange(formatValue(value, factor, numFractionDigits, numIntegerDigits));
|
|
59
|
+
onChange(formatValue(value, factor, numFractionDigits, numIntegerDigits, positiveOnly));
|
|
60
60
|
},
|
|
61
61
|
onFocus: () => {
|
|
62
62
|
valueRef.current = { wip: true, value: value === undefined ? Number.NaN : value / factor };
|
|
@@ -92,11 +92,11 @@ function NumberField(props) {
|
|
|
92
92
|
// This is called on each DOM change, to push the latest value into the field
|
|
93
93
|
onChange: (rawInputValue) => {
|
|
94
94
|
const parsedValue = numberParser.parse(rawInputValue || "");
|
|
95
|
-
onChange(formatValue(parsedValue, factor, numFractionDigits, numIntegerDigits));
|
|
95
|
+
onChange(formatValue(parsedValue, factor, numFractionDigits, numIntegerDigits, positiveOnly));
|
|
96
96
|
}, inputRef: inputRef, onBlur: onBlur, onFocus: onFocus, errorMsg: errorMsg, helperText: helperText, tooltip: (0, components_1.resolveTooltip)(disabled, undefined, readOnly), ...otherProps }));
|
|
97
97
|
}
|
|
98
98
|
exports.NumberField = NumberField;
|
|
99
|
-
function formatValue(value, factor, numFractionDigits, numIntegerDigits) {
|
|
99
|
+
function formatValue(value, factor, numFractionDigits, numIntegerDigits, positiveOnly = false) {
|
|
100
100
|
// We treat percents & cents as (mostly) integers, while useNumberField wants decimals, so
|
|
101
101
|
// undo that via `* factor` and `Math.round`, but also keep any specifically-requested `numFractionDigits`,
|
|
102
102
|
// i.e. for `type=percent value=12.34`, `value` will be `0.1234` that we want turn into `12.34`.
|
|
@@ -104,7 +104,7 @@ function formatValue(value, factor, numFractionDigits, numIntegerDigits) {
|
|
|
104
104
|
// Reverse the integer/decimal conversion
|
|
105
105
|
const decimalAdjusted = Number.isNaN(value)
|
|
106
106
|
? undefined
|
|
107
|
-
: Math.round(value * factor * maybeAdjustForDecimals) / maybeAdjustForDecimals;
|
|
107
|
+
: Math.round((positiveOnly ? Math.abs(value) : value) * factor * maybeAdjustForDecimals) / maybeAdjustForDecimals;
|
|
108
108
|
if (numIntegerDigits === undefined || decimalAdjusted === undefined) {
|
|
109
109
|
return decimalAdjusted;
|
|
110
110
|
}
|