@helpdice/ui 2.1.8 → 2.1.9

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 (100) hide show
  1. package/dist/auto-complete/index.js +2147 -8
  2. package/dist/button/index.js +2 -2
  3. package/dist/carousal/index.js +2 -2
  4. package/dist/container/box.d.ts +42 -0
  5. package/dist/container/index.d.ts +2 -0
  6. package/dist/container/index.js +948 -0
  7. package/dist/copy-to-clipboard/clipboard.d.ts +13 -1
  8. package/dist/copy-to-clipboard/index.d.ts +1 -1
  9. package/dist/copy-to-clipboard/index.js +142 -157
  10. package/dist/grid/index.d.ts +2 -3
  11. package/dist/grid/index.js +0 -1
  12. package/dist/index.d.ts +3 -4
  13. package/dist/index.js +15389 -15350
  14. package/dist/input/index.d.ts +4 -0
  15. package/dist/input/index.js +2151 -9
  16. package/dist/input/phone-input/phone.d.ts +13 -0
  17. package/dist/modal/index.js +2 -2
  18. package/dist/placeholder/index.js +643 -8
  19. package/dist/table/index.js +3515 -1368
  20. package/dist/text/child.d.ts +1 -0
  21. package/dist/text/index.js +686 -8
  22. package/dist/text/text.d.ts +1 -0
  23. package/esm/button/button.js +2 -2
  24. package/esm/container/box.d.ts +42 -0
  25. package/esm/container/box.js +39 -0
  26. package/esm/container/index.d.ts +2 -0
  27. package/esm/container/index.js +2 -0
  28. package/esm/copy-to-clipboard/clipboard.d.ts +13 -1
  29. package/esm/copy-to-clipboard/clipboard.js +91 -0
  30. package/esm/copy-to-clipboard/index.d.ts +1 -1
  31. package/esm/copy-to-clipboard/index.js +1 -1
  32. package/esm/grid/index.d.ts +2 -3
  33. package/esm/grid/index.js +5 -3
  34. package/esm/index.d.ts +3 -4
  35. package/esm/index.js +4 -3
  36. package/esm/input/index.d.ts +4 -0
  37. package/esm/input/index.js +4 -0
  38. package/esm/input/phone-input/phone.d.ts +13 -0
  39. package/esm/input/phone-input/phone.js +166 -0
  40. package/esm/text/child.d.ts +1 -0
  41. package/esm/text/text.d.ts +1 -0
  42. package/esm/text/text.js +25 -3
  43. package/package.json +8 -15
  44. package/dist/currency-input/index.js +0 -862
  45. package/dist/phone-input/index.js +0 -2033
  46. package/dist/phone-input/phone.d.ts +0 -11
  47. package/esm/phone-input/phone.d.ts +0 -11
  48. package/esm/phone-input/phone.js +0 -161
  49. /package/dist/{currency-input → input/currency-input}/components/CurrencyInput.d.ts +0 -0
  50. /package/dist/{currency-input → input/currency-input}/components/CurrencyInputProps.d.ts +0 -0
  51. /package/dist/{currency-input → input/currency-input}/components/utils/cleanValue.d.ts +0 -0
  52. /package/dist/{currency-input → input/currency-input}/components/utils/escapeRegExp.d.ts +0 -0
  53. /package/dist/{currency-input → input/currency-input}/components/utils/fixedDecimalValue.d.ts +0 -0
  54. /package/dist/{currency-input → input/currency-input}/components/utils/formatValue.d.ts +0 -0
  55. /package/dist/{currency-input → input/currency-input}/components/utils/getLocaleConfig.d.ts +0 -0
  56. /package/dist/{currency-input → input/currency-input}/components/utils/getSuffix.d.ts +0 -0
  57. /package/dist/{currency-input → input/currency-input}/components/utils/index.d.ts +0 -0
  58. /package/dist/{currency-input → input/currency-input}/components/utils/isNumber.d.ts +0 -0
  59. /package/dist/{currency-input → input/currency-input}/components/utils/padTrimValue.d.ts +0 -0
  60. /package/dist/{currency-input → input/currency-input}/components/utils/parseAbbrValue.d.ts +0 -0
  61. /package/dist/{currency-input → input/currency-input}/components/utils/removeInvalidChars.d.ts +0 -0
  62. /package/dist/{currency-input → input/currency-input}/components/utils/removeSeparators.d.ts +0 -0
  63. /package/dist/{currency-input → input/currency-input}/components/utils/repositionCursor.d.ts +0 -0
  64. /package/dist/{currency-input → input/currency-input}/index.d.ts +0 -0
  65. /package/dist/{phone-input → input/phone-input}/index.d.ts +0 -0
  66. /package/esm/{currency-input → input/currency-input}/components/CurrencyInput.d.ts +0 -0
  67. /package/esm/{currency-input → input/currency-input}/components/CurrencyInput.js +0 -0
  68. /package/esm/{currency-input → input/currency-input}/components/CurrencyInputProps.d.ts +0 -0
  69. /package/esm/{currency-input → input/currency-input}/components/CurrencyInputProps.js +0 -0
  70. /package/esm/{currency-input → input/currency-input}/components/utils/addSeparators.js +0 -0
  71. /package/esm/{currency-input → input/currency-input}/components/utils/cleanValue.d.ts +0 -0
  72. /package/esm/{currency-input → input/currency-input}/components/utils/cleanValue.js +0 -0
  73. /package/esm/{currency-input → input/currency-input}/components/utils/escapeRegExp.d.ts +0 -0
  74. /package/esm/{currency-input → input/currency-input}/components/utils/escapeRegExp.js +0 -0
  75. /package/esm/{currency-input → input/currency-input}/components/utils/fixedDecimalValue.d.ts +0 -0
  76. /package/esm/{currency-input → input/currency-input}/components/utils/fixedDecimalValue.js +0 -0
  77. /package/esm/{currency-input → input/currency-input}/components/utils/formatValue.d.ts +0 -0
  78. /package/esm/{currency-input → input/currency-input}/components/utils/formatValue.js +0 -0
  79. /package/esm/{currency-input → input/currency-input}/components/utils/getLocaleConfig.d.ts +0 -0
  80. /package/esm/{currency-input → input/currency-input}/components/utils/getLocaleConfig.js +0 -0
  81. /package/esm/{currency-input → input/currency-input}/components/utils/getSuffix.d.ts +0 -0
  82. /package/esm/{currency-input → input/currency-input}/components/utils/getSuffix.js +0 -0
  83. /package/esm/{currency-input → input/currency-input}/components/utils/index.d.ts +0 -0
  84. /package/esm/{currency-input → input/currency-input}/components/utils/index.js +0 -0
  85. /package/esm/{currency-input → input/currency-input}/components/utils/isNumber.d.ts +0 -0
  86. /package/esm/{currency-input → input/currency-input}/components/utils/isNumber.js +0 -0
  87. /package/esm/{currency-input → input/currency-input}/components/utils/padTrimValue.d.ts +0 -0
  88. /package/esm/{currency-input → input/currency-input}/components/utils/padTrimValue.js +0 -0
  89. /package/esm/{currency-input → input/currency-input}/components/utils/parseAbbrValue.d.ts +0 -0
  90. /package/esm/{currency-input → input/currency-input}/components/utils/parseAbbrValue.js +0 -0
  91. /package/esm/{currency-input → input/currency-input}/components/utils/removeInvalidChars.d.ts +0 -0
  92. /package/esm/{currency-input → input/currency-input}/components/utils/removeInvalidChars.js +0 -0
  93. /package/esm/{currency-input → input/currency-input}/components/utils/removeSeparators.d.ts +0 -0
  94. /package/esm/{currency-input → input/currency-input}/components/utils/removeSeparators.js +0 -0
  95. /package/esm/{currency-input → input/currency-input}/components/utils/repositionCursor.d.ts +0 -0
  96. /package/esm/{currency-input → input/currency-input}/components/utils/repositionCursor.js +0 -0
  97. /package/esm/{currency-input → input/currency-input}/index.d.ts +0 -0
  98. /package/esm/{currency-input → input/currency-input}/index.js +0 -0
  99. /package/esm/{phone-input → input/phone-input}/index.d.ts +0 -0
  100. /package/esm/{phone-input → input/phone-input}/index.js +0 -0
@@ -1,862 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
- var theme = require('@helpdice/theme');
7
-
8
- function _arrayLikeToArray(r, a) {
9
- (null == a || a > r.length) && (a = r.length);
10
- for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
11
- return n;
12
- }
13
- function _arrayWithHoles(r) {
14
- if (Array.isArray(r)) return r;
15
- }
16
- function _arrayWithoutHoles(r) {
17
- if (Array.isArray(r)) return _arrayLikeToArray(r);
18
- }
19
- function _defineProperty(e, r, t) {
20
- return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
21
- value: t,
22
- enumerable: true,
23
- configurable: true,
24
- writable: true
25
- }) : e[r] = t, e;
26
- }
27
- function _extends() {
28
- return _extends = Object.assign ? Object.assign.bind() : function (n) {
29
- for (var e = 1; e < arguments.length; e++) {
30
- var t = arguments[e];
31
- for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
32
- }
33
- return n;
34
- }, _extends.apply(null, arguments);
35
- }
36
- function _iterableToArray(r) {
37
- if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
38
- }
39
- function _iterableToArrayLimit(r, l) {
40
- var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
41
- if (null != t) {
42
- var e,
43
- n,
44
- i,
45
- u,
46
- a = [],
47
- f = true,
48
- o = false;
49
- try {
50
- if (i = (t = t.call(r)).next, 0 === l) {
51
- if (Object(t) !== t) return;
52
- f = !1;
53
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
54
- } catch (r) {
55
- o = true, n = r;
56
- } finally {
57
- try {
58
- if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
59
- } finally {
60
- if (o) throw n;
61
- }
62
- }
63
- return a;
64
- }
65
- }
66
- function _nonIterableRest() {
67
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
68
- }
69
- function _nonIterableSpread() {
70
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
71
- }
72
- function ownKeys(e, r) {
73
- var t = Object.keys(e);
74
- if (Object.getOwnPropertySymbols) {
75
- var o = Object.getOwnPropertySymbols(e);
76
- r && (o = o.filter(function (r) {
77
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
78
- })), t.push.apply(t, o);
79
- }
80
- return t;
81
- }
82
- function _objectSpread2(e) {
83
- for (var r = 1; r < arguments.length; r++) {
84
- var t = null != arguments[r] ? arguments[r] : {};
85
- r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
86
- _defineProperty(e, r, t[r]);
87
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
88
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
89
- });
90
- }
91
- return e;
92
- }
93
- function _objectWithoutProperties(e, t) {
94
- if (null == e) return {};
95
- var o,
96
- r,
97
- i = _objectWithoutPropertiesLoose(e, t);
98
- if (Object.getOwnPropertySymbols) {
99
- var n = Object.getOwnPropertySymbols(e);
100
- for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
101
- }
102
- return i;
103
- }
104
- function _objectWithoutPropertiesLoose(r, e) {
105
- if (null == r) return {};
106
- var t = {};
107
- for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
108
- if (-1 !== e.indexOf(n)) continue;
109
- t[n] = r[n];
110
- }
111
- return t;
112
- }
113
- function _slicedToArray(r, e) {
114
- return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
115
- }
116
- function _toConsumableArray(r) {
117
- return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
118
- }
119
- function _toPrimitive(t, r) {
120
- if ("object" != typeof t || !t) return t;
121
- var e = t[Symbol.toPrimitive];
122
- if (void 0 !== e) {
123
- var i = e.call(t, r);
124
- if ("object" != typeof i) return i;
125
- throw new TypeError("@@toPrimitive must return a primitive value.");
126
- }
127
- return ("string" === r ? String : Number)(t);
128
- }
129
- function _toPropertyKey(t) {
130
- var i = _toPrimitive(t, "string");
131
- return "symbol" == typeof i ? i : i + "";
132
- }
133
- function _unsupportedIterableToArray(r, a) {
134
- if (r) {
135
- if ("string" == typeof r) return _arrayLikeToArray(r, a);
136
- var t = {}.toString.call(r).slice(8, -1);
137
- return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
138
- }
139
- }
140
-
141
- /**
142
- * Escape regex char
143
- *
144
- * See: https://stackoverflow.com/questions/17885855/use-dynamic-variable-string-as-regex-pattern-in-javascript
145
- */
146
- var escapeRegExp = function escapeRegExp(stringToGoIntoTheRegex) {
147
- return stringToGoIntoTheRegex.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
148
- };
149
-
150
- var abbrMap = {
151
- k: 1000,
152
- m: 1000000,
153
- b: 1000000000
154
- };
155
-
156
- /**
157
- * Parse a value with abbreviation e.g 1k = 1000
158
- */
159
- var parseAbbrValue = function parseAbbrValue(value) {
160
- var decimalSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
161
- var reg = new RegExp("(\\d+(".concat(escapeRegExp(decimalSeparator), "\\d*)?)([kmb])$"), 'i');
162
- var match = value.match(reg);
163
- if (match) {
164
- var _match = _slicedToArray(match, 4),
165
- digits = _match[1],
166
- abbr = _match[3];
167
- var multiplier = abbrMap[abbr.toLowerCase()];
168
- return Number(digits.replace(decimalSeparator, '.')) * multiplier;
169
- }
170
- return undefined;
171
- };
172
-
173
- /**
174
- * Remove group separator from value eg. 1,000 > 1000
175
- */
176
- var removeSeparators = function removeSeparators(value) {
177
- var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';
178
- var reg = new RegExp(escapeRegExp(separator), 'g');
179
- return value.replace(reg, '');
180
- };
181
-
182
- /**
183
- * Remove invalid characters
184
- */
185
- var removeInvalidChars = function removeInvalidChars(value, validChars) {
186
- var chars = escapeRegExp(validChars.join(''));
187
- var reg = new RegExp("[^\\d".concat(chars, "]"), 'gi');
188
- return value.replace(reg, '');
189
- };
190
-
191
- /**
192
- * Remove prefix, separators and extra decimals from value
193
- */
194
- var cleanValue = function cleanValue(_ref) {
195
- var value = _ref.value,
196
- _ref$groupSeparator = _ref.groupSeparator,
197
- groupSeparator = _ref$groupSeparator === void 0 ? ',' : _ref$groupSeparator,
198
- _ref$decimalSeparator = _ref.decimalSeparator,
199
- decimalSeparator = _ref$decimalSeparator === void 0 ? '.' : _ref$decimalSeparator,
200
- _ref$allowDecimals = _ref.allowDecimals,
201
- allowDecimals = _ref$allowDecimals === void 0 ? true : _ref$allowDecimals,
202
- _ref$decimalsLimit = _ref.decimalsLimit,
203
- decimalsLimit = _ref$decimalsLimit === void 0 ? 2 : _ref$decimalsLimit,
204
- _ref$allowNegativeVal = _ref.allowNegativeValue,
205
- allowNegativeValue = _ref$allowNegativeVal === void 0 ? true : _ref$allowNegativeVal,
206
- _ref$disableAbbreviat = _ref.disableAbbreviations,
207
- disableAbbreviations = _ref$disableAbbreviat === void 0 ? false : _ref$disableAbbreviat,
208
- _ref$prefix = _ref.prefix,
209
- prefix = _ref$prefix === void 0 ? '' : _ref$prefix,
210
- _ref$transformRawValu = _ref.transformRawValue,
211
- transformRawValue = _ref$transformRawValu === void 0 ? function (rawValue) {
212
- return rawValue;
213
- } : _ref$transformRawValu;
214
- var transformedValue = transformRawValue(value);
215
- if (transformedValue === '-') {
216
- return transformedValue;
217
- }
218
- var abbreviations = disableAbbreviations ? [] : ['k', 'm', 'b'];
219
- var reg = new RegExp("((^|\\D)-\\d)|(-".concat(escapeRegExp(prefix), ")"));
220
- var isNegative = reg.test(transformedValue);
221
-
222
- // Is there a digit before the prefix? eg. 1$
223
- var _ref2 = RegExp("(\\d+)-?".concat(escapeRegExp(prefix))).exec(value) || [],
224
- _ref3 = _slicedToArray(_ref2, 2),
225
- prefixWithValue = _ref3[0],
226
- preValue = _ref3[1];
227
- var withoutPrefix = prefix ? prefixWithValue ? transformedValue.replace(prefixWithValue, '').concat(preValue) : transformedValue.replace(prefix, '') : transformedValue;
228
- var withoutSeparators = removeSeparators(withoutPrefix, groupSeparator);
229
- var withoutInvalidChars = removeInvalidChars(withoutSeparators, [groupSeparator, decimalSeparator].concat(abbreviations));
230
- var valueOnly = withoutInvalidChars;
231
- if (!disableAbbreviations) {
232
- // disallow letter without number
233
- if (abbreviations.some(function (letter) {
234
- return letter === withoutInvalidChars.toLowerCase().replace(decimalSeparator, '');
235
- })) {
236
- return '';
237
- }
238
- var parsed = parseAbbrValue(withoutInvalidChars, decimalSeparator);
239
- if (parsed) {
240
- valueOnly = String(parsed);
241
- }
242
- }
243
- var includeNegative = isNegative && allowNegativeValue ? '-' : '';
244
- if (decimalSeparator && valueOnly.includes(decimalSeparator)) {
245
- var _withoutInvalidChars$ = withoutInvalidChars.split(decimalSeparator),
246
- _withoutInvalidChars$2 = _slicedToArray(_withoutInvalidChars$, 2),
247
- _int = _withoutInvalidChars$2[0],
248
- decimals = _withoutInvalidChars$2[1];
249
- var trimmedDecimals = decimalsLimit && decimals ? decimals.slice(0, decimalsLimit) : decimals;
250
- var includeDecimals = allowDecimals ? "".concat(decimalSeparator).concat(trimmedDecimals) : '';
251
- return "".concat(includeNegative).concat(_int).concat(includeDecimals);
252
- }
253
- return "".concat(includeNegative).concat(valueOnly);
254
- };
255
-
256
- var fixedDecimalValue = function fixedDecimalValue(value, decimalSeparator, fixedDecimalLength) {
257
- if (fixedDecimalLength !== undefined && value.length > 1) {
258
- if (fixedDecimalLength === 0) {
259
- return value.replace(decimalSeparator, '');
260
- }
261
- if (value.includes(decimalSeparator)) {
262
- var _value$split = value.split(decimalSeparator),
263
- _value$split2 = _slicedToArray(_value$split, 2),
264
- _int = _value$split2[0],
265
- decimals = _value$split2[1];
266
- if (decimals.length === fixedDecimalLength) {
267
- return value;
268
- }
269
- if (decimals.length > fixedDecimalLength) {
270
- return "".concat(_int).concat(decimalSeparator).concat(decimals.slice(0, fixedDecimalLength));
271
- }
272
- }
273
- var reg = value.length > fixedDecimalLength ? new RegExp("(\\d+)(\\d{".concat(fixedDecimalLength, "})")) : new RegExp("(\\d)(\\d+)");
274
- var match = value.match(reg);
275
- if (match) {
276
- var _match = _slicedToArray(match, 3),
277
- _int2 = _match[1],
278
- _decimals = _match[2];
279
- return "".concat(_int2).concat(decimalSeparator).concat(_decimals);
280
- }
281
- }
282
- return value;
283
- };
284
-
285
- var getSuffix = function getSuffix(value, _ref) {
286
- var _ref$groupSeparator = _ref.groupSeparator,
287
- groupSeparator = _ref$groupSeparator === void 0 ? ',' : _ref$groupSeparator,
288
- _ref$decimalSeparator = _ref.decimalSeparator,
289
- decimalSeparator = _ref$decimalSeparator === void 0 ? '.' : _ref$decimalSeparator;
290
- var suffixReg = new RegExp("\\d([^".concat(escapeRegExp(groupSeparator)).concat(escapeRegExp(decimalSeparator), "0-9]+)"));
291
- var suffixMatch = value.match(suffixReg);
292
- return suffixMatch ? suffixMatch[1] : undefined;
293
- };
294
-
295
- /**
296
- * Format value with decimal separator, group separator and prefix
297
- */
298
- var formatValue = function formatValue(options) {
299
- var _value = options.value,
300
- decimalSeparator = options.decimalSeparator,
301
- intlConfig = options.intlConfig,
302
- decimalScale = options.decimalScale,
303
- _options$prefix = options.prefix,
304
- prefix = _options$prefix === void 0 ? '' : _options$prefix,
305
- _options$suffix = options.suffix,
306
- suffix = _options$suffix === void 0 ? '' : _options$suffix;
307
- if (_value === '' || _value === undefined) {
308
- return '';
309
- }
310
- if (_value === '-') {
311
- return '-';
312
- }
313
- var isNegative = new RegExp("^\\d?-".concat(prefix ? "".concat(escapeRegExp(prefix), "?") : '', "\\d")).test(_value);
314
- var value = decimalSeparator !== '.' ? replaceDecimalSeparator(_value, decimalSeparator, isNegative) : _value;
315
- if (decimalSeparator && decimalSeparator !== '-' && value.startsWith(decimalSeparator)) {
316
- value = '0' + value;
317
- }
318
- var defaultNumberFormatOptions = {
319
- minimumFractionDigits: decimalScale || 0,
320
- maximumFractionDigits: 20
321
- };
322
- var numberFormatter = intlConfig ? new Intl.NumberFormat(intlConfig.locale, intlConfig.currency ? _objectSpread2(_objectSpread2({}, defaultNumberFormatOptions), {}, {
323
- style: 'currency',
324
- currency: intlConfig.currency
325
- }) : defaultNumberFormatOptions) : new Intl.NumberFormat(undefined, defaultNumberFormatOptions);
326
- var parts = numberFormatter.formatToParts(Number(value));
327
- var formatted = replaceParts(parts, options);
328
-
329
- // Does intl formatting add a suffix?
330
- var intlSuffix = getSuffix(formatted, _objectSpread2({}, options));
331
-
332
- // Include decimal separator if user input ends with decimal separator
333
- var includeDecimalSeparator = _value.slice(-1) === decimalSeparator ? decimalSeparator : '';
334
- var _ref = value.match(RegExp('\\d+\\.(\\d+)')) || [],
335
- _ref2 = _slicedToArray(_ref, 2),
336
- decimals = _ref2[1];
337
-
338
- // Keep original decimal padding if no decimalScale
339
- if (decimalScale === undefined && decimals && decimalSeparator) {
340
- if (formatted.includes(decimalSeparator)) {
341
- formatted = formatted.replace(RegExp("(\\d+)(".concat(escapeRegExp(decimalSeparator), ")(\\d+)"), 'g'), "$1$2".concat(decimals));
342
- } else {
343
- if (intlSuffix && !suffix) {
344
- formatted = formatted.replace(intlSuffix, "".concat(decimalSeparator).concat(decimals).concat(intlSuffix));
345
- } else {
346
- formatted = "".concat(formatted).concat(decimalSeparator).concat(decimals);
347
- }
348
- }
349
- }
350
- if (suffix && includeDecimalSeparator) {
351
- return "".concat(formatted).concat(includeDecimalSeparator).concat(suffix);
352
- }
353
- if (intlSuffix && includeDecimalSeparator) {
354
- return formatted.replace(intlSuffix, "".concat(includeDecimalSeparator).concat(intlSuffix));
355
- }
356
- if (intlSuffix && suffix) {
357
- return formatted.replace(intlSuffix, "".concat(includeDecimalSeparator).concat(suffix));
358
- }
359
- return [formatted, includeDecimalSeparator, suffix].join('');
360
- };
361
-
362
- /**
363
- * Before converting to Number, decimal separator has to be .
364
- */
365
- var replaceDecimalSeparator = function replaceDecimalSeparator(value, decimalSeparator, isNegative) {
366
- var newValue = value;
367
- if (decimalSeparator && decimalSeparator !== '.') {
368
- newValue = newValue.replace(RegExp(escapeRegExp(decimalSeparator), 'g'), '.');
369
- if (isNegative && decimalSeparator === '-') {
370
- newValue = "-".concat(newValue.slice(1));
371
- }
372
- }
373
- return newValue;
374
- };
375
- var replaceParts = function replaceParts(parts, _ref3) {
376
- var prefix = _ref3.prefix,
377
- groupSeparator = _ref3.groupSeparator,
378
- decimalSeparator = _ref3.decimalSeparator,
379
- decimalScale = _ref3.decimalScale,
380
- _ref3$disableGroupSep = _ref3.disableGroupSeparators,
381
- disableGroupSeparators = _ref3$disableGroupSep === void 0 ? false : _ref3$disableGroupSep;
382
- return parts.reduce(function (prev, _ref4, i) {
383
- var type = _ref4.type,
384
- value = _ref4.value;
385
- if (i === 0 && prefix) {
386
- if (type === 'minusSign') {
387
- return [value, prefix];
388
- }
389
- if (type === 'currency') {
390
- return [].concat(_toConsumableArray(prev), [prefix]);
391
- }
392
- return [prefix, value];
393
- }
394
- if (type === 'currency') {
395
- return prefix ? prev : [].concat(_toConsumableArray(prev), [value]);
396
- }
397
- if (type === 'group') {
398
- return !disableGroupSeparators ? [].concat(_toConsumableArray(prev), [groupSeparator !== undefined ? groupSeparator : value]) : prev;
399
- }
400
- if (type === 'decimal') {
401
- if (decimalScale !== undefined && decimalScale === 0) {
402
- return prev;
403
- }
404
- return [].concat(_toConsumableArray(prev), [decimalSeparator !== undefined ? decimalSeparator : value]);
405
- }
406
- if (type === 'fraction') {
407
- return [].concat(_toConsumableArray(prev), [decimalScale !== undefined ? value.slice(0, decimalScale) : value]);
408
- }
409
- return [].concat(_toConsumableArray(prev), [value]);
410
- }, ['']).join('');
411
- };
412
-
413
- var defaultConfig = {
414
- currencySymbol: '',
415
- groupSeparator: '',
416
- decimalSeparator: '',
417
- prefix: '',
418
- suffix: ''
419
- };
420
-
421
- /**
422
- * Get locale config from input or default
423
- */
424
- var getLocaleConfig = function getLocaleConfig(intlConfig) {
425
- var _ref = intlConfig || {},
426
- locale = _ref.locale,
427
- currency = _ref.currency;
428
- var numberFormatter = locale ? new Intl.NumberFormat(locale, currency ? {
429
- currency: currency,
430
- style: 'currency'
431
- } : undefined) : new Intl.NumberFormat();
432
- return numberFormatter.formatToParts(1000.1).reduce(function (prev, curr, i) {
433
- if (curr.type === 'currency') {
434
- if (i === 0) {
435
- return _objectSpread2(_objectSpread2({}, prev), {}, {
436
- currencySymbol: curr.value,
437
- prefix: curr.value
438
- });
439
- } else {
440
- return _objectSpread2(_objectSpread2({}, prev), {}, {
441
- currencySymbol: curr.value,
442
- suffix: curr.value
443
- });
444
- }
445
- }
446
- if (curr.type === 'group') {
447
- return _objectSpread2(_objectSpread2({}, prev), {}, {
448
- groupSeparator: curr.value
449
- });
450
- }
451
- if (curr.type === 'decimal') {
452
- return _objectSpread2(_objectSpread2({}, prev), {}, {
453
- decimalSeparator: curr.value
454
- });
455
- }
456
- return prev;
457
- }, defaultConfig);
458
- };
459
-
460
- var isNumber = function isNumber(input) {
461
- return RegExp(/\d/, 'gi').test(input);
462
- };
463
-
464
- var padTrimValue = function padTrimValue(value) {
465
- var decimalSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
466
- var decimalScale = arguments.length > 2 ? arguments[2] : undefined;
467
- if (decimalScale === undefined || value === '' || value === undefined) {
468
- return value;
469
- }
470
- if (!value.match(/\d/g)) {
471
- return '';
472
- }
473
- var _value$split = value.split(decimalSeparator),
474
- _value$split2 = _slicedToArray(_value$split, 2),
475
- _int = _value$split2[0],
476
- decimals = _value$split2[1];
477
- if (decimalScale === 0) {
478
- return _int;
479
- }
480
- var newValue = decimals || '';
481
- if (newValue.length < decimalScale) {
482
- while (newValue.length < decimalScale) {
483
- newValue += '0';
484
- }
485
- } else {
486
- newValue = newValue.slice(0, decimalScale);
487
- }
488
- return "".concat(_int).concat(decimalSeparator).concat(newValue);
489
- };
490
-
491
- /**
492
- * Based on the last key stroke and the cursor position, update the value
493
- * and reposition the cursor to the right place
494
- */
495
- var repositionCursor = function repositionCursor(_ref) {
496
- var selectionStart = _ref.selectionStart,
497
- value = _ref.value,
498
- lastKeyStroke = _ref.lastKeyStroke,
499
- stateValue = _ref.stateValue,
500
- groupSeparator = _ref.groupSeparator;
501
- var cursorPosition = selectionStart;
502
- var modifiedValue = value;
503
- if (stateValue && cursorPosition) {
504
- var splitValue = value.split('');
505
- // if cursor is to right of groupSeparator and backspace pressed, delete the character to the left of the separator and reposition the cursor
506
- if (lastKeyStroke === 'Backspace' && stateValue[cursorPosition] === groupSeparator) {
507
- splitValue.splice(cursorPosition - 1, 1);
508
- cursorPosition -= 1;
509
- }
510
- // if cursor is to left of groupSeparator and delete pressed, delete the character to the right of the separator and reposition the cursor
511
- if (lastKeyStroke === 'Delete' && stateValue[cursorPosition] === groupSeparator) {
512
- splitValue.splice(cursorPosition, 1);
513
- cursorPosition += 1;
514
- }
515
- modifiedValue = splitValue.join('');
516
- return {
517
- modifiedValue: modifiedValue,
518
- cursorPosition: cursorPosition
519
- };
520
- }
521
- return {
522
- modifiedValue: modifiedValue,
523
- cursorPosition: selectionStart
524
- };
525
- };
526
-
527
- var _excluded = ["allowDecimals", "allowNegativeValue", "id", "name", "className", "customInput", "decimalsLimit", "defaultValue", "disabled", "maxLength", "value", "onValueChange", "fixedDecimalLength", "placeholder", "decimalScale", "prefix", "suffix", "intlConfig", "step", "min", "max", "disableGroupSeparators", "disableAbbreviations", "decimalSeparator", "groupSeparator", "onChange", "onFocus", "onBlur", "onKeyDown", "onKeyUp", "transformRawValue", "formatValueOnBlur", "children"];
528
- var InputCurrency = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
529
- var _ref$allowDecimals = _ref.allowDecimals,
530
- allowDecimals = _ref$allowDecimals === void 0 ? true : _ref$allowDecimals,
531
- _ref$allowNegativeVal = _ref.allowNegativeValue,
532
- allowNegativeValue = _ref$allowNegativeVal === void 0 ? true : _ref$allowNegativeVal,
533
- id = _ref.id,
534
- name = _ref.name,
535
- className = _ref.className,
536
- customInput = _ref.customInput,
537
- decimalsLimit = _ref.decimalsLimit,
538
- defaultValue = _ref.defaultValue,
539
- _ref$disabled = _ref.disabled,
540
- disabled = _ref$disabled === void 0 ? false : _ref$disabled,
541
- userMaxLength = _ref.maxLength,
542
- userValue = _ref.value,
543
- onValueChange = _ref.onValueChange,
544
- fixedDecimalLength = _ref.fixedDecimalLength,
545
- placeholder = _ref.placeholder,
546
- decimalScale = _ref.decimalScale,
547
- prefix = _ref.prefix,
548
- suffix = _ref.suffix,
549
- intlConfig = _ref.intlConfig,
550
- step = _ref.step,
551
- min = _ref.min,
552
- max = _ref.max,
553
- _ref$disableGroupSepa = _ref.disableGroupSeparators,
554
- disableGroupSeparators = _ref$disableGroupSepa === void 0 ? false : _ref$disableGroupSepa,
555
- _ref$disableAbbreviat = _ref.disableAbbreviations,
556
- disableAbbreviations = _ref$disableAbbreviat === void 0 ? false : _ref$disableAbbreviat,
557
- _decimalSeparator = _ref.decimalSeparator,
558
- _groupSeparator = _ref.groupSeparator,
559
- onChange = _ref.onChange,
560
- onFocus = _ref.onFocus,
561
- onBlur = _ref.onBlur,
562
- onKeyDown = _ref.onKeyDown,
563
- onKeyUp = _ref.onKeyUp,
564
- transformRawValue = _ref.transformRawValue,
565
- _ref$formatValueOnBlu = _ref.formatValueOnBlur,
566
- formatValueOnBlur = _ref$formatValueOnBlu === void 0 ? true : _ref$formatValueOnBlu,
567
- children = _ref.children,
568
- props = _objectWithoutProperties(_ref, _excluded);
569
- var theme$1 = theme.useTheme();
570
- if (_decimalSeparator && isNumber(_decimalSeparator)) {
571
- throw new Error('decimalSeparator cannot be a number');
572
- }
573
- if (_groupSeparator && isNumber(_groupSeparator)) {
574
- throw new Error('groupSeparator cannot be a number');
575
- }
576
- var localeConfig = React.useMemo(function () {
577
- return getLocaleConfig(intlConfig);
578
- }, [intlConfig]);
579
- var decimalSeparator = _decimalSeparator || localeConfig.decimalSeparator || '';
580
- var groupSeparator = _groupSeparator || localeConfig.groupSeparator || '';
581
- if (decimalSeparator && groupSeparator && decimalSeparator === groupSeparator && disableGroupSeparators === false) {
582
- throw new Error('decimalSeparator cannot be the same as groupSeparator');
583
- }
584
- var formatValueOptions = {
585
- decimalSeparator: decimalSeparator,
586
- groupSeparator: groupSeparator,
587
- disableGroupSeparators: disableGroupSeparators,
588
- intlConfig: intlConfig,
589
- prefix: prefix || localeConfig.prefix,
590
- suffix: suffix
591
- };
592
- var cleanValueOptions = {
593
- decimalSeparator: decimalSeparator,
594
- groupSeparator: groupSeparator,
595
- allowDecimals: allowDecimals,
596
- decimalsLimit: decimalsLimit || fixedDecimalLength || 2,
597
- allowNegativeValue: allowNegativeValue,
598
- disableAbbreviations: disableAbbreviations,
599
- prefix: prefix || localeConfig.prefix,
600
- transformRawValue: transformRawValue
601
- };
602
- var _useState = React.useState(function () {
603
- return defaultValue != null ? formatValue(_objectSpread2(_objectSpread2({}, formatValueOptions), {}, {
604
- decimalScale: decimalScale,
605
- value: String(defaultValue)
606
- })) : userValue != null ? formatValue(_objectSpread2(_objectSpread2({}, formatValueOptions), {}, {
607
- decimalScale: decimalScale,
608
- value: String(userValue)
609
- })) : '';
610
- }),
611
- _useState2 = _slicedToArray(_useState, 2),
612
- stateValue = _useState2[0],
613
- setStateValue = _useState2[1];
614
- var _useState3 = React.useState(false),
615
- _useState4 = _slicedToArray(_useState3, 2),
616
- dirty = _useState4[0],
617
- setDirty = _useState4[1];
618
- var _useState5 = React.useState(0),
619
- _useState6 = _slicedToArray(_useState5, 2),
620
- cursor = _useState6[0],
621
- setCursor = _useState6[1];
622
- var _useState7 = React.useState(0),
623
- _useState8 = _slicedToArray(_useState7, 2),
624
- changeCount = _useState8[0],
625
- setChangeCount = _useState8[1];
626
- var _useState9 = React.useState(null),
627
- _useState0 = _slicedToArray(_useState9, 2),
628
- lastKeyStroke = _useState0[0],
629
- setLastKeyStroke = _useState0[1];
630
- var inputRef = React.useRef(null);
631
- React.useImperativeHandle(ref, function () {
632
- return inputRef.current;
633
- });
634
-
635
- /**
636
- * Process change in value
637
- */
638
- var processChange = function processChange(value, selectionStart) {
639
- setDirty(true);
640
- var _repositionCursor = repositionCursor({
641
- selectionStart: selectionStart,
642
- value: value,
643
- lastKeyStroke: lastKeyStroke,
644
- stateValue: stateValue,
645
- groupSeparator: groupSeparator
646
- }),
647
- modifiedValue = _repositionCursor.modifiedValue,
648
- cursorPosition = _repositionCursor.cursorPosition;
649
- var stringValue = cleanValue(_objectSpread2({
650
- value: modifiedValue
651
- }, cleanValueOptions));
652
- if (userMaxLength && stringValue.replace(/-/g, '').length > userMaxLength) {
653
- return;
654
- }
655
- if (stringValue === '' || stringValue === '-' || stringValue === decimalSeparator) {
656
- onValueChange && onValueChange(undefined, name, {
657
- "float": null,
658
- formatted: '',
659
- value: ''
660
- });
661
- setStateValue(stringValue);
662
- // Always sets cursor after '-' or decimalSeparator input
663
- setCursor(1);
664
- return;
665
- }
666
- var stringValueWithoutSeparator = decimalSeparator ? stringValue.replace(decimalSeparator, '.') : stringValue;
667
- var numberValue = parseFloat(stringValueWithoutSeparator);
668
- var formattedValue = formatValue(_objectSpread2({
669
- value: stringValue
670
- }, formatValueOptions));
671
- if (cursorPosition != null) {
672
- // Prevent cursor jumping
673
- var newCursor = cursorPosition + (formattedValue.length - value.length);
674
- newCursor = newCursor <= 0 ? prefix ? prefix.length : 0 : newCursor;
675
- setCursor(newCursor);
676
- setChangeCount(changeCount + 1);
677
- }
678
- setStateValue(formattedValue);
679
- if (onValueChange) {
680
- var values = {
681
- "float": numberValue,
682
- formatted: formattedValue,
683
- value: stringValue
684
- };
685
- onValueChange(stringValue, name, values);
686
- }
687
- };
688
-
689
- /**
690
- * Handle change event
691
- */
692
- var handleOnChange = function handleOnChange(event) {
693
- var _event$target = event.target,
694
- value = _event$target.value,
695
- selectionStart = _event$target.selectionStart;
696
- processChange(value, selectionStart);
697
- onChange && onChange(event);
698
- };
699
-
700
- /**
701
- * Handle focus event
702
- */
703
- var handleOnFocus = function handleOnFocus(event) {
704
- onFocus && onFocus(event);
705
- return stateValue ? stateValue.length : 0;
706
- };
707
-
708
- /**
709
- * Handle blur event
710
- *
711
- * Format value by padding/trimming decimals if required by
712
- */
713
- var handleOnBlur = function handleOnBlur(event) {
714
- var value = event.target.value;
715
- var valueOnly = cleanValue(_objectSpread2({
716
- value: value
717
- }, cleanValueOptions));
718
- if (valueOnly === '-' || valueOnly === decimalSeparator || !valueOnly) {
719
- setStateValue('');
720
- onBlur && onBlur(event);
721
- return;
722
- }
723
- var fixedDecimals = fixedDecimalValue(valueOnly, decimalSeparator, fixedDecimalLength);
724
- var newValue = padTrimValue(fixedDecimals, decimalSeparator, decimalScale !== undefined ? decimalScale : fixedDecimalLength);
725
- var numberValue = parseFloat(newValue.replace(decimalSeparator, '.'));
726
- var formattedValue = formatValue(_objectSpread2(_objectSpread2({}, formatValueOptions), {}, {
727
- value: newValue
728
- }));
729
- if (onValueChange && formatValueOnBlur) {
730
- onValueChange(newValue, name, {
731
- "float": numberValue,
732
- formatted: formattedValue,
733
- value: newValue
734
- });
735
- }
736
- setStateValue(formattedValue);
737
- onBlur && onBlur(event);
738
- };
739
-
740
- /**
741
- * Handle key down event
742
- *
743
- * Increase or decrease value by step
744
- */
745
- var handleOnKeyDown = function handleOnKeyDown(event) {
746
- var key = event.key;
747
- setLastKeyStroke(key);
748
- if (step && (key === 'ArrowUp' || key === 'ArrowDown')) {
749
- event.preventDefault();
750
- setCursor(stateValue.length);
751
- var currentValue = parseFloat(userValue != null ? String(userValue).replace(decimalSeparator, '.') : cleanValue(_objectSpread2({
752
- value: stateValue
753
- }, cleanValueOptions))) || 0;
754
- var newValue = key === 'ArrowUp' ? currentValue + step : currentValue - step;
755
- if (min !== undefined && newValue < Number(min)) {
756
- return;
757
- }
758
- if (max !== undefined && newValue > Number(max)) {
759
- return;
760
- }
761
- var fixedLength = String(step).includes('.') ? Number(String(step).split('.')[1].length) : undefined;
762
- processChange(String(fixedLength ? newValue.toFixed(fixedLength) : newValue).replace('.', decimalSeparator));
763
- }
764
- onKeyDown && onKeyDown(event);
765
- };
766
-
767
- /**
768
- * Handle key up event
769
- *
770
- * Move cursor if there is a suffix to prevent user typing past suffix
771
- */
772
- var handleOnKeyUp = function handleOnKeyUp(event) {
773
- var key = event.key,
774
- selectionStart = event.currentTarget.selectionStart;
775
- if (key !== 'ArrowUp' && key !== 'ArrowDown' && stateValue !== '-') {
776
- var _suffix = getSuffix(stateValue, {
777
- groupSeparator: groupSeparator,
778
- decimalSeparator: decimalSeparator
779
- });
780
- if (_suffix && selectionStart && selectionStart > stateValue.length - _suffix.length) {
781
- /* istanbul ignore else */
782
- if (inputRef.current) {
783
- var newCursor = stateValue.length - _suffix.length;
784
- inputRef.current.setSelectionRange(newCursor, newCursor);
785
- }
786
- }
787
- }
788
- onKeyUp && onKeyUp(event);
789
- };
790
-
791
- // Update state if userValue changes to undefined
792
- React.useEffect(function () {
793
- if (userValue == null && defaultValue == null) {
794
- setStateValue('');
795
- }
796
- }, [defaultValue, userValue]);
797
- React.useEffect(function () {
798
- // prevent cursor jumping if editing value
799
- if (dirty && stateValue !== '-' && inputRef.current && document.activeElement === inputRef.current) {
800
- inputRef.current.setSelectionRange(cursor, cursor);
801
- }
802
- }, [stateValue, cursor, inputRef, dirty, changeCount]);
803
-
804
- /**
805
- * If user has only entered "-" or decimal separator,
806
- * keep the char to allow them to enter next value
807
- */
808
- var getRenderValue = function getRenderValue() {
809
- if (userValue != null && stateValue !== '-' && (!decimalSeparator || stateValue !== decimalSeparator)) {
810
- return formatValue(_objectSpread2(_objectSpread2({}, formatValueOptions), {}, {
811
- decimalScale: dirty ? undefined : decimalScale,
812
- value: String(userValue)
813
- }));
814
- }
815
- return stateValue;
816
- };
817
- var inputProps = _objectSpread2({
818
- type: 'text',
819
- inputMode: 'decimal',
820
- id: id,
821
- name: name,
822
- className: className,
823
- onChange: handleOnChange,
824
- onBlur: handleOnBlur,
825
- onFocus: handleOnFocus,
826
- onKeyDown: handleOnKeyDown,
827
- onKeyUp: handleOnKeyUp,
828
- placeholder: placeholder,
829
- disabled: disabled,
830
- value: getRenderValue(),
831
- ref: inputRef
832
- }, props);
833
- if (customInput) {
834
- var CustomInput = customInput;
835
- return /*#__PURE__*/React.createElement(CustomInput, inputProps);
836
- }
837
- return /*#__PURE__*/React.createElement("div", null, children && /*#__PURE__*/React.createElement("label", {
838
- style: {
839
- display: 'block',
840
- marginBottom: '0.7rem',
841
- fontSize: '14px',
842
- fontWeight: 'light'
843
- }
844
- }, children), /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
845
- style: {
846
- textAlign: 'right',
847
- border: "1px solid ".concat(theme$1.palette.border),
848
- fontSize: '14px',
849
- borderRadius: '5px',
850
- height: '35px',
851
- padding: '5px 10px',
852
- fontWeight: 'light',
853
- width: 'auto',
854
- background: 'transparent',
855
- minWidth: '200px'
856
- }
857
- })));
858
- });
859
-
860
- exports.cleanValue = cleanValue;
861
- exports.default = InputCurrency;
862
- exports.formatValue = formatValue;