@stella-source/stellasource-dsl 0.1.1 → 0.1.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.
@@ -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>;
@@ -17791,6 +17791,7 @@ const Wrapper = dt.div `
17791
17791
  min-width: 60px;
17792
17792
  max-width: 400px;
17793
17793
  position: relative;
17794
+ width: 100%;
17794
17795
  `;
17795
17796
  const TextFieldWrapper = dt.div.attrs({
17796
17797
  className: "DSL-stella-textfield",
@@ -17934,17 +17935,24 @@ const PasswordInput = ({ id, className, htmlProps, label, placeholder = "Enter P
17934
17935
  };
17935
17936
 
17936
17937
  const formatDynamicNumbersWithCommas = (num, decimalPlaces) => {
17937
- const onlyDigitNum = num.toString().replace(/[^0-9.]/g, "");
17938
+ const onlyDigitNum = num.toString().replace(/[^\d.-]/g, "");
17938
17939
  const decimalEnding = onlyDigitNum.endsWith(".");
17940
+ const decimalZero = onlyDigitNum.includes(".") && onlyDigitNum.endsWith("0");
17941
+ if (onlyDigitNum === "" || onlyDigitNum === "-" || onlyDigitNum === ".") {
17942
+ return onlyDigitNum;
17943
+ }
17944
+ if (isNaN(Number(onlyDigitNum))) {
17945
+ return "";
17946
+ }
17939
17947
  return onlyDigitNum
17940
17948
  ? `${Number(onlyDigitNum).toLocaleString("en-US", {
17941
17949
  minimumFractionDigits: 0,
17942
17950
  maximumFractionDigits: decimalPlaces,
17943
- })}${decimalEnding ? "." : ""}`
17951
+ })}${decimalEnding ? "." : ""}${decimalZero ? "0" : ""}`
17944
17952
  : "";
17945
17953
  };
17946
17954
 
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, }) => {
17955
+ 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
17956
  const containerProps = {
17949
17957
  id,
17950
17958
  label,
@@ -17968,16 +17976,21 @@ const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, la
17968
17976
  if (e.key === "," ||
17969
17977
  (e.key === "." && decimalPlaces === 0) ||
17970
17978
  (e.key === "." && inputValue.includes(".")) ||
17971
- (inputValue.includes(".") &&
17972
- inputValue.split(".")[1]?.length >= decimalPlaces)) {
17979
+ (e.key === "-" && !allowNegativeNumbers) ||
17980
+ (e.key === "-" && inputValue.includes("-"))) {
17973
17981
  e.preventDefault();
17974
17982
  }
17975
17983
  };
17976
17984
  const handleChange = (e) => {
17977
17985
  const value = e.target.value;
17978
- if (e.currentTarget.checkValidity()) {
17986
+ const inputDecimalPlaces = value.includes(".")
17987
+ ? value.split(".")[1].length
17988
+ : 0;
17989
+ if (e.currentTarget.checkValidity() &&
17990
+ inputDecimalPlaces <= decimalPlaces) {
17991
+ console.log(value);
17979
17992
  setInputValue(value);
17980
- onChange(value.toString().replace(/[^0-9.]/g, ""));
17993
+ onChange(value.toString().replace(/[^\d.-]/g, ""));
17981
17994
  }
17982
17995
  };
17983
17996
  const handleBlur = (e) => {
@@ -17990,14 +18003,17 @@ const NumberInput = ({ id, className, htmlProps, leftCategory, rightCategory, la
17990
18003
  !onFocus && setPlaceholderText("");
17991
18004
  };
17992
18005
  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
18006
+ a__default.createElement(TextFieldInput, { "aria-describedby": `${id}-helper-text ${ariaDescribedBy}`, type: "text", inputMode: "numeric",
18007
+ // prettier-ignore
18008
+ pattern: allowNegativeNumbers ? "^[\\d,\\.\\-]*$" : "^[\\d,\\.]*$", id: `${id}-text-field`, disabled: disabled || isLoading, placeholder: placeholderText, onChange: handleChange, onBlur: handleBlur, onFocus: handleFocus, onKeyDown: handleKeyDown, value: thousandSeparator
17994
18009
  ? formatDynamicNumbersWithCommas(valueToUse, decimalPlaces)
17995
18010
  : valueToUse, maxLength: maxLength &&
17996
18011
  maxLength +
17997
18012
  (thousandSeparator
17998
18013
  ? Math.floor(valueToUse.toString().split(".")[0].length / 4)
17999
18014
  : 0) +
18000
- (inputValue.includes(".") ? 1 : 0), "$isTruncated": isTruncated, "$textAlign": textAlign, "$isDisabled": disabled, "$isLoading": isLoading, ...htmlProps })));
18015
+ (inputValue.includes(".") ? 1 : 0) +
18016
+ (inputValue.includes("-") ? 1 : 0), "$isTruncated": isTruncated, "$textAlign": textAlign, "$isDisabled": disabled, "$isLoading": isLoading, ...htmlProps })));
18001
18017
  };
18002
18018
 
18003
18019
  export { HelperText, IconButton, Label, Loader, NumberInput, PasswordInput, StellaButton, StellaButtonVariant, StellaIcon, Styles, TextInput, Tooltip };