@stella-source/stellasource-dsl 0.1.1 → 0.1.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.
@@ -1,4 +1,4 @@
1
1
  import React from "react";
2
2
  import { NumberInputProps } from "./types";
3
- declare const NumberInput: ({ id, className, htmlProps, leftCategory, rightCategory, label, onChange, onBlur, onFocus, value, isRequired, maxLength, tooltipText, helperText, endIcon, placeholder, textAlign, isTruncated, helperTextAffectLayout, hideLabel, disabled, isLoading, hasError, thousandSeparator, decimalPlaces, ariaDescribedBy, }: NumberInputProps) => React.JSX.Element;
3
+ declare const NumberInput: ({ id, className, htmlProps, leftCategory, rightCategory, label, onChange, onBlur, onFocus, value, isRequired, maxLength, tooltipText, helperText, endIcon, placeholder, textAlign, isTruncated, helperTextAffectLayout, hideLabel, disabled, isLoading, hasError, thousandSeparator, decimalPlaces, allowNegativeNumbers, ariaDescribedBy, }: NumberInputProps) => React.JSX.Element;
4
4
  export default NumberInput;
@@ -27,6 +27,7 @@ export interface NumberInputProps {
27
27
  rightCategory?: `${TextFieldRightCategory}`;
28
28
  thousandSeparator?: boolean;
29
29
  decimalPlaces?: number;
30
+ allowNegativeNumbers?: boolean;
30
31
  /** for any extra text elements that need to be associated */
31
32
  ariaDescribedBy?: string;
32
33
  htmlProps?: HTMLAttributes<HTMLInputElement>;
@@ -17934,17 +17934,24 @@ const PasswordInput = ({ id, className, htmlProps, label, placeholder = "Enter P
17934
17934
  };
17935
17935
 
17936
17936
  const formatDynamicNumbersWithCommas = (num, decimalPlaces) => {
17937
- const onlyDigitNum = num.toString().replace(/[^0-9.]/g, "");
17937
+ const onlyDigitNum = num.toString().replace(/[^\d.-]/g, "");
17938
17938
  const decimalEnding = onlyDigitNum.endsWith(".");
17939
+ const decimalZero = onlyDigitNum.includes(".") && onlyDigitNum.endsWith("0");
17940
+ if (onlyDigitNum === "" || onlyDigitNum === "-" || onlyDigitNum === ".") {
17941
+ return onlyDigitNum;
17942
+ }
17943
+ if (isNaN(Number(onlyDigitNum))) {
17944
+ return "";
17945
+ }
17939
17946
  return onlyDigitNum
17940
17947
  ? `${Number(onlyDigitNum).toLocaleString("en-US", {
17941
17948
  minimumFractionDigits: 0,
17942
17949
  maximumFractionDigits: decimalPlaces,
17943
- })}${decimalEnding ? "." : ""}`
17950
+ })}${decimalEnding ? "." : ""}${decimalZero ? "0" : ""}`
17944
17951
  : "";
17945
17952
  };
17946
17953
 
17947
- const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, label, onChange, onBlur, onFocus, value, isRequired, maxLength, tooltipText, helperText, endIcon, placeholder = "0", textAlign = "left", isTruncated = false, helperTextAffectLayout = true, hideLabel = false, disabled = false, isLoading = false, hasError = false, thousandSeparator = true, decimalPlaces = 0, ariaDescribedBy, }) => {
17954
+ const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, label, onChange, onBlur, onFocus, value, isRequired, maxLength, tooltipText, helperText, endIcon, placeholder = "0", textAlign = "left", isTruncated = false, helperTextAffectLayout = true, hideLabel = false, disabled = false, isLoading = false, hasError = false, thousandSeparator = true, decimalPlaces = 0, allowNegativeNumbers = false, ariaDescribedBy, }) => {
17948
17955
  const containerProps = {
17949
17956
  id,
17950
17957
  label,
@@ -17968,16 +17975,21 @@ const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, la
17968
17975
  if (e.key === "," ||
17969
17976
  (e.key === "." && decimalPlaces === 0) ||
17970
17977
  (e.key === "." && inputValue.includes(".")) ||
17971
- (inputValue.includes(".") &&
17972
- inputValue.split(".")[1]?.length >= decimalPlaces)) {
17978
+ (e.key === "-" && !allowNegativeNumbers) ||
17979
+ (e.key === "-" && inputValue.includes("-"))) {
17973
17980
  e.preventDefault();
17974
17981
  }
17975
17982
  };
17976
17983
  const handleChange = (e) => {
17977
17984
  const value = e.target.value;
17978
- if (e.currentTarget.checkValidity()) {
17985
+ const inputDecimalPlaces = value.includes(".")
17986
+ ? value.split(".")[1].length
17987
+ : 0;
17988
+ if (e.currentTarget.checkValidity() &&
17989
+ inputDecimalPlaces <= decimalPlaces) {
17990
+ console.log(value);
17979
17991
  setInputValue(value);
17980
- onChange(value.toString().replace(/[^0-9.]/g, ""));
17992
+ onChange(value.toString().replace(/[^\d.-]/g, ""));
17981
17993
  }
17982
17994
  };
17983
17995
  const handleBlur = (e) => {
@@ -17990,14 +18002,17 @@ const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, la
17990
18002
  !onFocus && setPlaceholderText("");
17991
18003
  };
17992
18004
  return (a__default.createElement(TextFieldContainer, { ...containerProps },
17993
- a__default.createElement(TextFieldInput, { "aria-describedby": `${id}-helper-text ${ariaDescribedBy}`, type: "text", inputMode: "numeric", pattern: "^[\\d,.]*$", id: `${id}-text-field`, disabled: disabled || isLoading, placeholder: placeholderText, onChange: handleChange, onBlur: handleBlur, onFocus: handleFocus, onKeyDown: handleKeyDown, value: thousandSeparator
18005
+ a__default.createElement(TextFieldInput, { "aria-describedby": `${id}-helper-text ${ariaDescribedBy}`, type: "text", inputMode: "numeric",
18006
+ // prettier-ignore
18007
+ pattern: allowNegativeNumbers ? "^[\\d,\\.\\-]*$" : "^[\\d,\\.]*$", id: `${id}-text-field`, disabled: disabled || isLoading, placeholder: placeholderText, onChange: handleChange, onBlur: handleBlur, onFocus: handleFocus, onKeyDown: handleKeyDown, value: thousandSeparator
17994
18008
  ? formatDynamicNumbersWithCommas(valueToUse, decimalPlaces)
17995
18009
  : valueToUse, maxLength: maxLength &&
17996
18010
  maxLength +
17997
18011
  (thousandSeparator
17998
18012
  ? Math.floor(valueToUse.toString().split(".")[0].length / 4)
17999
18013
  : 0) +
18000
- (inputValue.includes(".") ? 1 : 0), "$isTruncated": isTruncated, "$textAlign": textAlign, "$isDisabled": disabled, "$isLoading": isLoading, ...htmlProps })));
18014
+ (inputValue.includes(".") ? 1 : 0) +
18015
+ (inputValue.includes("-") ? 1 : 0), "$isTruncated": isTruncated, "$textAlign": textAlign, "$isDisabled": disabled, "$isLoading": isLoading, ...htmlProps })));
18001
18016
  };
18002
18017
 
18003
18018
  export { HelperText, IconButton, Label, Loader, NumberInput, PasswordInput, StellaButton, StellaButtonVariant, StellaIcon, Styles, TextInput, Tooltip };