@homebound/beam 2.188.0 → 2.190.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.
@@ -22,9 +22,15 @@ export interface NumberFieldProps {
22
22
  xss?: Xss<"textAlign" | "justifyContent">;
23
23
  displayDirection?: boolean;
24
24
  numFractionDigits?: number;
25
+ numIntegerDigits?: number;
25
26
  numberFormatOptions?: Intl.NumberFormatOptions;
26
27
  truncate?: boolean;
27
28
  onEnter?: VoidFunction;
29
+ placeholder?: string;
30
+ errorInTooltip?: true;
31
+ /** Whether to show comma separation for group numbers.
32
+ * @default true */
33
+ useGrouping?: boolean;
28
34
  }
29
35
  export declare function NumberField(props: NumberFieldProps): import("@emotion/react/jsx-runtime").JSX.Element;
30
- export declare function formatValue(value: number, factor: number, numFractionDigits: number | undefined): number | undefined;
36
+ export declare function formatValue(value: number, factor: number, numFractionDigits: number | undefined, numIntegerDigits: number | undefined): number | undefined;
@@ -15,31 +15,37 @@ function NumberField(props) {
15
15
  // Determine default alignment based on presentation context
16
16
  const { fieldProps } = (0, PresentationContext_1.usePresentationContext)();
17
17
  const alignment = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberAlignment) === "right" ? Css_1.Css.tr.jcfe.$ : Css_1.Css.tl.jcfs.$;
18
- const { disabled, required, readOnly, type, label, onBlur, onFocus, errorMsg, helperText, value, onChange, xss, displayDirection = false, numFractionDigits = type === "dollars" ? 2 : undefined, truncate = false, onEnter, ...otherProps } = props;
18
+ 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, ...otherProps } = props;
19
19
  const isDisabled = !!disabled;
20
20
  const isReadOnly = !!readOnly;
21
21
  const factor = type === "percent" || type === "cents" ? 100 : type === "basisPoints" ? 10000 : 1;
22
22
  const signDisplay = displayDirection ? "exceptZero" : "auto";
23
- const fractionFormatOptions = { [truncate ? "maximumFractionDigits" : "minimumFractionDigits"]: numFractionDigits };
23
+ const defaultFormatOptions = (0, react_1.useMemo)(() => ({
24
+ [truncate ? "maximumFractionDigits" : "minimumFractionDigits"]: numFractionDigits,
25
+ ...(numIntegerDigits !== undefined && { minimumIntegerDigits: numIntegerDigits }),
26
+ useGrouping,
27
+ signDisplay,
28
+ }), [truncate, numIntegerDigits, useGrouping, signDisplay]);
24
29
  const { locale } = (0, react_aria_1.useLocale)();
25
30
  // If formatOptions isn't memo'd, a useEffect in useNumberStateField will cause jank,
26
31
  // see: https://github.com/adobe/react-spectrum/issues/1893.
27
32
  const formatOptions = (0, react_1.useMemo)(() => {
28
- if (props.numberFormatOptions !== undefined) {
29
- return props.numberFormatOptions;
33
+ if (numberFormatOptions !== undefined) {
34
+ return numberFormatOptions;
30
35
  }
31
- return type === "percent"
32
- ? { style: "percent", signDisplay, ...fractionFormatOptions }
36
+ const typeFormat = type === "percent"
37
+ ? { style: "percent" }
33
38
  : type === "basisPoints"
34
- ? { style: "percent", minimumFractionDigits: 2, signDisplay }
39
+ ? { style: "percent", minimumFractionDigits: 2 }
35
40
  : type === "cents"
36
- ? { style: "currency", currency: "USD", minimumFractionDigits: 2, signDisplay }
41
+ ? { style: "currency", currency: "USD", minimumFractionDigits: 2 }
37
42
  : type === "dollars"
38
- ? { style: "currency", currency: "USD", minimumFractionDigits: numFractionDigits !== null && numFractionDigits !== void 0 ? numFractionDigits : 2, signDisplay }
43
+ ? { style: "currency", currency: "USD", minimumFractionDigits: numFractionDigits !== null && numFractionDigits !== void 0 ? numFractionDigits : 2 }
39
44
  : type === "days"
40
- ? { style: "unit", unit: "day", unitDisplay: "long", maximumFractionDigits: 0, signDisplay }
41
- : fractionFormatOptions;
42
- }, [type]);
45
+ ? { style: "unit", unit: "day", unitDisplay: "long", maximumFractionDigits: 0 }
46
+ : {};
47
+ return { ...defaultFormatOptions, ...typeFormat };
48
+ }, [type, numberFormatOptions]);
43
49
  const numberParser = (0, react_1.useMemo)(() => new number_1.NumberParser(locale, formatOptions), [locale, formatOptions]);
44
50
  const valueRef = (0, react_1.useRef)({ wip: false });
45
51
  // We can use this for both useNumberFieldState + useNumberField
@@ -49,7 +55,7 @@ function NumberField(props) {
49
55
  value: valueRef.current.wip ? valueRef.current.value : value === undefined ? Number.NaN : value / factor,
50
56
  // // This is called on blur with the final/committed value.
51
57
  onChange: (value) => {
52
- onChange(formatValue(value, factor, numFractionDigits));
58
+ onChange(formatValue(value, factor, numFractionDigits, numIntegerDigits));
53
59
  },
54
60
  onFocus: () => {
55
61
  valueRef.current = { wip: true, value: value === undefined ? Number.NaN : value / factor };
@@ -69,6 +75,7 @@ function NumberField(props) {
69
75
  isDisabled,
70
76
  isReadOnly,
71
77
  formatOptions,
78
+ ...otherProps,
72
79
  };
73
80
  const state = (0, react_stately_1.useNumberFieldState)(useProps);
74
81
  const inputRef = (0, react_1.useRef)(null);
@@ -82,16 +89,28 @@ function NumberField(props) {
82
89
  // This is called on each DOM change, to push the latest value into the field
83
90
  onChange: (rawInputValue) => {
84
91
  const parsedValue = numberParser.parse(rawInputValue || "");
85
- onChange(formatValue(parsedValue, factor, numFractionDigits));
92
+ onChange(formatValue(parsedValue, factor, numFractionDigits, numIntegerDigits));
86
93
  }, inputRef: inputRef, onBlur: onBlur, onFocus: onFocus, errorMsg: errorMsg, helperText: helperText, tooltip: (0, components_1.resolveTooltip)(disabled, undefined, readOnly) }, otherProps), void 0));
87
94
  }
88
95
  exports.NumberField = NumberField;
89
- function formatValue(value, factor, numFractionDigits) {
96
+ function formatValue(value, factor, numFractionDigits, numIntegerDigits) {
90
97
  // We treat percents & cents as (mostly) integers, while useNumberField wants decimals, so
91
98
  // undo that via `* factor` and `Math.round`, but also keep any specifically-requested `numFractionDigits`,
92
99
  // i.e. for `type=percent value=12.34`, `value` will be `0.1234` that we want turn into `12.34`.
93
100
  const maybeAdjustForDecimals = numFractionDigits ? Math.pow(10, numFractionDigits) : 1;
94
101
  // Reverse the integer/decimal conversion
95
- return Number.isNaN(value) ? undefined : Math.round(value * factor * maybeAdjustForDecimals) / maybeAdjustForDecimals;
102
+ const decimalAdjusted = Number.isNaN(value)
103
+ ? undefined
104
+ : Math.round(value * factor * maybeAdjustForDecimals) / maybeAdjustForDecimals;
105
+ if (numIntegerDigits === undefined || decimalAdjusted === undefined) {
106
+ return decimalAdjusted;
107
+ }
108
+ // If `numIntegerDigits` is defined, then we must truncate that manually, so that 1234.56 can turn into 34.56
109
+ const fractionalValue = Math.round((decimalAdjusted % 1) * maybeAdjustForDecimals) / maybeAdjustForDecimals;
110
+ const maybeNegate = decimalAdjusted < 0 ? -1 : 1;
111
+ const trimmedInteger = Number(Math.trunc(Math.abs(decimalAdjusted))
112
+ .toString()
113
+ .slice(-1 * numIntegerDigits));
114
+ return (trimmedInteger + fractionalValue) * maybeNegate;
96
115
  }
97
116
  exports.formatValue = formatValue;
@@ -2,7 +2,7 @@ import type { NumberFieldAria } from "@react-aria/numberfield";
2
2
  import { InputHTMLAttributes, LabelHTMLAttributes, MutableRefObject, ReactNode, TextareaHTMLAttributes } from "react";
3
3
  import { Only } from "../Css";
4
4
  import { BeamTextFieldProps, TextFieldXss } from "../interfaces";
5
- export interface TextFieldBaseProps<X> extends Pick<BeamTextFieldProps<X>, "label" | "required" | "errorMsg" | "onBlur" | "onFocus" | "helperText" | "hideLabel" | "placeholder" | "compact" | "borderless" | "visuallyDisabled" | "xss">, Partial<Pick<BeamTextFieldProps<X>, "onChange">> {
5
+ export interface TextFieldBaseProps<X> extends Pick<BeamTextFieldProps<X>, "label" | "required" | "errorMsg" | "errorInTooltip" | "onBlur" | "onFocus" | "helperText" | "hideLabel" | "placeholder" | "compact" | "borderless" | "visuallyDisabled" | "xss">, Partial<Pick<BeamTextFieldProps<X>, "onChange">> {
6
6
  labelProps?: LabelHTMLAttributes<HTMLLabelElement>;
7
7
  inputProps: InputHTMLAttributes<HTMLInputElement> | TextareaHTMLAttributes<HTMLTextAreaElement>;
8
8
  inputRef?: MutableRefObject<HTMLInputElement | HTMLTextAreaElement | null>;
@@ -17,9 +17,8 @@ const useTestIds_1 = require("../utils/useTestIds");
17
17
  function TextFieldBase(props) {
18
18
  var _a, _b, _c, _d, _e, _f, _g;
19
19
  const { fieldProps } = (0, PresentationContext_1.usePresentationContext)();
20
- const { label, required, labelProps, hideLabel = (_a = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.hideLabel) !== null && _a !== void 0 ? _a : false, inputProps, inputRef, inputWrapRef, groupProps, compact = (_b = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.compact) !== null && _b !== void 0 ? _b : false, errorMsg, helperText, multiline = false, onChange, onBlur, onFocus, xss, endAdornment, startAdornment, inlineLabel, contrast = false, borderless = (_c = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.borderless) !== null && _c !== void 0 ? _c : false, textAreaMinHeight = 96, clearable = false, tooltip, visuallyDisabled = (_d = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.visuallyDisabled) !== null && _d !== void 0 ? _d : true, } = props;
21
- const typeScale = (_e = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.typeScale) !== null && _e !== void 0 ? _e : (inputProps.readOnly && !hideLabel ? "smMd" : "sm");
22
- const errorInTooltip = (_f = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.errorInTooltip) !== null && _f !== void 0 ? _f : false;
20
+ const { label, required, labelProps, hideLabel = (_a = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.hideLabel) !== null && _a !== void 0 ? _a : false, inputProps, inputRef, inputWrapRef, groupProps, compact = (_b = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.compact) !== null && _b !== void 0 ? _b : false, errorMsg, helperText, multiline = false, onChange, onBlur, onFocus, xss, endAdornment, startAdornment, inlineLabel, contrast = false, borderless = (_c = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.borderless) !== null && _c !== void 0 ? _c : false, textAreaMinHeight = 96, clearable = false, tooltip, visuallyDisabled = (_d = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.visuallyDisabled) !== null && _d !== void 0 ? _d : true, errorInTooltip = (_e = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.errorInTooltip) !== null && _e !== void 0 ? _e : false, } = props;
21
+ const typeScale = (_f = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.typeScale) !== null && _f !== void 0 ? _f : (inputProps.readOnly && !hideLabel ? "smMd" : "sm");
23
22
  const internalProps = props.internalProps || {};
24
23
  const { compound = false, forceFocus = false, forceHover = false } = internalProps;
25
24
  const errorMessageId = `${inputProps.id}-error`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/beam",
3
- "version": "2.188.0",
3
+ "version": "2.190.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",