@tecsinapse/react-core 1.13.0 → 1.14.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/components/atoms/Input/InputElement/InputElement.d.ts +5 -3
  3. package/dist/components/atoms/Input/InputElement/InputElement.js +48 -7
  4. package/dist/components/atoms/Input/InputElement/InputElement.js.map +1 -1
  5. package/dist/components/atoms/Input/hooks/useNumberMask.d.ts +8 -0
  6. package/dist/components/atoms/Input/hooks/useNumberMask.js +98 -0
  7. package/dist/components/atoms/Input/hooks/useNumberMask.js.map +1 -0
  8. package/dist/components/atoms/Input/hooks/useStringMask.d.ts +8 -0
  9. package/dist/components/atoms/Input/hooks/useStringMask.js +109 -0
  10. package/dist/components/atoms/Input/hooks/useStringMask.js.map +1 -0
  11. package/dist/components/atoms/Input/index.d.ts +2 -3
  12. package/dist/components/atoms/Input/index.js +12 -26
  13. package/dist/components/atoms/Input/index.js.map +1 -1
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +8 -29
  16. package/dist/index.js.map +1 -1
  17. package/dist/utils/formatWithMask.d.ts +3 -0
  18. package/dist/utils/formatWithMask.js +31 -0
  19. package/dist/utils/formatWithMask.js.map +1 -0
  20. package/dist/utils/index.d.ts +2 -0
  21. package/dist/utils/index.js +28 -0
  22. package/dist/utils/index.js.map +1 -1
  23. package/dist/utils/masks.d.ts +11 -0
  24. package/dist/utils/masks.js +19 -0
  25. package/dist/utils/masks.js.map +1 -0
  26. package/package.json +2 -2
  27. package/src/components/atoms/Input/InputElement/InputElement.tsx +75 -10
  28. package/src/components/atoms/Input/hooks/useNumberMask.ts +99 -0
  29. package/src/components/atoms/Input/hooks/useStringMask.ts +128 -0
  30. package/src/components/atoms/Input/index.ts +2 -3
  31. package/src/index.ts +3 -6
  32. package/src/utils/formatWithMask.ts +25 -0
  33. package/src/utils/index.ts +4 -2
  34. package/src/utils/masks.ts +15 -0
  35. package/dist/components/atoms/Input/hooks/useCurrencyMask.d.ts +0 -3
  36. package/dist/components/atoms/Input/hooks/useCurrencyMask.js +0 -69
  37. package/dist/components/atoms/Input/hooks/useCurrencyMask.js.map +0 -1
  38. package/dist/components/atoms/Input/hooks/useMask.d.ts +0 -9
  39. package/dist/components/atoms/Input/hooks/useMask.js +0 -61
  40. package/dist/components/atoms/Input/hooks/useMask.js.map +0 -1
  41. package/dist/components/atoms/Input/masks/index.d.ts +0 -10
  42. package/dist/components/atoms/Input/masks/index.js +0 -18
  43. package/dist/components/atoms/Input/masks/index.js.map +0 -1
  44. package/src/components/atoms/Input/hooks/useCurrencyMask.ts +0 -74
  45. package/src/components/atoms/Input/hooks/useMask.ts +0 -92
  46. package/src/components/atoms/Input/masks/index.ts +0 -12
package/CHANGELOG.md CHANGED
@@ -3,6 +3,48 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.14.3](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.14.2...@tecsinapse/react-core@1.14.3) (2022-02-16)
7
+
8
+ **Note:** Version bump only for package @tecsinapse/react-core
9
+
10
+
11
+
12
+
13
+
14
+ ## [1.14.2](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.14.1...@tecsinapse/react-core@1.14.2) (2022-02-15)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * Input initializan for string and no mask. ([78353ef](https://github.com/tecsinapse/design-system/commit/78353ef7b4d24216a1b99eab800f8d448ff21a34))
20
+
21
+
22
+
23
+
24
+
25
+ ## [1.14.1](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.14.0...@tecsinapse/react-core@1.14.1) (2022-02-15)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * Input initialization. ([a48c2d6](https://github.com/tecsinapse/design-system/commit/a48c2d6d8d1ee8d1282fa5082af40c6a1bce3e4b))
31
+ * Input initialization. ([b0be51f](https://github.com/tecsinapse/design-system/commit/b0be51f7699a3936b7d3d193437a2636bad7abc7))
32
+
33
+
34
+
35
+
36
+
37
+ # [1.14.0](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.13.0...@tecsinapse/react-core@1.14.0) (2022-02-14)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * formatWithMask util. Refactoring on useNumberMask and useStringMask hooks. ([76920f6](https://github.com/tecsinapse/design-system/commit/76920f62fb8cf9070b9b2e477dc720e9b7b24077))
43
+
44
+
45
+
46
+
47
+
6
48
  # [1.13.0](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.12.10...@tecsinapse/react-core@1.13.0) (2022-02-09)
7
49
 
8
50
  **Note:** Version bump only for package @tecsinapse/react-core
@@ -1,12 +1,14 @@
1
1
  import React, { FC } from 'react';
2
2
  import { StyleProp, TextInputProps, TextStyle } from 'react-native';
3
- import { IMask } from '../hooks/useMask';
3
+ import { MaskType } from '../hooks/useStringMask';
4
+ import { CurrencyOptions } from '../hooks/useNumberMask';
4
5
  export interface InputElementProps extends Omit<TextInputProps, 'onChange' | 'value' | 'ref'> {
5
6
  style?: StyleProp<TextStyle>;
6
- value?: string | IMask;
7
+ value: string | number;
7
8
  placeholder?: string;
8
9
  disabled?: boolean;
9
- onChange?: (value: string) => void;
10
+ onChange?: (value: any) => void;
11
+ mask?: (MaskType[] | ((value: string) => MaskType[])) | CurrencyOptions;
10
12
  onFocus?: () => void;
11
13
  onBlur?: () => void;
12
14
  ref?: React.Ref<any>;
@@ -7,11 +7,17 @@ exports.default = void 0;
7
7
 
8
8
  var _react = require("@emotion/react");
9
9
 
10
- var _react2 = _interopRequireDefault(require("react"));
10
+ var _react2 = _interopRequireWildcard(require("react"));
11
11
 
12
12
  var _styled = require("../styled");
13
13
 
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+ var _useStringMask = require("../hooks/useStringMask");
15
+
16
+ var _useNumberMask = require("../hooks/useNumberMask");
17
+
18
+ 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); }
19
+
20
+ 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; }
15
21
 
16
22
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
17
23
 
@@ -21,19 +27,54 @@ const InputElement = _react2.default.forwardRef(({
21
27
  value,
22
28
  disabled = false,
23
29
  placeholderTextColor,
30
+ mask,
24
31
  ...rest
25
32
  }, ref) => {
26
- var _value$maskValue;
27
-
28
33
  const theme = (0, _react.useTheme)();
29
-
30
- const _value = typeof value === 'string' ? value : value === null || value === void 0 ? void 0 : (_value$maskValue = value.maskValue) === null || _value$maskValue === void 0 ? void 0 : _value$maskValue.formatted;
34
+ const [valueReinitialized, setValueReinitialized] = (0, _react2.useState)(false);
31
35
 
32
36
  const _placeholderColor = placeholderTextColor || theme.font.color.dark;
33
37
 
38
+ const getInputHook = () => {
39
+ if (mask !== undefined) {
40
+ if (Array.isArray(mask) || typeof mask === 'function') {
41
+ return (0, _useStringMask.useStringMask)(mask, value ?? '');
42
+ } else {
43
+ return (0, _useNumberMask.useNumberMask)(mask, value ?? '');
44
+ }
45
+ } else {
46
+ return [undefined, undefined];
47
+ }
48
+ };
49
+
50
+ const [maskValue, setMaskValue] = getInputHook();
51
+
52
+ const _value = maskValue !== undefined ? (maskValue === null || maskValue === void 0 ? void 0 : maskValue.formatted) ?? '' : (value === null || value === void 0 ? void 0 : value.toString()) ?? '';
53
+
54
+ const onChangeMaskValue = (0, _react2.useCallback)(() => {
55
+ if (onChange) {
56
+ onChange(maskValue === null || maskValue === void 0 ? void 0 : maskValue.raw);
57
+ }
58
+ }, [maskValue]);
59
+ const onChangeValue = (0, _react2.useCallback)(async value => {
60
+ if (maskValue !== undefined && setMaskValue !== undefined) {
61
+ await setMaskValue(value);
62
+ onChangeMaskValue();
63
+ } else onChange && onChange(value);
64
+ }, [value]);
65
+ (0, _react2.useEffect)(() => {
66
+ if (!valueReinitialized) {
67
+ if (maskValue !== undefined && setMaskValue !== undefined && value !== undefined && typeof maskValue === 'object') {
68
+ if (maskValue.raw !== value.toString()) {
69
+ setValueReinitialized(true);
70
+ setMaskValue(value);
71
+ }
72
+ }
73
+ }
74
+ }, [value, maskValue, valueReinitialized]);
34
75
  return _react2.default.createElement(_styled.StyledInputElement, _extends({}, rest, {
35
76
  ref: ref,
36
- onChangeText: onChange,
77
+ onChangeText: onChangeValue,
37
78
  value: _value,
38
79
  placeholder: placeholder,
39
80
  placeholderTextColor: _placeholderColor,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/components/atoms/Input/InputElement/InputElement.tsx"],"names":["InputElement","React","forwardRef","onChange","placeholder","value","disabled","placeholderTextColor","rest","ref","theme","_value","maskValue","formatted","_placeholderColor","font","color","dark","displayName"],"mappings":";;;;;;;AAAA;;AAEA;;AAGA;;;;;;AAiBA,MAAMA,YAAmC,GAAGC,gBAAMC,UAAN,CAC1C,CACE;AACEC,EAAAA,QADF;AAEEC,EAAAA,WAFF;AAGEC,EAAAA,KAHF;AAIEC,EAAAA,QAAQ,GAAG,KAJb;AAKEC,EAAAA,oBALF;AAME,KAAGC;AANL,CADF,EASEC,GATF,KAUkB;AAAA;;AAChB,QAAMC,KAAK,GAAG,sBAAd;;AACA,QAAMC,MAAM,GACV,OAAON,KAAP,KAAiB,QAAjB,GAA4BA,KAA5B,GAAoCA,KAApC,aAAoCA,KAApC,2CAAoCA,KAAK,CAAEO,SAA3C,qDAAoC,iBAAkBC,SADxD;;AAEA,QAAMC,iBAAiB,GAAGP,oBAAoB,IAAIG,KAAK,CAACK,IAAN,CAAWC,KAAX,CAAiBC,IAAnE;;AAEA,SACE,8BAAC,0BAAD,eACMT,IADN;AAEE,IAAA,GAAG,EAAEC,GAFP;AAGE,IAAA,YAAY,EAAEN,QAHhB;AAIE,IAAA,KAAK,EAAEQ,MAJT;AAKE,IAAA,WAAW,EAAEP,WALf;AAME,IAAA,oBAAoB,EAAEU,iBANxB;AAOE,IAAA,QAAQ,EAAER,QAPZ;AAQE,IAAA,QAAQ,EAAE,CAACA;AARb,KADF;AAYD,CA7ByC,CAA5C;;AAgCAN,YAAY,CAACkB,WAAb,GAA2B,cAA3B;eAEelB,Y","sourcesContent":["import { useTheme } from '@emotion/react';\nimport { ThemeProp } from '@tecsinapse/react-core';\nimport React, { FC } from 'react';\nimport { StyleProp, TextInputProps, TextStyle } from 'react-native';\nimport { IMask } from '../hooks/useMask';\nimport { StyledInputElement } from '../styled';\n\nexport interface InputElementProps\n extends Omit<TextInputProps, 'onChange' | 'value' | 'ref'> {\n style?: StyleProp<TextStyle>;\n /**\n * TODO:\n */\n value?: string | IMask;\n placeholder?: string;\n disabled?: boolean;\n onChange?: (value: string) => void;\n onFocus?: () => void;\n onBlur?: () => void;\n ref?: React.Ref<any>;\n}\n\nconst InputElement: FC<InputElementProps> = React.forwardRef(\n (\n {\n onChange,\n placeholder,\n value,\n disabled = false,\n placeholderTextColor,\n ...rest\n },\n ref: React.Ref<any>\n ): JSX.Element => {\n const theme = useTheme() as ThemeProp;\n const _value =\n typeof value === 'string' ? value : value?.maskValue?.formatted;\n const _placeholderColor = placeholderTextColor || theme.font.color.dark;\n\n return (\n <StyledInputElement\n {...rest}\n ref={ref}\n onChangeText={onChange}\n value={_value}\n placeholder={placeholder}\n placeholderTextColor={_placeholderColor}\n disabled={disabled}\n editable={!disabled}\n />\n );\n }\n);\n\nInputElement.displayName = 'InputElement';\n\nexport default InputElement;\n"],"file":"InputElement.js"}
1
+ {"version":3,"sources":["../../../../../src/components/atoms/Input/InputElement/InputElement.tsx"],"names":["InputElement","React","forwardRef","onChange","placeholder","value","disabled","placeholderTextColor","mask","rest","ref","theme","valueReinitialized","setValueReinitialized","_placeholderColor","font","color","dark","getInputHook","undefined","Array","isArray","maskValue","setMaskValue","_value","formatted","toString","onChangeMaskValue","raw","onChangeValue","displayName"],"mappings":";;;;;;;AAAA;;AAEA;;AAEA;;AACA;;AACA;;;;;;;;AAuBA,MAAMA,YAAmC,GAAGC,gBAAMC,UAAN,CAC1C,CACE;AACEC,EAAAA,QADF;AAEEC,EAAAA,WAFF;AAGEC,EAAAA,KAHF;AAIEC,EAAAA,QAAQ,GAAG,KAJb;AAKEC,EAAAA,oBALF;AAMEC,EAAAA,IANF;AAOE,KAAGC;AAPL,CADF,EAUEC,GAVF,KAWkB;AAChB,QAAMC,KAAK,GAAG,sBAAd;AAGA,QAAM,CAACC,kBAAD,EAAqBC,qBAArB,IAA8C,sBAClD,KADkD,CAApD;;AAIA,QAAMC,iBAAiB,GAAGP,oBAAoB,IAAII,KAAK,CAACI,IAAN,CAAWC,KAAX,CAAiBC,IAAnE;;AAEA,QAAMC,YAAY,GAAG,MAAM;AACzB,QAAIV,IAAI,KAAKW,SAAb,EAAwB;AACtB,UAAIC,KAAK,CAACC,OAAN,CAAcb,IAAd,KAAuB,OAAOA,IAAP,KAAgB,UAA3C,EAAuD;AACrD,eAAO,kCAAcA,IAAd,EAAoBH,KA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D,CAArB;AACAU,UAAAA,YAAY,CAAClB,KAAD,CAAZ;AACD;AACF;AACF;AACF,GAhBD,EAgBG,CAACA,KAAD,EAAQiB,SAAR,EAAmBV,kBAAnB,CAhBH;AAkBA,SACE,8BAAC,0BAAD,eACMH,IADN;AAEE,IAAA,GAAG,EAAEC,GAFP;AAGE,IAAA,YAAY,EAAEmB,aAHhB;AAIE,IAAA,KAAK,EAAEL,MAJT;AAKE,IAAA,WAAW,EAAEpB,WALf;AAME,IAAA,oBAAoB,EAAEU,iBANxB;AAOE,IAAA,QAAQ,EAAER,QAPZ;AAQE,IAAA,QAAQ,EAAE,CAACA;AARb,KADF;AAYD,CAvFyC,CAA5C;;AA0FAN,YAAY,CAAC8B,WAAb,GAA2B,cAA3B;eAEe9B,Y","sourcesContent":["import { useTheme } from '@emotion/react';\nimport { ThemeProp } from '@tecsinapse/react-core';\nimport React, { FC, useCallback, useEffect, useState } from 'react';\nimport { StyleProp, TextInputProps, TextStyle } from 'react-native';\nimport { StyledInputElement } from '../styled';\nimport { MaskType, useStringMask } from '../hooks/useStringMask';\nimport { CurrencyOptions, useNumberMask } from '../hooks/useNumberMask';\n\nexport interface InputElementProps\n extends Omit<TextInputProps, 'onChange' | 'value' | 'ref'> {\n style?: StyleProp<TextStyle>;\n value: string | number;\n placeholder?: string;\n disabled?: boolean;\n onChange?: (value: any) => void;\n /**\n To use mask for strings you have to pass a MaskType[] or ((value: string) => MaskType[])\n A MaskType can be a string, RegExp, or Array<RegExp>.\n In case we have a string, '9' represents digits, 'a' represents alphabet characters, and any other character represents fixed characters in the mask.\n For example a phone mask can be represented as ['(99) \\\\99999-9999'], or ['(99) ', '/[9]/', '9999-9999'], or ['(', /[0-9]/, /[0-9]/, ') ', '/[9]/', '9999-9999'] and many others.\n To use mask for numbers you have to pass a CurrencyOptions object\n A CurrencyOptions object contains symbol, separator, decimal, precision. For example {symbol: 'R$ ', separator: '.', decimal: ',', precision: 2,}.\n **/\n mask?: (MaskType[] | ((value: string) => MaskType[])) | CurrencyOptions;\n onFocus?: () => void;\n onBlur?: () => void;\n ref?: React.Ref<any>;\n}\n\nconst InputElement: FC<InputElementProps> = React.forwardRef(\n (\n {\n onChange,\n placeholder,\n value,\n disabled = false,\n placeholderTextColor,\n mask,\n ...rest\n },\n ref: React.Ref<any>\n ): JSX.Element => {\n const theme = useTheme() as ThemeProp;\n\n /** Check if value was reinitialized, without this state we can't total \"erase\" a value that was already reinitialized **/\n const [valueReinitialized, setValueReinitialized] = useState<boolean>(\n false\n );\n\n const _placeholderColor = placeholderTextColor || theme.font.color.dark;\n\n const getInputHook = () => {\n if (mask !== undefined) {\n if (Array.isArray(mask) || typeof mask === 'function') {\n return useStringMask(mask, value ?? '');\n } else {\n return useNumberMask(mask, value ?? '');\n }\n } else {\n return [undefined, undefined];\n }\n };\n\n const [maskValue, setMaskValue] = getInputHook();\n\n const _value =\n maskValue !== undefined\n ? maskValue?.formatted ?? ''\n : value?.toString() ?? '';\n\n const onChangeMaskValue = useCallback(() => {\n if (onChange) {\n onChange(maskValue?.raw);\n }\n }, [maskValue]);\n\n const onChangeValue = useCallback(\n async (value: string | number) => {\n if (maskValue !== undefined && setMaskValue !== undefined) {\n await setMaskValue(value);\n onChangeMaskValue();\n } else onChange && onChange(value);\n },\n [value]\n );\n\n useEffect(() => {\n if (!valueReinitialized) {\n /** Used to reinitialize maskValue with a value that was loaded after Input was rendered **/\n if (\n maskValue !== undefined &&\n setMaskValue !== undefined &&\n value !== undefined &&\n typeof maskValue === 'object'\n ) {\n /** Case there is a mask **/\n if (maskValue.raw !== value.toString()) {\n setValueReinitialized(true);\n setMaskValue(value);\n }\n }\n }\n }, [value, maskValue, valueReinitialized]);\n\n return (\n <StyledInputElement\n {...rest}\n ref={ref}\n onChangeText={onChangeValue}\n value={_value}\n placeholder={placeholder}\n placeholderTextColor={_placeholderColor}\n disabled={disabled}\n editable={!disabled}\n />\n );\n }\n);\n\nInputElement.displayName = 'InputElement';\n\nexport default InputElement;\n"],"file":"InputElement.js"}
@@ -0,0 +1,8 @@
1
+ import currency from 'currency.js';
2
+ import { MaskValue } from './useStringMask';
3
+ export declare type CurrencyOptions = currency.Options;
4
+ export declare const getInternalNumberAndMask: (value: string | number, options?: currency.Options | undefined) => {
5
+ internalNumber: number;
6
+ mergedOptions: CurrencyOptions;
7
+ };
8
+ export declare const useNumberMask: (options?: currency.Options | undefined, defaultValue?: string | number | undefined) => [MaskValue, (value: string | number) => void];
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useNumberMask = exports.getInternalNumberAndMask = void 0;
7
+
8
+ var _currency = _interopRequireDefault(require("currency.js"));
9
+
10
+ var _react = require("react");
11
+
12
+ var _utils = require("../../../../utils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const DEFAULT_OPTIONS = {
17
+ symbol: 'R$ ',
18
+ separator: '.',
19
+ decimal: ',',
20
+ precision: 2
21
+ };
22
+
23
+ const getRegex = precision => new RegExp(`\\B(?=(\\d{${precision}})(?!\\d))`, 'g');
24
+
25
+ const getInternalNumberAndMask = (value, options) => {
26
+ const mergedOptions = { ...DEFAULT_OPTIONS,
27
+ ...options
28
+ };
29
+ const {
30
+ precision = -1
31
+ } = mergedOptions;
32
+ let internalNumber;
33
+
34
+ if (typeof value === 'number') {
35
+ if (precision) {
36
+ let stringValue = String(value);
37
+ const decimalIndex = stringValue.indexOf('.');
38
+ const currentPrecision = decimalIndex + precision;
39
+
40
+ if (decimalIndex !== -1 && currentPrecision <= stringValue.length) {
41
+ const zeros = stringValue.length + 1 - currentPrecision;
42
+
43
+ for (let i = 0; i < zeros; i++) stringValue = stringValue + `0`;
44
+ }
45
+
46
+ internalNumber = Number(stringValue);
47
+ } else {
48
+ internalNumber = value;
49
+ }
50
+ } else {
51
+ const onlyNumbers = String((0, _utils.extractNumbersFromString)(value));
52
+ const padZeros = String(onlyNumbers).padStart(precision + 1, '0');
53
+ internalNumber = Number(padZeros.replace(getRegex(precision), '.'));
54
+ }
55
+
56
+ if (internalNumber > Number.MAX_SAFE_INTEGER) {
57
+ internalNumber = Number.MAX_SAFE_INTEGER;
58
+ }
59
+
60
+ if (internalNumber < Number.MIN_SAFE_INTEGER) {
61
+ internalNumber = Number.MIN_SAFE_INTEGER;
62
+ }
63
+
64
+ return {
65
+ internalNumber,
66
+ mergedOptions
67
+ };
68
+ };
69
+
70
+ exports.getInternalNumberAndMask = getInternalNumberAndMask;
71
+
72
+ const useNumberMask = (options, defaultValue) => {
73
+ const applyMask = (0, _react.useCallback)((value = 0) => {
74
+ const {
75
+ internalNumber,
76
+ mergedOptions
77
+ } = getInternalNumberAndMask(value, options);
78
+ return {
79
+ raw: internalNumber,
80
+ formatted: (0, _currency.default)(internalNumber).format(mergedOptions)
81
+ };
82
+ }, [options, getRegex]);
83
+ const [value, setValue] = (0, _react.useState)(applyMask(defaultValue));
84
+ const handleChangeValue = (0, _react.useCallback)(formattedValue => {
85
+ const {
86
+ raw,
87
+ formatted
88
+ } = applyMask(formattedValue);
89
+ setValue({
90
+ raw,
91
+ formatted
92
+ });
93
+ }, [applyMask, setValue]);
94
+ return [value, handleChangeValue];
95
+ };
96
+
97
+ exports.useNumberMask = useNumberMask;
98
+ //# sourceMappingURL=useNumberMask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/components/atoms/Input/hooks/useNumberMask.ts"],"names":["DEFAULT_OPTIONS","symbol","separator","decimal","precision","getRegex","RegExp","getInternalNumberAndMask","value","options","mergedOptions","internalNumber","stringValue","String","decimalIndex","indexOf","currentPrecision","length","zeros","i","Number","onlyNumbers","padZeros","padStart","replace","MAX_SAFE_INTEGER","MIN_SAFE_INTEGER","useNumberMask","defaultValue","applyMask","raw","formatted","format","setValue","handleChangeValue","formattedValue"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;AAKA,MAAMA,eAAgC,GAAG;AACvCC,EA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APuB,EAQxB,CAACF,SAAD,EAAYI,QAAZ,CARwB,CAA1B;AAWA,SAAO,CAACzB,KAAD,EAAQ0B,iBAAR,CAAP;AACD,CAjCM","sourcesContent":["import currency from 'currency.js';\nimport { useCallback, useState } from 'react';\nimport { extractNumbersFromString } from '../../../../utils';\nimport { MaskValue } from './useStringMask';\n\nexport type CurrencyOptions = currency.Options;\n\nconst DEFAULT_OPTIONS: CurrencyOptions = {\n symbol: 'R$ ',\n separator: '.',\n decimal: ',',\n precision: 2,\n};\n\nconst getRegex = (precision: number) =>\n new RegExp(`\\\\B(?=(\\\\d{${precision}})(?!\\\\d))`, 'g');\n\nexport const getInternalNumberAndMask = (\n value: string | number,\n options?: CurrencyOptions\n): { internalNumber: number; mergedOptions: CurrencyOptions } => {\n const mergedOptions = { ...DEFAULT_OPTIONS, ...options };\n const { precision = -1 } = mergedOptions;\n\n let internalNumber;\n\n if (typeof value === 'number') {\n if (precision) {\n let stringValue = String(value);\n const decimalIndex = stringValue.indexOf('.');\n const currentPrecision = decimalIndex + precision;\n if (decimalIndex !== -1 && currentPrecision <= stringValue.length) {\n const zeros = stringValue.length + 1 - currentPrecision;\n for (let i = 0; i < zeros; i++) stringValue = stringValue + `0`;\n }\n internalNumber = Number(stringValue);\n } else {\n internalNumber = value;\n }\n } else {\n const onlyNumbers = String(extractNumbersFromString(value));\n const padZeros = String(onlyNumbers).padStart(precision + 1, '0');\n internalNumber = Number(padZeros.replace(getRegex(precision), '.'));\n }\n\n if (internalNumber > Number.MAX_SAFE_INTEGER) {\n internalNumber = Number.MAX_SAFE_INTEGER;\n }\n\n if (internalNumber < Number.MIN_SAFE_INTEGER) {\n internalNumber = Number.MIN_SAFE_INTEGER;\n }\n\n return {\n internalNumber,\n mergedOptions,\n };\n};\n\n/**\n * TODO:\n * @param options\n * @param defaultValue\n * @returns\n */\nexport const useNumberMask = (\n options?: CurrencyOptions,\n defaultValue?: string | number\n): [MaskValue, (value: string | number) => void] => {\n const applyMask = useCallback(\n (value: string | number = 0): MaskValue => {\n const { internalNumber, mergedOptions } = getInternalNumberAndMask(\n value,\n options\n );\n\n return {\n raw: internalNumber,\n formatted: currency(internalNumber).format(mergedOptions),\n };\n },\n [options, getRegex]\n );\n\n const [value, setValue] = useState<MaskValue>(applyMask(defaultValue));\n\n const handleChangeValue = useCallback(\n (formattedValue: string | number) => {\n const { raw, formatted } = applyMask(formattedValue);\n setValue({\n raw,\n formatted,\n });\n },\n [applyMask, setValue]\n );\n\n return [value, handleChangeValue];\n};\n"],"file":"useNumberMask.js"}
@@ -0,0 +1,8 @@
1
+ export interface MaskValue {
2
+ formatted?: string;
3
+ raw?: string | number;
4
+ }
5
+ export declare type MaskType = string | RegExp | Array<RegExp>;
6
+ export declare const mergeMask: (value: string | undefined, mask: MaskType[]) => MaskValue;
7
+ export declare const getMask: (mask: MaskType[] | ((value: string) => MaskType[]), newValue: string) => MaskType[];
8
+ export declare const useStringMask: (mask: MaskType[] | ((value: string) => MaskType[]), defaultValue?: string | number | undefined) => [MaskValue, (text: string | number) => void];
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useStringMask = exports.getMask = exports.mergeMask = void 0;
7
+
8
+ var _react = require("react");
9
+
10
+ const mergeMask = (value = '', mask) => {
11
+ let formatted = '';
12
+ let raw = '';
13
+ let iMask = 0;
14
+ let iChars = 0;
15
+
16
+ while (!(iMask === mask.length || iChars === value.length)) {
17
+ const maskChar = mask[iMask];
18
+ const valueChar = value[iChars];
19
+
20
+ if (maskChar === valueChar) {
21
+ formatted += maskChar;
22
+ iChars++;
23
+ iMask++;
24
+ continue;
25
+ }
26
+
27
+ const rawValueChar = value[iChars];
28
+
29
+ if (typeof maskChar === 'object') {
30
+ iChars++;
31
+ const maskCharRegex = Array.isArray(maskChar) ? maskChar[0] : maskChar;
32
+ const matchRegex = RegExp(maskCharRegex).test(valueChar);
33
+
34
+ if (matchRegex) {
35
+ formatted += valueChar;
36
+ raw += rawValueChar;
37
+ iMask++;
38
+ }
39
+ } else {
40
+ formatted += maskChar;
41
+ iMask++;
42
+ }
43
+ }
44
+
45
+ return {
46
+ raw,
47
+ formatted
48
+ };
49
+ };
50
+
51
+ exports.mergeMask = mergeMask;
52
+
53
+ const getMask = (mask, newValue) => {
54
+ let maskArray;
55
+ const regexArray = [];
56
+
57
+ if (typeof mask === 'function') {
58
+ maskArray = mask(newValue);
59
+ } else {
60
+ maskArray = mask;
61
+ }
62
+
63
+ maskArray.forEach(exp => {
64
+ if (typeof exp !== 'string') {
65
+ if (Array.isArray(exp)) regexArray.push(exp);else regexArray.push(exp);
66
+ } else {
67
+ for (let i = 0; i < exp.length; i++) {
68
+ if (exp[i] === '\\') {
69
+ regexArray.push(exp[i + 1]);
70
+ i++;
71
+ } else {
72
+ if (exp[i] === '9') regexArray.push(/\d/);else if (exp[i] === 'a') regexArray.push(/[a-zA-Z]/);else regexArray.push(exp[i]);
73
+ }
74
+ }
75
+ }
76
+ });
77
+ return regexArray;
78
+ };
79
+
80
+ exports.getMask = getMask;
81
+
82
+ const useStringMask = (mask, defaultValue) => {
83
+ const applyMask = (0, _react.useCallback)((value = '') => {
84
+ const selectedMask = getMask(mask, value);
85
+ const {
86
+ formatted,
87
+ raw
88
+ } = mergeMask(value, selectedMask);
89
+ return {
90
+ raw,
91
+ formatted
92
+ };
93
+ }, [mask]);
94
+ const [value, setValue] = (0, _react.useState)(applyMask(defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.toString()));
95
+ const handleChangeValue = (0, _react.useCallback)(formattedValue => {
96
+ const {
97
+ raw,
98
+ formatted
99
+ } = applyMask(formattedValue.toString());
100
+ setValue({
101
+ raw,
102
+ formatted
103
+ });
104
+ }, [applyMask, setValue]);
105
+ return [value, handleChangeValue];
106
+ };
107
+
108
+ exports.useStringMask = useStringMask;
109
+ //# sourceMappingURL=useStringMask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/components/atoms/Input/hooks/useStringMask.ts"],"names":["mergeMask","value","mask","formatted","raw","iMask","iChars","length","maskChar","valueChar","rawValueChar","maskCharRegex","Array","isArray","matchRegex","RegExp","test","getMask","newValue","maskArray","regexArray","forEach","exp","push","i","useStringMask","defaultValue","applyMask","selectedMask","setValue","toString","handleChangeValue","formattedValue"],"mappings":";;;;;;;AAAA;;AAeO,MAAMA,SAAS,GAAG,CAACC,KAAK,GAAG,EAAT,EAAaC,IAAb,KAA6C;AACpE,MAAIC,SAAS,GAAG,EAAhB;AACA,MAAIC,GAAG,GAAG,EAAV;AACA,MAAIC,KAAK,GAAG,CAAZ;AACA,MAAIC,MAAM,GAAG,CAAb;;AAEA,SAAO,EAAED,KAAK,KAAKH,IAAI,CAACK,MAAf,IAAyBD,MAAM,KAAKL,KAAK,CAACM,MAA5C,CAAP,EAA4D;AAC1D,UAAMC,QAAQ,GAAGN,IAAI,CAACG,KAAD,CAArB;AACA,UAAMI,SAAS,GAAGR,KAAK,CAACK,MAAD,CAAvB;;AAEA,QAAIE,QAAQ,KAAKC,SAAjB,EAA4B;AAC1BN,MAAAA,SAAS,IAAIK,QAAb;AACAF,MAAAA,MAAM;AACND,MAAAA,KAAK;AACL;AACD;;AAED,UAAMK,YAAY,GAAGT,KAAK,CAACK,MAAD,CAA1B;;AAEA,QAAI,OAAOE,QAAP,KAAoB,QAAxB,EAAkC;AAChCF,MAAAA,MAAM;AAEN,YAAMK,aAAa,GAAGC,KAAK,CAACC,OAAN,CAAcL,QAAd,IAA0BA,QAAQ,CAAC,CAAD,CAAlC,GAAwCA,QAA9D;AACA,YAAMM,UAAU,GAAGC,MAAM,CAACJ,aAAD,CAAN,CAAsBK,IAAtB,CAA2BP,SAA3B,CAAnB;;AAEA,UAAIK,UAAJ,EAAgB;AACdX,QAAAA,SAAS,IAAIM,SAAb;AACAL,QAAAA,GAAG,IAAIM,YAAP;AACAL,QAAAA,KAAK;AACN;AACF,KAXD,MAWO;AACLF,MAAAA,SAAS,IA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CM;;;;AAwCA,MAAMK,aAAa,GAAG,CAC3BvB,IAD2B,EAE3BwB,YAF2B,KAGsB;AACjD,QAAMC,SAAS,GAAG,wBAChB,CAAC1B,KAAK,GAAG,EAAT,KAA2B;AACzB,UAAM2B,YAAY,GAAGX,OAAO,CAACf,IAAD,EAAOD,KAAP,CAA5B;AACA,UAAM;AAAEE,MAAAA,SAAF;AAAaC,MAAAA;AAAb,QAAqBJ,SAAS,CAACC,KAAD,EAAQ2B,YAAR,CAApC;AAEA,WAAO;AACLxB,MAAAA,GADK;AAELD,MAAAA;AAFK,KAAP;AAID,GATe,EAUhB,CAACD,IAAD,CAVgB,CAAlB;AAaA,QAAM,CAACD,KAAD,EAAQ4B,QAAR,IAAoB,qBACxBF,SAAS,CAACD,YAAD,aAACA,YAAD,uBAACA,YAAY,CAAEI,QAAd,EAAD,CADe,CAA1B;AAIA,QAAMC,iBAAiB,GAAG,wBACvBC,cAAD,IAAqC;AACnC,UAAM;AAAE5B,MAAAA,GAAF;AAAOD,MAAAA;AAAP,QAAqBwB,SAAS,CAACK,cAAc,CAACF,QAAf,EAAD,CAApC;AACAD,IAAAA,QAAQ,CAAC;AACPzB,MAAAA,GADO;AAEPD,MAAAA;AAFO,KAAD,CAAR;AAID,GAPuB,EAQxB,CAACwB,SAAD,EAAYE,QAAZ,CARwB,CAA1B;AAWA,SAAO,CAAC5B,KAAD,EAAQ8B,iBAAR,CAAP;AACD,CAjCM","sourcesContent":["import { useCallback, useState } from 'react';\n\nexport interface MaskValue {\n formatted?: string;\n raw?: string | number;\n}\n\nexport type MaskType = string | RegExp | Array<RegExp>;\n\n/**\n * TODO:\n * @param value\n * @param mask\n * @returns\n */\nexport const mergeMask = (value = '', mask: MaskType[]): MaskValue => {\n let formatted = '';\n let raw = '';\n let iMask = 0;\n let iChars = 0;\n\n while (!(iMask === mask.length || iChars === value.length)) {\n const maskChar = mask[iMask];\n const valueChar = value[iChars];\n\n if (maskChar === valueChar) {\n formatted += maskChar;\n iChars++;\n iMask++;\n continue;\n }\n\n const rawValueChar = value[iChars];\n\n if (typeof maskChar === 'object') {\n iChars++;\n\n const maskCharRegex = Array.isArray(maskChar) ? maskChar[0] : maskChar;\n const matchRegex = RegExp(maskCharRegex).test(valueChar);\n\n if (matchRegex) {\n formatted += valueChar;\n raw += rawValueChar;\n iMask++;\n }\n } else {\n formatted += maskChar;\n iMask++;\n }\n }\n\n return { raw, formatted };\n};\n\nexport const getMask = (\n mask: MaskType[] | ((value: string) => MaskType[]),\n newValue: string\n): MaskType[] => {\n let maskArray: MaskType[];\n const regexArray: MaskType[] = [];\n\n if (typeof mask === 'function') {\n maskArray = mask(newValue);\n } else {\n maskArray = mask;\n }\n\n maskArray.forEach(exp => {\n if (typeof exp !== 'string') {\n if (Array.isArray(exp)) regexArray.push(exp);\n else regexArray.push(exp);\n } else {\n for (let i = 0; i < exp.length; i++) {\n if (exp[i] === '\\\\') {\n regexArray.push(exp[i + 1]);\n i++;\n } else {\n if (exp[i] === '9') regexArray.push(/\\d/);\n else if (exp[i] === 'a') regexArray.push(/[a-zA-Z]/);\n else regexArray.push(exp[i]);\n }\n }\n }\n });\n\n return regexArray;\n};\n\n/**\n * TODO:\n * @param mask\n * @param defaultValue\n * @returns\n */\nexport const useStringMask = (\n mask: MaskType[] | ((value: string) => MaskType[]),\n defaultValue?: string | number\n): [MaskValue, (text: string | number) => void] => {\n const applyMask = useCallback(\n (value = ''): MaskValue => {\n const selectedMask = getMask(mask, value);\n const { formatted, raw } = mergeMask(value, selectedMask);\n\n return {\n raw,\n formatted,\n };\n },\n [mask]\n );\n\n const [value, setValue] = useState<MaskValue>(\n applyMask(defaultValue?.toString())\n );\n\n const handleChangeValue = useCallback(\n (formattedValue: string | number) => {\n const { raw, formatted } = applyMask(formattedValue.toString());\n setValue({\n raw,\n formatted,\n });\n },\n [applyMask, setValue]\n );\n\n return [value, handleChangeValue];\n};\n"],"file":"useStringMask.js"}
@@ -1,9 +1,8 @@
1
1
  export { default as Hint } from './Hint';
2
- export * from './hooks/useCurrencyMask';
3
2
  export * from './hooks/useInputFocus';
4
- export * from './hooks/useMask';
3
+ export * from './hooks/useNumberMask';
4
+ export * from './hooks/useStringMask';
5
5
  export { default as InputContainer, InputContainerProps, InputVariantType, } from './InputContainer/InputContainer';
6
6
  export { default as InputElement, InputElementProps, } from './InputElement/InputElement';
7
- export * from './masks';
8
7
  export { PressableInputContainer, PressableInputContainerProps, } from './PressableInputContainer';
9
8
  export { StyledBorderKeeper, disabledInputStyles } from './styled';
@@ -78,20 +78,6 @@ Object.defineProperty(exports, "disabledInputStyles", {
78
78
 
79
79
  var _Hint = _interopRequireDefault(require("./Hint"));
80
80
 
81
- var _useCurrencyMask = require("./hooks/useCurrencyMask");
82
-
83
- Object.keys(_useCurrencyMask).forEach(function (key) {
84
- if (key === "default" || key === "__esModule") return;
85
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
86
- if (key in exports && exports[key] === _useCurrencyMask[key]) return;
87
- Object.defineProperty(exports, key, {
88
- enumerable: true,
89
- get: function () {
90
- return _useCurrencyMask[key];
91
- }
92
- });
93
- });
94
-
95
81
  var _useInputFocus = require("./hooks/useInputFocus");
96
82
 
97
83
  Object.keys(_useInputFocus).forEach(function (key) {
@@ -106,38 +92,38 @@ Object.keys(_useInputFocus).forEach(function (key) {
106
92
  });
107
93
  });
108
94
 
109
- var _useMask = require("./hooks/useMask");
95
+ var _useNumberMask = require("./hooks/useNumberMask");
110
96
 
111
- Object.keys(_useMask).forEach(function (key) {
97
+ Object.keys(_useNumberMask).forEach(function (key) {
112
98
  if (key === "default" || key === "__esModule") return;
113
99
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
114
- if (key in exports && exports[key] === _useMask[key]) return;
100
+ if (key in exports && exports[key] === _useNumberMask[key]) return;
115
101
  Object.defineProperty(exports, key, {
116
102
  enumerable: true,
117
103
  get: function () {
118
- return _useMask[key];
104
+ return _useNumberMask[key];
119
105
  }
120
106
  });
121
107
  });
122
108
 
123
- var _InputContainer = _interopRequireWildcard(require("./InputContainer/InputContainer"));
124
-
125
- var _InputElement = _interopRequireWildcard(require("./InputElement/InputElement"));
126
-
127
- var _masks = require("./masks");
109
+ var _useStringMask = require("./hooks/useStringMask");
128
110
 
129
- Object.keys(_masks).forEach(function (key) {
111
+ Object.keys(_useStringMask).forEach(function (key) {
130
112
  if (key === "default" || key === "__esModule") return;
131
113
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
132
- if (key in exports && exports[key] === _masks[key]) return;
114
+ if (key in exports && exports[key] === _useStringMask[key]) return;
133
115
  Object.defineProperty(exports, key, {
134
116
  enumerable: true,
135
117
  get: function () {
136
- return _masks[key];
118
+ return _useStringMask[key];
137
119
  }
138
120
  });
139
121
  });
140
122
 
123
+ var _InputContainer = _interopRequireWildcard(require("./InputContainer/InputContainer"));
124
+
125
+ var _InputElement = _interopRequireWildcard(require("./InputElement/InputElement"));
126
+
141
127
  var _PressableInputContainer = require("./PressableInputContainer");
142
128
 
143
129
  var _styled = require("./styled");
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/components/atoms/Input/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAKA;;AAIA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAIA","sourcesContent":["export { default as Hint } from './Hint';\nexport * from './hooks/useCurrencyMask';\nexport * from './hooks/useInputFocus';\nexport * from './hooks/useMask';\nexport {\n default as InputContainer,\n InputContainerProps,\n InputVariantType,\n} from './InputContainer/InputContainer';\nexport {\n default as InputElement,\n InputElementProps,\n} from './InputElement/InputElement';\nexport * from './masks';\nexport {\n PressableInputContainer,\n PressableInputContainerProps,\n} from './PressableInputContainer';\nexport { StyledBorderKeeper, disabledInputStyles } from './styled';\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../../../../src/components/atoms/Input/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA;;AAKA;;AAIA;;AAIA","sourcesContent":["export { default as Hint } from './Hint';\nexport * from './hooks/useInputFocus';\nexport * from './hooks/useNumberMask';\nexport * from './hooks/useStringMask';\nexport {\n default as InputContainer,\n InputContainerProps,\n InputVariantType,\n} from './InputContainer/InputContainer';\nexport {\n default as InputElement,\n InputElementProps,\n} from './InputElement/InputElement';\nexport {\n PressableInputContainer,\n PressableInputContainerProps,\n} from './PressableInputContainer';\nexport { StyledBorderKeeper, disabledInputStyles } from './styled';\n"],"file":"index.js"}
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export { Checkbox, CheckboxProps } from './components/atoms/Checkbox';
9
9
  export { Divider, DividerProps } from './components/atoms/Divider';
10
10
  export { GroupButton, GroupButtonOption, GroupButtonOptionProps, GroupButtonProps, GroupButtonValue, } from './components/atoms/GroupButton';
11
11
  export { Icon, IconProps } from './components/atoms/Icon';
12
- export { Hint, InputContainer, InputContainerProps, InputElement, InputElementProps, InputVariantType, Masks, PressableInputContainer, PressableInputContainerProps, StyledBorderKeeper, useCurrencyMask, useInputFocus, useMask, disabledInputStyles, IMask, IMaskValue, } from './components/atoms/Input';
12
+ export { Hint, InputContainer, InputContainerProps, InputElement, InputElementProps, InputVariantType, PressableInputContainer, PressableInputContainerProps, StyledBorderKeeper, useInputFocus, useNumberMask, useStringMask, disabledInputStyles, } from './components/atoms/Input';
13
13
  export { Paper, PaperProps } from './components/atoms/Paper';
14
14
  export { PressableSurface, PressableSurfaceProps, } from './components/atoms/PressableSurface';
15
15
  export { ProgressBar, ProgressBarProps } from './components/atoms/ProgressBar';
@@ -17,7 +17,7 @@ export { RadioButton, RadioButtonProps } from './components/atoms/RadioButton';
17
17
  export { Switch, SwitchProps } from './components/atoms/Switch';
18
18
  export { Tag, TagProps } from './components/atoms/Tag';
19
19
  export { Text, TextProps } from './components/atoms/Text';
20
- export { Calendar, CalendarProps, DateRange, SelectionType, Value } from './components/molecules/Calendar';
20
+ export { Calendar, CalendarProps, DateRange, SelectionType, Value, } from './components/molecules/Calendar';
21
21
  export { DatePicker, DatePickerProps } from './components/molecules/DatePicker';
22
22
  export { DateTimePicker, DateTimePickerProps, } from './components/molecules/DateTimePicker';
23
23
  export { DateTimeSelector, DateTimeSelectorMode, DateTimeSelectorProps, } from './components/molecules/DateTimeSelector';
package/dist/index.js CHANGED
@@ -41,16 +41,13 @@ var _exportNames = {
41
41
  InputElement: true,
42
42
  InputElementProps: true,
43
43
  InputVariantType: true,
44
- Masks: true,
45
44
  PressableInputContainer: true,
46
45
  PressableInputContainerProps: true,
47
46
  StyledBorderKeeper: true,
48
- useCurrencyMask: true,
49
47
  useInputFocus: true,
50
- useMask: true,
48
+ useNumberMask: true,
49
+ useStringMask: true,
51
50
  disabledInputStyles: true,
52
- IMask: true,
53
- IMaskValue: true,
54
51
  Paper: true,
55
52
  PaperProps: true,
56
53
  PressableSurface: true,
@@ -313,12 +310,6 @@ Object.defineProperty(exports, "InputVariantType", {
313
310
  return _Input.InputVariantType;
314
311
  }
315
312
  });
316
- Object.defineProperty(exports, "Masks", {
317
- enumerable: true,
318
- get: function () {
319
- return _Input.Masks;
320
- }
321
- });
322
313
  Object.defineProperty(exports, "PressableInputContainer", {
323
314
  enumerable: true,
324
315
  get: function () {
@@ -337,40 +328,28 @@ Object.defineProperty(exports, "StyledBorderKeeper", {
337
328
  return _Input.StyledBorderKeeper;
338
329
  }
339
330
  });
340
- Object.defineProperty(exports, "useCurrencyMask", {
341
- enumerable: true,
342
- get: function () {
343
- return _Input.useCurrencyMask;
344
- }
345
- });
346
331
  Object.defineProperty(exports, "useInputFocus", {
347
332
  enumerable: true,
348
333
  get: function () {
349
334
  return _Input.useInputFocus;
350
335
  }
351
336
  });
352
- Object.defineProperty(exports, "useMask", {
337
+ Object.defineProperty(exports, "useNumberMask", {
353
338
  enumerable: true,
354
339
  get: function () {
355
- return _Input.useMask;
340
+ return _Input.useNumberMask;
356
341
  }
357
342
  });
358
- Object.defineProperty(exports, "disabledInputStyles", {
359
- enumerable: true,
360
- get: function () {
361
- return _Input.disabledInputStyles;
362
- }
363
- });
364
- Object.defineProperty(exports, "IMask", {
343
+ Object.defineProperty(exports, "useStringMask", {
365
344
  enumerable: true,
366
345
  get: function () {
367
- return _Input.IMask;
346
+ return _Input.useStringMask;
368
347
  }
369
348
  });
370
- Object.defineProperty(exports, "IMaskValue", {
349
+ Object.defineProperty(exports, "disabledInputStyles", {
371
350
  enumerable: true,
372
351
  get: function () {
373
- return _Input.IMaskValue;
352
+ return _Input.disabledInputStyles;
374
353
  }
375
354
  });
376
355
  Object.defineProperty(exports, "Paper", {