@chayns-components/core 5.0.0-beta.271 → 5.0.0-beta.273

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.
@@ -56,6 +56,10 @@ export type InputProps = {
56
56
  * If true, the input field is focused when the component is mounted
57
57
  */
58
58
  shouldUseAutoFocus?: boolean;
59
+ /**
60
+ * If true, the input field is marked as invalid
61
+ */
62
+ isInvalid?: boolean;
59
63
  };
60
64
  declare const Input: React.ForwardRefExoticComponent<InputProps & React.RefAttributes<InputRef>>;
61
65
  export default Input;
@@ -7,6 +7,7 @@ exports.default = void 0;
7
7
  var _react = _interopRequireWildcard(require("react"));
8
8
  var _Icon = _interopRequireDefault(require("../icon/Icon"));
9
9
  var _Input = require("./Input.styles");
10
+ var _styledComponents = require("styled-components");
10
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
13
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -24,9 +25,11 @@ const Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
24
25
  shouldShowClearIcon = false,
25
26
  type = 'text',
26
27
  value,
27
- shouldUseAutoFocus = false
28
+ shouldUseAutoFocus = false,
29
+ isInvalid = false
28
30
  } = _ref;
29
31
  const [hasValue, setHasValue] = (0, _react.useState)(typeof value === 'string' && value !== '');
32
+ const theme = (0, _styledComponents.useTheme)();
30
33
  const inputRef = (0, _react.useRef)(null);
31
34
  const handleClearIconClick = (0, _react.useCallback)(() => {
32
35
  if (inputRef.current) {
@@ -69,7 +72,8 @@ const Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
69
72
  }, [hasValue]);
70
73
  return /*#__PURE__*/_react.default.createElement(_Input.StyledInput, {
71
74
  className: "beta-chayns-input",
72
- isDisabled: isDisabled
75
+ isDisabled: isDisabled,
76
+ isInvalid: isInvalid
73
77
  }, iconElement && /*#__PURE__*/_react.default.createElement(_Input.StyledInputIconWrapper, null, iconElement), /*#__PURE__*/_react.default.createElement(_Input.StyledInputContent, null, /*#__PURE__*/_react.default.createElement(_Input.StyledInputField, {
74
78
  disabled: isDisabled,
75
79
  onBlur: onBlur,
@@ -80,7 +84,8 @@ const Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
80
84
  type: type,
81
85
  value: value,
82
86
  autoFocus: shouldUseAutoFocus,
83
- inputMode: inputMode
87
+ inputMode: inputMode,
88
+ isInvalid: isInvalid
84
89
  }), /*#__PURE__*/_react.default.createElement(_Input.StyledMotionInputLabel, {
85
90
  animate: {
86
91
  scale: hasValue ? 0.6 : 1
@@ -92,7 +97,8 @@ const Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
92
97
  },
93
98
  transition: {
94
99
  type: 'tween'
95
- }
100
+ },
101
+ isInvalid: isInvalid
96
102
  }, placeholderElement, placeholder)), shouldShowClearIcon && /*#__PURE__*/_react.default.createElement(_Input.StyledMotionInputClearIcon, {
97
103
  animate: {
98
104
  opacity: hasValue ? 1 : 0
@@ -101,9 +107,11 @@ const Input = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
101
107
  onClick: handleClearIconClick,
102
108
  transition: {
103
109
  type: 'tween'
104
- }
110
+ },
111
+ isInvalid: isInvalid
105
112
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
106
- icons: ['fa fa-times']
113
+ icons: ['fa fa-times'],
114
+ color: isInvalid ? theme.wrong : undefined
107
115
  })));
108
116
  });
109
117
  Input.displayName = 'Input';
@@ -1 +1 @@
1
- {"version":3,"file":"Input.js","names":["_react","_interopRequireWildcard","require","_Icon","_interopRequireDefault","_Input","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","Input","forwardRef","_ref","ref","iconElement","inputMode","isDisabled","onBlur","onChange","onFocus","onKeyDown","placeholder","placeholderElement","shouldShowClearIcon","type","value","shouldUseAutoFocus","hasValue","setHasValue","useState","inputRef","useRef","handleClearIconClick","useCallback","current","target","handleInputFieldChange","event","useImperativeHandle","focus","_inputRef$current","useEffect","labelPosition","useMemo","bottom","right","left","top","createElement","StyledInput","className","StyledInputIconWrapper","StyledInputContent","StyledInputField","disabled","autoFocus","StyledMotionInputLabel","animate","scale","initial","layout","style","transition","StyledMotionInputClearIcon","opacity","onClick","icons","displayName","_default","exports"],"sources":["../../../src/components/input/Input.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\n ChangeEventHandler,\n FocusEventHandler,\n forwardRef,\n HTMLInputTypeAttribute,\n KeyboardEventHandler,\n ReactNode,\n useCallback,\n useEffect,\n useImperativeHandle,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport Icon from '../icon/Icon';\nimport {\n StyledInput,\n StyledInputContent,\n StyledInputField,\n StyledInputIconWrapper,\n StyledMotionInputClearIcon,\n StyledMotionInputLabel,\n} from './Input.styles';\n\nexport type InputRef = {\n focus: VoidFunction;\n};\n\ntype InputMode =\n | 'email'\n | 'search'\n | 'tel'\n | 'text'\n | 'url'\n | 'none'\n | 'numeric'\n | 'decimal'\n | undefined;\n\nexport type InputProps = {\n /**\n * Icon element to be displayed on the left side of the input field\n */\n iconElement?: ReactNode;\n /**\n * Defines the input mode of the input\n */\n inputMode?: InputMode;\n /**\n * Disables the input so that it cannot be changed anymore\n */\n isDisabled?: boolean;\n /**\n * Function that is executed when the input field loses focus\n */\n onBlur?: FocusEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when the text of the input changes\n */\n onChange?: ChangeEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when the input field is focused\n */\n onFocus?: FocusEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when a letter is pressed\n */\n onKeyDown?: KeyboardEventHandler<HTMLInputElement>;\n /**\n * Placeholder for the input field\n */\n placeholder?: string;\n /**\n * Element to be displayed next to or instead of the \"placeholder\"\n */\n placeholderElement?: ReactNode;\n /**\n * If true, a clear icon is displayed at the end of the input field\n */\n shouldShowClearIcon?: boolean;\n /**\n * Input type set for input element (e.g. 'text', 'number' or 'password')\n */\n type?: HTMLInputTypeAttribute;\n /**\n * Value if the input field should be controlled\n */\n value?: string;\n /**\n * If true, the input field is focused when the component is mounted\n */\n shouldUseAutoFocus?: boolean;\n};\n\nconst Input = forwardRef<InputRef, InputProps>(\n (\n {\n iconElement,\n inputMode,\n isDisabled,\n onBlur,\n onChange,\n onFocus,\n onKeyDown,\n placeholder,\n placeholderElement,\n shouldShowClearIcon = false,\n type = 'text',\n value,\n shouldUseAutoFocus = false,\n },\n ref\n ) => {\n const [hasValue, setHasValue] = useState(typeof value === 'string' && value !== '');\n\n const inputRef = useRef<HTMLInputElement>(null);\n\n const handleClearIconClick = useCallback(() => {\n if (inputRef.current) {\n inputRef.current.value = '';\n\n if (typeof onChange === 'function') {\n onChange({ target: inputRef.current } as ChangeEvent<HTMLInputElement>);\n }\n }\n }, [onChange]);\n\n const handleInputFieldChange = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n setHasValue(event.target.value !== '');\n\n if (typeof onChange === 'function') {\n onChange(event);\n }\n },\n [onChange]\n );\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => inputRef.current?.focus(),\n }),\n []\n );\n\n useEffect(() => {\n if (typeof value === 'string') {\n setHasValue(value !== '');\n }\n }, [value]);\n\n const labelPosition = useMemo(() => {\n if (hasValue) {\n return { bottom: -8, right: -6 };\n }\n\n return { left: 0, top: 0 };\n }, [hasValue]);\n\n return (\n <StyledInput className=\"beta-chayns-input\" isDisabled={isDisabled}>\n {iconElement && <StyledInputIconWrapper>{iconElement}</StyledInputIconWrapper>}\n <StyledInputContent>\n <StyledInputField\n disabled={isDisabled}\n onBlur={onBlur}\n onChange={handleInputFieldChange}\n onFocus={onFocus}\n onKeyDown={onKeyDown}\n ref={inputRef}\n type={type}\n value={value}\n autoFocus={shouldUseAutoFocus}\n inputMode={inputMode}\n />\n <StyledMotionInputLabel\n animate={{ scale: hasValue ? 0.6 : 1 }}\n initial={false}\n layout\n style={{ ...labelPosition }}\n transition={{ type: 'tween' }}\n >\n {placeholderElement}\n {placeholder}\n </StyledMotionInputLabel>\n </StyledInputContent>\n {shouldShowClearIcon && (\n <StyledMotionInputClearIcon\n animate={{ opacity: hasValue ? 1 : 0 }}\n initial={false}\n onClick={handleClearIconClick}\n transition={{ type: 'tween' }}\n >\n <Icon icons={['fa fa-times']} />\n </StyledMotionInputClearIcon>\n )}\n </StyledInput>\n );\n }\n);\n\nInput.displayName = 'Input';\n\nexport default Input;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAeA,IAAAC,KAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAOwB,SAAAE,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,SAAAG,yBAAAC,WAAA,eAAAC,OA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KAAM;IACfC,MAAM;IACNC,KAAK,EAAE;MAAE,GAAGnB;IAAc,CAAE;IAC5BoB,UAAU,EAAE;MAAEtC,IAAI,EAAE;IAAQ;EAAE,GAE7BF,kBAAkB,EAClBD,WACmB,CACR,CAAC,EACpBE,mBAAmB,iBAChBzC,MAAA,CAAAQ,OAAA,CAAA0D,aAAA,CAAC7D,MAAA,CAAA4E,0BAA0B;IACvBN,OAAO,EAAE;MAAEO,OAAO,EAAErC,QAAQ,GAAG,CAAC,GAAG;IAAE,CAAE;IACvCgC,OAAO,EAAE,KAAM;IACfM,OAAO,EAAEjC,oBAAqB;IAC9B8B,UAAU,EAAE;MAAEtC,IAAI,EAAE;IAAQ;EAAE,gBAE9B1C,MAAA,CAAAQ,OAAA,CAAA0D,aAAA,CAAC/D,KAAA,CAAAK,OAAI;IAAC4E,KAAK,EAAE,CAAC,aAAa;EAAE,CAAE,CACP,CAEvB,CAAC;AAEtB,CACJ,CAAC;AAEDxD,KAAK,CAACyD,WAAW,GAAG,OAAO;AAAC,IAAAC,QAAA,GAEb1D,KAAK;AAAA2D,OAAA,CAAA/E,OAAA,GAAA8E,QAAA"}
1
+ {"version":3,"file":"Input.js","names":["_react","_interopRequireWildcard","require","_Icon","_interopRequireDefault","_Input","_styledComponents","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","Input","forwardRef","_ref","ref","iconElement","inputMode","isDisabled","onBlur","onChange","onFocus","onKeyDown","placeholder","placeholderElement","shouldShowClearIcon","type","value","shouldUseAutoFocus","isInvalid","hasValue","setHasValue","useState","theme","useTheme","inputRef","useRef","handleClearIconClick","useCallback","current","target","handleInputFieldChange","event","useImperativeHandle","focus","_inputRef$current","useEffect","labelPosition","useMemo","bottom","right","left","top","createElement","StyledInput","className","StyledInputIconWrapper","StyledInputContent","StyledInputField","disabled","autoFocus","StyledMotionInputLabel","animate","scale","initial","layout","style","transition","StyledMotionInputClearIcon","opacity","onClick","icons","color","wrong","undefined","displayName","_default","exports"],"sources":["../../../src/components/input/Input.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\n ChangeEventHandler,\n FocusEventHandler,\n forwardRef,\n HTMLInputTypeAttribute,\n KeyboardEventHandler,\n ReactNode,\n useCallback,\n useEffect,\n useImperativeHandle,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport Icon from '../icon/Icon';\nimport {\n StyledInput,\n StyledInputContent,\n StyledInputField,\n StyledInputIconWrapper,\n StyledMotionInputClearIcon,\n StyledMotionInputLabel,\n} from './Input.styles';\nimport type { Theme } from '../color-scheme-provider/ColorSchemeProvider';\nimport { useTheme } from 'styled-components';\n\nexport type InputRef = {\n focus: VoidFunction;\n};\n\ntype InputMode =\n | 'email'\n | 'search'\n | 'tel'\n | 'text'\n | 'url'\n | 'none'\n | 'numeric'\n | 'decimal'\n | undefined;\n\nexport type InputProps = {\n /**\n * Icon element to be displayed on the left side of the input field\n */\n iconElement?: ReactNode;\n /**\n * Defines the input mode of the input\n */\n inputMode?: InputMode;\n /**\n * Disables the input so that it cannot be changed anymore\n */\n isDisabled?: boolean;\n /**\n * Function that is executed when the input field loses focus\n */\n onBlur?: FocusEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when the text of the input changes\n */\n onChange?: ChangeEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when the input field is focused\n */\n onFocus?: FocusEventHandler<HTMLInputElement>;\n /**\n * Function that is executed when a letter is pressed\n */\n onKeyDown?: KeyboardEventHandler<HTMLInputElement>;\n /**\n * Placeholder for the input field\n */\n placeholder?: string;\n /**\n * Element to be displayed next to or instead of the \"placeholder\"\n */\n placeholderElement?: ReactNode;\n /**\n * If true, a clear icon is displayed at the end of the input field\n */\n shouldShowClearIcon?: boolean;\n /**\n * Input type set for input element (e.g. 'text', 'number' or 'password')\n */\n type?: HTMLInputTypeAttribute;\n /**\n * Value if the input field should be controlled\n */\n value?: string;\n /**\n * If true, the input field is focused when the component is mounted\n */\n shouldUseAutoFocus?: boolean;\n /**\n * If true, the input field is marked as invalid\n */\n isInvalid?: boolean;\n};\n\nconst Input = forwardRef<InputRef, InputProps>(\n (\n {\n iconElement,\n inputMode,\n isDisabled,\n onBlur,\n onChange,\n onFocus,\n onKeyDown,\n placeholder,\n placeholderElement,\n shouldShowClearIcon = false,\n type = 'text',\n value,\n shouldUseAutoFocus = false,\n isInvalid = false\n },\n ref\n ) => {\n const [hasValue, setHasValue] = useState(typeof value === 'string' && value !== '');\n const theme = useTheme() as Theme;\n const inputRef = useRef<HTMLInputElement>(null);\n\n const handleClearIconClick = useCallback(() => {\n if (inputRef.current) {\n inputRef.current.value = '';\n\n if (typeof onChange === 'function') {\n onChange({ target: inputRef.current } as ChangeEvent<HTMLInputElement>);\n }\n }\n }, [onChange]);\n\n const handleInputFieldChange = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n setHasValue(event.target.value !== '');\n\n if (typeof onChange === 'function') {\n onChange(event);\n }\n },\n [onChange]\n );\n\n useImperativeHandle(\n ref,\n () => ({\n focus: () => inputRef.current?.focus(),\n }),\n []\n );\n\n useEffect(() => {\n if (typeof value === 'string') {\n setHasValue(value !== '');\n }\n }, [value]);\n\n const labelPosition = useMemo(() => {\n if (hasValue) {\n return { bottom: -8, right: -6 };\n }\n\n return { left: 0, top: 0 };\n }, [hasValue]);\n\n return (\n <StyledInput className=\"beta-chayns-input\" isDisabled={isDisabled} isInvalid={isInvalid}>\n {iconElement && <StyledInputIconWrapper>{iconElement}</StyledInputIconWrapper>}\n <StyledInputContent>\n <StyledInputField\n disabled={isDisabled}\n onBlur={onBlur}\n onChange={handleInputFieldChange}\n onFocus={onFocus}\n onKeyDown={onKeyDown}\n ref={inputRef}\n type={type}\n value={value}\n autoFocus={shouldUseAutoFocus}\n inputMode={inputMode}\n isInvalid={isInvalid}\n />\n <StyledMotionInputLabel\n animate={{ scale: hasValue ? 0.6 : 1 }}\n initial={false}\n layout\n style={{ ...labelPosition }}\n transition={{ type: 'tween' }}\n isInvalid={isInvalid}\n >\n {placeholderElement}\n {placeholder}\n </StyledMotionInputLabel>\n </StyledInputContent>\n {shouldShowClearIcon && (\n <StyledMotionInputClearIcon\n animate={{ opacity: hasValue ? 1 : 0 }}\n initial={false}\n onClick={handleClearIconClick}\n transition={{ type: 'tween' }}\n isInvalid={isInvalid}\n >\n <Icon icons={['fa fa-times']} color={isInvalid ? theme.wrong : undefined}/>\n </StyledMotionInputClearIcon>\n )}\n </StyledInput>\n );\n }\n);\n\nInput.displayName = 'Input';\n\nexport default Input;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAeA,IAAAC,KAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AASA,IAAAI,iBAAA,GAAAJ,OAAA;AAA6C,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,SAAAG,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAV,wBAAAM,GAAA,EAAAI,WAAA,SAAAA,WAAA,IAAAJ,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAQ,KAAA,GAAAL,wBAAA,CAAAC,WAAA,OAAAI,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAT,GAAA,YAAAQ,KAAA,CAAAE,GAAA,CAAAV,GAAA,SAAAW,MAAA,WAAAC,qBAAA,GAAAC,MAA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ACfM,OAAO,EAAEjC,oBAAqB;IAC9B8B,UAAU,EAAE;MAAEzC,IAAI,EAAE;IAAQ,CAAE;IAC9BG,SAAS,EAAEA;EAAU,gBAErB9C,MAAA,CAAAS,OAAA,CAAA6D,aAAA,CAACnE,KAAA,CAAAM,OAAI;IAAC+E,KAAK,EAAE,CAAC,aAAa,CAAE;IAACC,KAAK,EAAE3C,SAAS,GAAGI,KAAK,CAACwC,KAAK,GAAGC;EAAU,CAAC,CAClD,CAEvB,CAAC;AAEtB,CACJ,CAAC;AAED9D,KAAK,CAAC+D,WAAW,GAAG,OAAO;AAAC,IAAAC,QAAA,GAEbhE,KAAK;AAAAiE,OAAA,CAAArF,OAAA,GAAAoF,QAAA"}
@@ -1,9 +1,15 @@
1
1
  import type { InputProps } from './Input';
2
- export declare const StyledInput: import("styled-components").StyledComponent<"div", any, Pick<InputProps, "isDisabled"> & {
2
+ export declare const StyledInput: import("styled-components").StyledComponent<"div", any, Pick<InputProps, "isDisabled" | "isInvalid"> & {
3
3
  theme: import("../color-scheme-provider/ColorSchemeProvider").Theme;
4
4
  }, never>;
5
5
  export declare const StyledInputContent: import("styled-components").StyledComponent<"div", any, {}, never>;
6
- export declare const StyledInputField: import("styled-components").StyledComponent<"input", any, {}, never>;
7
- export declare const StyledMotionInputLabel: import("styled-components").StyledComponent<import("framer-motion").ForwardRefComponent<HTMLLabelElement, import("framer-motion").HTMLMotionProps<"label">>, any, {}, never>;
8
- export declare const StyledMotionInputClearIcon: import("styled-components").StyledComponent<import("framer-motion").ForwardRefComponent<HTMLDivElement, import("framer-motion").HTMLMotionProps<"div">>, any, {}, never>;
6
+ export declare const StyledInputField: import("styled-components").StyledComponent<"input", any, Pick<InputProps, "isInvalid"> & {
7
+ theme: import("../color-scheme-provider/ColorSchemeProvider").Theme;
8
+ }, never>;
9
+ export declare const StyledMotionInputLabel: import("styled-components").StyledComponent<import("framer-motion").ForwardRefComponent<HTMLLabelElement, import("framer-motion").HTMLMotionProps<"label">>, any, Pick<InputProps, "isInvalid"> & {
10
+ theme: import("../color-scheme-provider/ColorSchemeProvider").Theme;
11
+ }, never>;
12
+ export declare const StyledMotionInputClearIcon: import("styled-components").StyledComponent<import("framer-motion").ForwardRefComponent<HTMLDivElement, import("framer-motion").HTMLMotionProps<"div">>, any, Pick<InputProps, "isInvalid"> & {
13
+ theme: import("../color-scheme-provider/ColorSchemeProvider").Theme;
14
+ }, never>;
9
15
  export declare const StyledInputIconWrapper: import("styled-components").StyledComponent<"div", any, {}, never>;
@@ -15,21 +15,27 @@ const StyledInput = _styledComponents.default.div`
15
15
  } = _ref;
16
16
  return theme['100'];
17
17
  }};
18
- border: 1px solid rgba(160, 160, 160, 0.3);
18
+ border: 1px solid ${_ref2 => {
19
+ let {
20
+ theme,
21
+ isInvalid
22
+ } = _ref2;
23
+ return isInvalid ? theme.wrong : 'rgba(160, 160, 160, 0.3)';
24
+ }};
19
25
  border-radius: 3px;
20
- color: ${_ref2 => {
26
+ color: ${_ref3 => {
21
27
  let {
22
28
  theme
23
- } = _ref2;
29
+ } = _ref3;
24
30
  return theme['006'];
25
31
  }};
26
32
  display: flex;
27
33
  justify-content: space-between;
28
34
  min-height: 42px;
29
- opacity: ${_ref3 => {
35
+ opacity: ${_ref4 => {
30
36
  let {
31
37
  isDisabled
32
- } = _ref3;
38
+ } = _ref4;
33
39
  return isDisabled ? 0.5 : 1;
34
40
  }};
35
41
  transition: opacity 0.3s ease;
@@ -47,11 +53,12 @@ exports.StyledInputContent = StyledInputContent;
47
53
  const StyledInputField = _styledComponents.default.input`
48
54
  background: none;
49
55
  border: none;
50
- color: ${_ref4 => {
56
+ color: ${_ref5 => {
51
57
  let {
52
- theme
53
- } = _ref4;
54
- return theme.text;
58
+ theme,
59
+ isInvalid
60
+ } = _ref5;
61
+ return isInvalid ? theme.wrong : theme.text;
55
62
  }};
56
63
  padding: 0;
57
64
  width: 100%;
@@ -66,17 +73,18 @@ const StyledMotionInputLabel = (0, _styledComponents.default)(_framerMotion.moti
66
73
  pointer-events: none;
67
74
  position: absolute;
68
75
  user-select: none;
76
+ color: ${_ref6 => {
77
+ let {
78
+ theme,
79
+ isInvalid
80
+ } = _ref6;
81
+ return isInvalid ? theme.wrong : undefined;
82
+ }}
69
83
  `;
70
84
  exports.StyledMotionInputLabel = StyledMotionInputLabel;
71
85
  const StyledMotionInputClearIcon = (0, _styledComponents.default)(_framerMotion.motion.div)`
72
86
  align-items: center;
73
87
  border-left: 1px solid rgba(160, 160, 160, 0.3);
74
- color: ${_ref5 => {
75
- let {
76
- theme
77
- } = _ref5;
78
- return theme.headline;
79
- }};
80
88
  cursor: pointer;
81
89
  display: flex;
82
90
  flex: 0 0 auto;
@@ -1 +1 @@
1
- {"version":3,"file":"Input.styles.js","names":["_framerMotion","require","_styledComponents","_interopRequireDefault","obj","__esModule","default","StyledInput","styled","div","_ref","theme","_ref2","_ref3","isDisabled","exports","StyledInputContent","StyledInputField","input","_ref4","text","StyledMotionInputLabel","motion","label","StyledMotionInputClearIcon","_ref5","headline","StyledInputIconWrapper"],"sources":["../../../src/components/input/Input.styles.ts"],"sourcesContent":["import { motion } from 'framer-motion';\nimport styled from 'styled-components';\nimport type { WithTheme } from '../color-scheme-provider/ColorSchemeProvider';\nimport type { InputProps } from './Input';\n\ntype StyledInputProps = WithTheme<Pick<InputProps, 'isDisabled'>>;\n\nexport const StyledInput = styled.div<StyledInputProps>`\n align-items: center;\n background-color: ${({ theme }: StyledInputProps) => theme['100']};\n border: 1px solid rgba(160, 160, 160, 0.3);\n border-radius: 3px;\n color: ${({ theme }: StyledInputProps) => theme['006']};\n display: flex;\n justify-content: space-between;\n min-height: 42px;\n opacity: ${({ isDisabled }) => (isDisabled ? 0.5 : 1)};\n transition: opacity 0.3s ease;\n width: 100%;\n`;\n\nexport const StyledInputContent = styled.div`\n display: flex;\n flex: 1 1 auto;\n min-width: 0;\n margin: 8px 10px;\n position: relative;\n`;\n\nexport const StyledInputField = styled.input`\n background: none;\n border: none;\n color: ${({ theme }: StyledInputProps) => theme.text};\n padding: 0;\n width: 100%;\n`;\n\nexport const StyledMotionInputLabel = styled(motion.label)`\n align-items: baseline;\n display: flex;\n flex: 0 0 auto;\n gap: 4px;\n line-height: 1.3;\n pointer-events: none;\n position: absolute;\n user-select: none;\n`;\n\nexport const StyledMotionInputClearIcon = styled(motion.div)`\n align-items: center;\n border-left: 1px solid rgba(160, 160, 160, 0.3);\n color: ${({ theme }: StyledInputProps) => theme.headline};\n cursor: pointer;\n display: flex;\n flex: 0 0 auto;\n height: 40px;\n justify-content: center;\n width: 40px;\n`;\n\nexport const StyledInputIconWrapper = styled.div`\n align-items: baseline;\n display: flex;\n flex: 0 0 auto;\n justify-content: center;\n margin-left: 10px;\n`;\n"],"mappings":";;;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAC,sBAAA,CAAAF,OAAA;AAAuC,SAAAE,uBAAAC,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAMhC,MAAMG,WAAW,GAAGC,yBAAM,CAACC,GAAsB;AACxD;AACA,wBAAwBC,IAAA;EAAA,IAAC;IAAEC;EAAwB,CAAC,GAAAD,IAAA;EAAA,OAAKC,KAAK,CAAC,KAAK,CAAC;AAAA,CAAC;AACtE;AACA;AACA,aAAaC,KAAA;EAAA,IAAC;IAAED;EAAwB,CAAC,GAAAC,KAAA;EAAA,OAAKD,KAAK,CAAC,KAAK,CAAC;AAAA,CAAC;AAC3D;AACA;AACA;AACA,eAAeE,KAAA;EAAA,IAAC;IAAEC;EAAW,CAAC,GAAAD,KAAA;EAAA,OAAMC,UAAU,GAAG,GAAG,GAAG,CAAC;AAAA,CAAE;AAC1D;AACA;AACA,CAAC;AAACC,OAAA,CAAAR,WAAA,GAAAA,WAAA;AAEK,MAAMS,kBAAkB,GAAGR,yBAAM,CAACC,GAAI;AAC7C;AACA;AACA;AACA;AACA;AACA,CAAC;AAACM,OAAA,CAAAC,kBAAA,GAAAA,kBAAA;AAEK,MAAMC,gBAAgB,GAAGT,yBAAM,CAACU,KAAM;AAC7C;AACA;AACA,aAAaC,KAAA;EAAA,IAAC;IAAER;EAAwB,CAAC,GAAAQ,KAAA;EAAA,OAAKR,KAAK,CAACS,IAAI;AAAA,CAAC;AACzD;AACA;AACA,CAAC;AAACL,OAAA,CAAAE,gBAAA,GAAAA,gBAAA;AAEK,MAAMI,sBAAsB,GAAG,IAAAb,yBAAM,EAACc,oBAAM,CAACC,KAAK,CAAE;AAC3D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAACR,OAAA,CAAAM,sBAAA,GAAAA,sBAAA;AAEK,MAAMG,0BAA0B,GAAG,IAAAhB,yBAAM,EAACc,oBAAM,CAACb,GAAG,CAAE;AAC7D;AACA;AACA,aAAagB,KAAA;EAAA,IAAC;IAAEd;EAAwB,CAAC,GAAAc,KAAA;EAAA,OAAKd,KAAK,CAACe,QAAQ;AAAA,CAAC;AAC7D;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAACX,OAAA,CAAAS,0BAAA,GAAAA,0BAAA;AAEK,MAAMG,sBAAsB,GAAGnB,yBAAM,CAACC,GAAI;AACjD;AACA;AACA;AACA;AACA;AACA,CAAC;AAACM,OAAA,CAAAY,sBAAA,GAAAA,sBAAA"}
1
+ {"version":3,"file":"Input.styles.js","names":["_framerMotion","require","_styledComponents","_interopRequireDefault","obj","__esModule","default","StyledInput","styled","div","_ref","theme","_ref2","isInvalid","wrong","_ref3","_ref4","isDisabled","exports","StyledInputContent","StyledInputField","input","_ref5","text","StyledMotionInputLabel","motion","label","_ref6","undefined","StyledMotionInputClearIcon","StyledInputIconWrapper"],"sources":["../../../src/components/input/Input.styles.ts"],"sourcesContent":["import { motion } from 'framer-motion';\nimport styled from 'styled-components';\nimport type { WithTheme } from '../color-scheme-provider/ColorSchemeProvider';\nimport type { InputProps } from './Input';\n\ntype StyledInputProps = WithTheme<Pick<InputProps, 'isDisabled'| 'isInvalid'>>;\n\nexport const StyledInput = styled.div<StyledInputProps>`\n align-items: center;\n background-color: ${({ theme }: StyledInputProps) => theme['100']};\n border: 1px solid ${({theme, isInvalid}:StyledInputProps ) => isInvalid ? theme.wrong : 'rgba(160, 160, 160, 0.3)'};\n border-radius: 3px;\n color: ${({ theme }: StyledInputProps) => theme['006']};\n display: flex;\n justify-content: space-between;\n min-height: 42px;\n opacity: ${({ isDisabled }) => (isDisabled ? 0.5 : 1)};\n transition: opacity 0.3s ease;\n width: 100%;\n`;\n\nexport const StyledInputContent = styled.div`\n display: flex;\n flex: 1 1 auto;\n min-width: 0;\n margin: 8px 10px;\n position: relative;\n`;\n\ntype StyledInputFieldProps = WithTheme<Pick<InputProps, 'isInvalid'>>;\n\nexport const StyledInputField = styled.input<StyledInputFieldProps>`\n background: none;\n border: none;\n color: ${({ theme, isInvalid }: StyledInputFieldProps) => isInvalid ? theme.wrong : theme.text};\n padding: 0;\n width: 100%;\n`;\n\ntype StyledMotionInputLabelProps = WithTheme<Pick<InputProps, 'isInvalid'>>;\n\nexport const StyledMotionInputLabel = styled(motion.label)<StyledMotionInputLabelProps>`\n align-items: baseline;\n display: flex;\n flex: 0 0 auto;\n gap: 4px;\n line-height: 1.3;\n pointer-events: none;\n position: absolute;\n user-select: none;\n color: ${({theme, isInvalid}: StyledMotionInputLabelProps)=> isInvalid ? theme.wrong : undefined}\n`;\n\ntype StyledMotionInputClearIconProps = WithTheme<Pick<InputProps, 'isInvalid'>>;\n\nexport const StyledMotionInputClearIcon = styled(motion.div)<StyledMotionInputClearIconProps>`\n align-items: center;\n border-left: 1px solid rgba(160, 160, 160, 0.3);\n cursor: pointer;\n display: flex;\n flex: 0 0 auto;\n height: 40px;\n justify-content: center;\n width: 40px;\n`;\n\nexport const StyledInputIconWrapper = styled.div`\n align-items: baseline;\n display: flex;\n flex: 0 0 auto;\n justify-content: center;\n margin-left: 10px;\n`;\n"],"mappings":";;;;;;AAAA,IAAAA,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I,sBAAsB,GAAG,IAAAhB,yBAAM,EAACiB,oBAAM,CAACC,KAAK,CAA+B;AACxF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAaC,KAAA;EAAA,IAAC;IAAChB,KAAK;IAAEE;EAAsC,CAAC,GAAAc,KAAA;EAAA,OAAId,SAAS,GAAGF,KAAK,CAACG,KAAK,GAAGc,SAAS;AAAA,CAAC;AACrG,CAAC;AAACV,OAAA,CAAAM,sBAAA,GAAAA,sBAAA;AAIK,MAAMK,0BAA0B,GAAG,IAAArB,yBAAM,EAACiB,oBAAM,CAAChB,GAAG,CAAmC;AAC9F;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAACS,OAAA,CAAAW,0BAAA,GAAAA,0BAAA;AAEK,MAAMC,sBAAsB,GAAGtB,yBAAM,CAACC,GAAI;AACjD;AACA;AACA;AACA;AACA;AACA,CAAC;AAACS,OAAA,CAAAY,sBAAA,GAAAA,sBAAA"}
@@ -23,9 +23,13 @@ const NumberInput = _ref => {
23
23
  onChange
24
24
  } = _ref;
25
25
  const [stringValue, setStringValue] = (0, _react.useState)('');
26
+ const [hasFocus, setHasFocus] = (0, _react.useState)(false);
26
27
  const localPlaceholder = placeholder !== null && placeholder !== void 0 ? placeholder : isMoneyInput ? '€' : undefined;
27
28
  const handleChange = (0, _react.useCallback)(function () {
28
29
  let newValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
30
+ if (hasFocus) {
31
+ return;
32
+ }
29
33
  if (typeof newValue !== 'number') {
30
34
  setStringValue('');
31
35
  return;
@@ -37,7 +41,7 @@ const NumberInput = _ref => {
37
41
  number: parsedValue,
38
42
  isMoneyInput
39
43
  }));
40
- }, [isMoneyInput]);
44
+ }, [hasFocus, isMoneyInput]);
41
45
  const onLocalChange = event => {
42
46
  const newValue = event.target.value;
43
47
  const sanitizedValue = newValue
@@ -97,6 +101,7 @@ const NumberInput = _ref => {
97
101
  }
98
102
  };
99
103
  const onLocalBlur = () => {
104
+ setHasFocus(false);
100
105
  const sanitizedValue = stringValue.length === 0 ? '0' : stringValue;
101
106
  const parsedValue = (0, _number2.parseFloatAndRound)({
102
107
  stringValue: sanitizedValue
@@ -110,6 +115,7 @@ const NumberInput = _ref => {
110
115
  }
111
116
  };
112
117
  const onFocus = () => {
118
+ setHasFocus(true);
113
119
  setStringValue(stringValue.replaceAll('.', ''));
114
120
  };
115
121
  (0, _react.useEffect)(() => {
@@ -1 +1 @@
1
- {"version":3,"file":"NumberInput.js","names":["_react","_interopRequireWildcard","require","_Input","_interopRequireDefault","_number","_number2","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","NumberInput","_ref","isDecimalInput","isMoneyInput","maxNumber","Infinity","number","placeholder","onBlur","isDisabled","onChange","stringValue","setStringValue","useState","localPlaceholder","undefined","handleChange","useCallback","newValue","arguments","length","parsedValue","parseFloatAndRound","toString","formateNumber","onLocalChange","event","target","value","sanitizedValue","replace","NUMBER_CLEAR_REGEX","DECIMAL_TEST","test","parsedNumber","MONEY_TEST","decimals","INTEGER_TEST","Number","onLocalBlur","onFocus","replaceAll","useEffect","createElement","inputMode","displayName","_default","exports"],"sources":["../../../src/components/number-input/NumberInput.tsx"],"sourcesContent":["import React, { ChangeEvent, FC, useCallback, useEffect, useState } from 'react';\nimport Input from '../input/Input';\nimport { DECIMAL_TEST, INTEGER_TEST, MONEY_TEST, NUMBER_CLEAR_REGEX } from './constants/number';\nimport { formateNumber, parseFloatAndRound } from './utils/number';\n\nexport type NumberInputProps = {\n /**\n * Whether the user can add decimal places. Enables the user to input a zero as first number\n */\n isDecimalInput?: boolean;\n /**\n * Applies rules for money input.\n * Rules: only two decimal places, one leading zero\n */\n isMoneyInput?: boolean;\n /**\n * Limits the number to this value\n */\n maxNumber?: number;\n /**\n * The number that should be displayed formatted in the input field. NOTE: The number has to match the mode (integer, decimal or money)\n */\n number: number | null;\n /**\n * The placeholder that should be in the input\n */\n placeholder?: string;\n /**\n * Callback function that is called when the input gets out of focus\n */\n onBlur?: (newNumber: number | null) => void;\n /**\n * Callback function that is called when the input changes\n */\n onChange?: (newNumber: number | null) => void;\n /**\n * Whether the input is disabled\n */\n isDisabled?: boolean;\n};\n\nconst NumberInput: FC<NumberInputProps> = (\n {\n isDecimalInput,\n isMoneyInput,\n maxNumber = Infinity,\n number,\n placeholder,\n onBlur,\n isDisabled,\n onChange\n }) => {\n const [stringValue, setStringValue] = useState<string>('');\n\n const localPlaceholder = placeholder ?? (isMoneyInput ? '€' : undefined);\n\n const handleChange = useCallback(\n (newValue: number | null = null) => {\n if (typeof newValue !== 'number') {\n setStringValue('');\n\n return;\n }\n\n const parsedValue = parseFloatAndRound({ stringValue: newValue?.toString() });\n\n setStringValue(formateNumber({ number: parsedValue, isMoneyInput }));\n },\n [isMoneyInput]\n );\n\n const onLocalChange = (event: ChangeEvent<HTMLInputElement>) => {\n const newValue = event.target.value;\n\n const sanitizedValue = newValue\n // Removes everything except numbers, commas and points\n .replace(NUMBER_CLEAR_REGEX, '')\n // Calculations need points for decimal indication\n .replace(',', '.');\n\n if (sanitizedValue.length === 0) {\n setStringValue('');\n\n if (typeof onChange === 'function') {\n onChange?.(null);\n }\n\n return;\n }\n\n // Allows numbers, one (comma/point) and any number of decimal places\n if (isDecimalInput && DECIMAL_TEST.test(sanitizedValue)) {\n const parsedNumber = parseFloatAndRound({ stringValue: sanitizedValue });\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue.replace('.', ','));\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n\n return;\n }\n\n // Allows numbers, one (comma/point) and 2 numbers of decimal places\n if (isMoneyInput && MONEY_TEST.test(sanitizedValue)) {\n const parsedNumber = parseFloatAndRound({\n stringValue: sanitizedValue,\n decimals: 2,\n });\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue.replace('.', ','));\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n\n return;\n }\n\n // Allows numbers but excludes numbers with leading 0\n if (INTEGER_TEST.test(sanitizedValue)) {\n const parsedNumber = Number(sanitizedValue);\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue);\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n }\n };\n\n const onLocalBlur = () => {\n const sanitizedValue = stringValue.length === 0 ? '0' : stringValue;\n const parsedValue = parseFloatAndRound({ stringValue: sanitizedValue });\n\n setStringValue(\n stringValue.length === 0\n ? ''\n : formateNumber({\n number: parsedValue,\n isMoneyInput,\n })\n );\n\n if (typeof onBlur === 'function') {\n onBlur(parsedValue === 0 ? null : parsedValue);\n }\n };\n\n const onFocus = () => {\n setStringValue(stringValue.replaceAll('.', ''));\n };\n\n useEffect(() => {\n handleChange(number);\n }, [handleChange, number]);\n\n return (\n <Input\n inputMode=\"decimal\"\n onChange={onLocalChange}\n value={stringValue}\n placeholder={localPlaceholder}\n onBlur={onLocalBlur}\n onFocus={onFocus}\n isDisabled={isDisabled}\n />\n );\n};\n\nNumberInput.displayName = 'NumberInput';\n\nexport default NumberInput;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAAmE,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,SAAAG,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,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AM,EAAEe,WAAW;MACnBlB;IACJ,CAAC,CACT,CAAC;IAED,IAAI,OAAOK,MAAM,KAAK,UAAU,EAAE;MAC9BA,MAAM,CAACa,WAAW,KAAK,CAAC,GAAG,IAAI,GAAGA,WAAW,CAAC;IAClD;EACJ,CAAC;EAED,MAAMmB,OAAO,GAAGA,CAAA,KAAM;IAClB5B,cAAc,CAACD,WAAW,CAAC8B,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;EACnD,CAAC;EAED,IAAAC,gBAAS,EAAC,MAAM;IACZ1B,YAAY,CAACV,MAAM,CAAC;EACxB,CAAC,EAAE,CAACU,YAAY,EAAEV,MAAM,CAAC,CAAC;EAE1B,oBACInC,MAAA,CAAAS,OAAA,CAAA+D,aAAA,CAACrE,MAAA,CAAAM,OAAK;IACFgE,SAAS,EAAC,SAAS;IACnBlC,QAAQ,EAAEe,aAAc;IACxBG,KAAK,EAAEjB,WAAY;IACnBJ,WAAW,EAAEO,gBAAiB;IAC9BN,MAAM,EAAE+B,WAAY;IACpBC,OAAO,EAAEA,OAAQ;IACjB/B,UAAU,EAAEA;EAAW,CAC1B,CAAC;AAEV,CAAC;AAEDT,WAAW,CAAC6C,WAAW,GAAG,aAAa;AAAC,IAAAC,QAAA,GAEzB9C,WAAW;AAAA+C,OAAA,CAAAnE,OAAA,GAAAkE,QAAA"}
1
+ {"version":3,"file":"NumberInput.js","names":["_react","_interopRequireWildcard","require","_Input","_interopRequireDefault","_number","_number2","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","NumberInput","_ref","isDecimalInput","isMoneyInput","maxNumber","Infinity","number","placeholder","onBlur","isDisabled","onChange","stringValue","setStringValue","useState","hasFocus","setHasFocus","localPlaceholder","undefined","handleChange","useCallback","newValue","arguments","length","parsedValue","parseFloatAndRound","toString","formateNumber","onLocalChange","event","target","value","sanitizedValue","replace","NUMBER_CLEAR_REGEX","DECIMAL_TEST","test","parsedNumber","MONEY_TEST","decimals","INTEGER_TEST","Number","onLocalBlur","onFocus","replaceAll","useEffect","createElement","inputMode","displayName","_default","exports"],"sources":["../../../src/components/number-input/NumberInput.tsx"],"sourcesContent":["import React, { ChangeEvent, FC, useCallback, useEffect, useState } from 'react';\nimport Input from '../input/Input';\nimport { DECIMAL_TEST, INTEGER_TEST, MONEY_TEST, NUMBER_CLEAR_REGEX } from './constants/number';\nimport { formateNumber, parseFloatAndRound } from './utils/number';\n\nexport type NumberInputProps = {\n /**\n * Whether the user can add decimal places. Enables the user to input a zero as first number\n */\n isDecimalInput?: boolean;\n /**\n * Applies rules for money input.\n * Rules: only two decimal places, one leading zero\n */\n isMoneyInput?: boolean;\n /**\n * Limits the number to this value\n */\n maxNumber?: number;\n /**\n * The number that should be displayed formatted in the input field. NOTE: The number has to match the mode (integer, decimal or money)\n */\n number: number | null;\n /**\n * The placeholder that should be in the input\n */\n placeholder?: string;\n /**\n * Callback function that is called when the input gets out of focus\n */\n onBlur?: (newNumber: number | null) => void;\n /**\n * Callback function that is called when the input changes\n */\n onChange?: (newNumber: number | null) => void;\n /**\n * Whether the input is disabled\n */\n isDisabled?: boolean;\n};\n\nconst NumberInput: FC<NumberInputProps> = (\n {\n isDecimalInput,\n isMoneyInput,\n maxNumber = Infinity,\n number,\n placeholder,\n onBlur,\n isDisabled,\n onChange\n }) => {\n const [stringValue, setStringValue] = useState<string>('');\n const [hasFocus, setHasFocus] = useState<boolean>(false);\n\n const localPlaceholder = placeholder ?? (isMoneyInput ? '€' : undefined);\n\n const handleChange = useCallback(\n (newValue: number | null = null) => {\n if (hasFocus) {\n return;\n }\n\n if (typeof newValue !== 'number') {\n setStringValue('');\n\n return;\n }\n\n const parsedValue = parseFloatAndRound({ stringValue: newValue?.toString() });\n\n setStringValue(formateNumber({ number: parsedValue, isMoneyInput }));\n },\n [hasFocus, isMoneyInput]\n );\n\n const onLocalChange = (event: ChangeEvent<HTMLInputElement>) => {\n const newValue = event.target.value;\n\n const sanitizedValue = newValue\n // Removes everything except numbers, commas and points\n .replace(NUMBER_CLEAR_REGEX, '')\n // Calculations need points for decimal indication\n .replace(',', '.');\n\n if (sanitizedValue.length === 0) {\n setStringValue('');\n\n if (typeof onChange === 'function') {\n onChange?.(null);\n }\n\n return;\n }\n\n // Allows numbers, one (comma/point) and any number of decimal places\n if (isDecimalInput && DECIMAL_TEST.test(sanitizedValue)) {\n const parsedNumber = parseFloatAndRound({ stringValue: sanitizedValue });\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue.replace('.', ','));\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n\n return;\n }\n\n // Allows numbers, one (comma/point) and 2 numbers of decimal places\n if (isMoneyInput && MONEY_TEST.test(sanitizedValue)) {\n const parsedNumber = parseFloatAndRound({\n stringValue: sanitizedValue,\n decimals: 2,\n });\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue.replace('.', ','));\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n\n return;\n }\n\n // Allows numbers but excludes numbers with leading 0\n if (INTEGER_TEST.test(sanitizedValue)) {\n const parsedNumber = Number(sanitizedValue);\n\n if (parsedNumber > maxNumber) {\n return;\n }\n\n setStringValue(sanitizedValue);\n\n if (typeof onChange === 'function') {\n onChange(parsedNumber);\n }\n }\n };\n\n const onLocalBlur = () => {\n setHasFocus(false);\n\n const sanitizedValue = stringValue.length === 0 ? '0' : stringValue;\n const parsedValue = parseFloatAndRound({ stringValue: sanitizedValue });\n\n setStringValue(\n stringValue.length === 0\n ? ''\n : formateNumber({\n number: parsedValue,\n isMoneyInput,\n })\n );\n\n if (typeof onBlur === 'function') {\n onBlur(parsedValue === 0 ? null : parsedValue);\n }\n };\n\n const onFocus = () => {\n setHasFocus(true);\n\n setStringValue(stringValue.replaceAll('.', ''));\n };\n\n useEffect(() => {\n handleChange(number);\n }, [handleChange, number]);\n\n return (\n <Input\n inputMode=\"decimal\"\n onChange={onLocalChange}\n value={stringValue}\n placeholder={localPlaceholder}\n onBlur={onLocalBlur}\n onFocus={onFocus}\n isDisabled={isDisabled}\n />\n );\n};\n\nNumberInput.displayName = 'NumberInput';\n\nexport default NumberInput;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAAmE,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,SAAAG,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAV,wBAAAM,GAAA,EAAAI,WAAA,SAAAA,WAAA,IAAAJ,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAQ,KAAA,GAAAL,wBAAA,CAAAC,WAAA,OAAAI,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAT,GAAA,YAAAQ,KAAA,CAAAE,GAAA,CAAAV,GAAA,SAAAW,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAhB,GAAA,QAAAgB,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAnB,GAAA,EAAAgB,GAAA,SAAAI,IAAA,GAAAR,qBAAA,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AAM,CAAC;EACxB,CAAC,EAAE,CAACY,YAAY,EAAEZ,MAAM,CAAC,CAAC;EAE1B,oBACInC,MAAA,CAAAS,OAAA,CAAAiE,aAAA,CAACvE,MAAA,CAAAM,OAAK;IACFkE,SAAS,EAAC,SAAS;IACnBpC,QAAQ,EAAEiB,aAAc;IACxBG,KAAK,EAAEnB,WAAY;IACnBJ,WAAW,EAAES,gBAAiB;IAC9BR,MAAM,EAAEiC,WAAY;IACpBC,OAAO,EAAEA,OAAQ;IACjBjC,UAAU,EAAEA;EAAW,CAC1B,CAAC;AAEV,CAAC;AAEDT,WAAW,CAAC+C,WAAW,GAAG,aAAa;AAAC,IAAAC,QAAA,GAEzBhD,WAAW;AAAAiD,OAAA,CAAArE,OAAA,GAAAoE,QAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chayns-components/core",
3
- "version": "5.0.0-beta.271",
3
+ "version": "5.0.0-beta.273",
4
4
  "description": "A set of beautiful React components for developing your own applications with chayns.",
5
5
  "keywords": [
6
6
  "chayns",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  },
68
- "gitHead": "8ae780d07dfb9296e5f32014e7aaef972c13b41f"
68
+ "gitHead": "928a3fd01336f971dcb8c6108f71904a64274f80"
69
69
  }