@helpdice/ui 2.6.1-beta.1 → 2.6.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,7 +96,7 @@ export type CurrencyInputProps = Overwrite<React.ComponentPropsWithRef<'input'>,
96
96
  /**
97
97
  * Handle change in value
98
98
  */
99
- onValueChange?: (value: string | undefined, name?: string, values?: CurrencyInputOnChangeValues) => void;
99
+ onChange?: (value: string | undefined, name?: string, values?: CurrencyInputOnChangeValues) => void;
100
100
  /**
101
101
  * Placeholder if there is no value
102
102
  */
@@ -1,10 +1,23 @@
1
1
  import { CurrencyInputProps } from "./input-currency-props";
2
2
  import React from "react";
3
3
  type Override<T, U> = Omit<T, keyof U> & U;
4
+ type RangeValue = {
5
+ min: number;
6
+ max: number;
7
+ };
8
+ type CurrencyRangeOutput = {
9
+ min: number;
10
+ max: number;
11
+ currency: string;
12
+ };
4
13
  type CurrencyRangeProps = Override<CurrencyInputProps, {
5
- onChange: (val: any) => void;
14
+ value?: RangeValue;
15
+ defaultValue?: RangeValue;
16
+ onChange?: (val: CurrencyRangeOutput) => void;
17
+ debounce?: number;
6
18
  error?: boolean;
7
19
  helperText?: string;
8
20
  }>;
9
- declare function CurrencyRangeInput({ prefix, allowDecimals, onChange, intlConfig, children, error, helperText }: CurrencyRangeProps): React.JSX.Element;
10
- export default CurrencyRangeInput;
21
+ declare function CurrencyRangeInput({ prefix, allowDecimals, intlConfig, children, value, defaultValue, onChange, debounce, error: externalError, helperText: externalHelperText, }: CurrencyRangeProps): React.JSX.Element;
22
+ declare const _default: React.MemoExoticComponent<typeof CurrencyRangeInput>;
23
+ export default _default;
@@ -2,83 +2,89 @@ import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import Grid from "../grid";
4
4
  import Input from ".";
5
- import React, { useEffect } from "react";
5
+ import React, { useMemo, useState, useEffect, useCallback } from "react";
6
6
  import InputBlockLabel, { InputError } from "./input-block-label";
7
- function parseCurrency(value) {
8
- var _trimmed$match;
9
- var trimmed = value.trim();
10
-
11
- // Extract currency symbol
12
- var currencySymbol = ((_trimmed$match = trimmed.match(/[^\d.,\s]/)) === null || _trimmed$match === void 0 ? void 0 : _trimmed$match[0]) || "";
13
-
14
- // Remove everything except digits, comma, dot
15
- var numericPart = trimmed.replace(/[^\d.,]/g, "");
16
-
17
- // Convert comma to dot if needed
18
- var normalized = numericPart.replace(",", ".");
19
- return {
20
- amount: Number(normalized),
21
- currency: currencySymbol
22
- };
23
- }
24
7
  function CurrencyRangeInput(_ref) {
25
8
  var prefix = _ref.prefix,
26
9
  allowDecimals = _ref.allowDecimals,
27
- onChange = _ref.onChange,
28
10
  intlConfig = _ref.intlConfig,
29
11
  children = _ref.children,
30
- error = _ref.error,
31
- helperText = _ref.helperText;
32
- var _React$useState = React.useState({
33
- min: '0',
34
- max: '0',
35
- currency: ''
36
- }),
37
- _React$useState2 = _slicedToArray(_React$useState, 2),
38
- value = _React$useState2[0],
39
- setValue = _React$useState2[1];
40
- useEffect(function () {
41
- var min = parseCurrency(value.min);
42
- var max = parseCurrency(value.max);
43
- if (max.amount > min.amount) {
44
- if (onChange) onChange({
45
- min: min.amount,
46
- max: max.amount,
47
- currency: min.currency
48
- });
12
+ value = _ref.value,
13
+ _ref$defaultValue = _ref.defaultValue,
14
+ defaultValue = _ref$defaultValue === void 0 ? {
15
+ min: 0,
16
+ max: 0
17
+ } : _ref$defaultValue,
18
+ onChange = _ref.onChange,
19
+ _ref$debounce = _ref.debounce,
20
+ debounce = _ref$debounce === void 0 ? 300 : _ref$debounce,
21
+ externalError = _ref.error,
22
+ externalHelperText = _ref.helperText;
23
+ var isControlled = value !== undefined;
24
+ var _useState = useState(defaultValue),
25
+ _useState2 = _slicedToArray(_useState, 2),
26
+ internalValue = _useState2[0],
27
+ setInternalValue = _useState2[1];
28
+ var currentValue = isControlled ? value : internalValue;
29
+ var setValue = useCallback(function (val) {
30
+ if (!isControlled) {
31
+ setInternalValue(val);
49
32
  }
50
- }, [value]);
33
+ }, [isControlled]);
34
+
35
+ // Validation
36
+ var isInvalid = useMemo(function () {
37
+ if (!currentValue.min || !currentValue.max) return false;
38
+ return currentValue.max <= currentValue.min;
39
+ }, [currentValue]);
40
+
41
+ // Debounced onChange
42
+ useEffect(function () {
43
+ if (!onChange || isInvalid) return;
44
+ var handler = setTimeout(function () {
45
+ if (currentValue.min && currentValue.max) {
46
+ onChange({
47
+ min: currentValue.min,
48
+ max: currentValue.max,
49
+ currency: ''
50
+ });
51
+ }
52
+ }, debounce);
53
+ return function () {
54
+ return clearTimeout(handler);
55
+ };
56
+ }, [isInvalid, onChange, debounce, currentValue]);
57
+ var finalError = externalError !== null && externalError !== void 0 ? externalError : isInvalid;
58
+ var finalHelperText = externalHelperText !== null && externalHelperText !== void 0 ? externalHelperText : isInvalid ? "Max must be greater than min" : undefined;
51
59
  return /*#__PURE__*/React.createElement("div", {
52
60
  className: "with-label"
53
61
  }, children && /*#__PURE__*/React.createElement(InputBlockLabel, null, children), /*#__PURE__*/React.createElement(Grid.Container, {
54
62
  gap: 2
55
63
  }, /*#__PURE__*/React.createElement(Grid, null, /*#__PURE__*/React.createElement(Input.Currency, {
56
- onChange: function onChange(e) {
57
- return setValue(function (prev) {
58
- return _extends({}, prev, {
59
- min: e.target.value
60
- });
61
- });
64
+ value: currentValue.min,
65
+ onChange: function onChange(val) {
66
+ return setValue(_extends({}, currentValue, {
67
+ min: Number(val)
68
+ }));
62
69
  },
63
70
  intlConfig: intlConfig,
64
71
  prefix: prefix,
65
72
  placeholder: "Min",
66
73
  allowDecimals: allowDecimals
67
74
  })), /*#__PURE__*/React.createElement(Grid, null, /*#__PURE__*/React.createElement(Input.Currency, {
68
- onChange: function onChange(e) {
69
- return setValue(function (prev) {
70
- return _extends({}, prev, {
71
- max: e.target.value
72
- });
73
- });
75
+ value: currentValue.max,
76
+ onChange: function onChange(val) {
77
+ return setValue(_extends({}, currentValue, {
78
+ max: Number(val)
79
+ }));
74
80
  },
75
81
  intlConfig: intlConfig,
76
82
  prefix: prefix,
77
83
  placeholder: "Max",
78
84
  allowDecimals: allowDecimals
79
- }))), helperText && /*#__PURE__*/React.createElement(InputError, {
80
- error: error,
81
- text: helperText
85
+ }))), finalHelperText && /*#__PURE__*/React.createElement(InputError, {
86
+ error: finalError,
87
+ text: finalHelperText
82
88
  }));
83
89
  }
84
- export default CurrencyRangeInput;
90
+ export default /*#__PURE__*/React.memo(CurrencyRangeInput);
@@ -1,7 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4
- var _excluded = ["allowDecimals", "allowNegativeValue", "id", "name", "className", "customInput", "decimalsLimit", "defaultValue", "disabled", "maxLength", "value", "onValueChange", "fixedDecimalLength", "placeholder", "decimalScale", "prefix", "suffix", "intlConfig", "fullWidth", "step", "min", "max", "disableGroupSeparators", "disableAbbreviations", "decimalSeparator", "groupSeparator", "onChange", "onFocus", "onBlur", "onKeyDown", "onKeyUp", "transformRawValue", "formatValueOnBlur", "children"];
4
+ var _excluded = ["allowDecimals", "allowNegativeValue", "id", "name", "className", "customInput", "decimalsLimit", "defaultValue", "disabled", "maxLength", "value", "fixedDecimalLength", "placeholder", "decimalScale", "prefix", "suffix", "intlConfig", "fullWidth", "step", "min", "max", "disableGroupSeparators", "disableAbbreviations", "decimalSeparator", "groupSeparator", "onChange", "onFocus", "onBlur", "onKeyDown", "onKeyUp", "transformRawValue", "formatValueOnBlur", "children"];
5
5
  import _JSXStyle from "../styled-jsx.es.js";
6
6
  import React, { useState, useEffect, useRef, forwardRef, useMemo, useImperativeHandle } from 'react';
7
7
  import { isNumber, cleanValue, fixedDecimalValue, formatValue, getLocaleConfig, padTrimValue, getSuffix, repositionCursor } from './utils';
@@ -23,7 +23,6 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
23
23
  disabled = _ref$disabled === void 0 ? false : _ref$disabled,
24
24
  userMaxLength = _ref.maxLength,
25
25
  userValue = _ref.value,
26
- onValueChange = _ref.onValueChange,
27
26
  fixedDecimalLength = _ref.fixedDecimalLength,
28
27
  placeholder = _ref.placeholder,
29
28
  decimalScale = _ref.decimalScale,
@@ -136,11 +135,15 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
136
135
  var stringValue = cleanValue(_extends({
137
136
  value: modifiedValue
138
137
  }, cleanValueOptions));
138
+
139
+ // console.log("Before clean:", modifiedValue);
140
+ // console.log("After clean:", stringValue);
141
+
139
142
  if (userMaxLength && stringValue.replace(/-/g, '').length > userMaxLength) {
140
143
  return;
141
144
  }
142
145
  if (stringValue === '' || stringValue === '-' || stringValue === decimalSeparator) {
143
- onValueChange && onValueChange(undefined, name, {
146
+ onChange && onChange(undefined, name, {
144
147
  "float": null,
145
148
  formatted: '',
146
149
  value: ''
@@ -152,9 +155,15 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
152
155
  }
153
156
  var stringValueWithoutSeparator = decimalSeparator ? stringValue.replace(decimalSeparator, '.') : stringValue;
154
157
  var numberValue = parseFloat(stringValueWithoutSeparator);
158
+
159
+ // console.log('Number Value', numberValue);
160
+
155
161
  var formattedValue = formatValue(_extends({
156
162
  value: stringValue
157
163
  }, formatValueOptions));
164
+
165
+ // console.log('Formatted Value', numberValue);
166
+
158
167
  if (cursorPosition != null) {
159
168
  // Prevent cursor jumping
160
169
  var newCursor = cursorPosition + (formattedValue.length - value.length);
@@ -162,15 +171,25 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
162
171
  setCursor(newCursor);
163
172
  setChangeCount(changeCount + 1);
164
173
  }
174
+
175
+ // console.log({
176
+ // inputValue: value,
177
+ // modifiedValue,
178
+ // stateValue,
179
+ // selectionStart,
180
+ // lastKeyStroke
181
+ // });
182
+
165
183
  setStateValue(formattedValue);
166
- if (onValueChange) {
184
+ if (onChange) {
167
185
  var values = {
168
186
  "float": numberValue,
169
187
  formatted: formattedValue,
170
188
  value: stringValue
171
189
  };
172
- onValueChange(stringValue, name, values);
190
+ onChange(stringValue, name, values);
173
191
  }
192
+ // onChange && onChange(stringValue);
174
193
  };
175
194
 
176
195
  /**
@@ -181,7 +200,6 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
181
200
  value = _event$target.value,
182
201
  selectionStart = _event$target.selectionStart;
183
202
  processChange(value, selectionStart);
184
- onChange && onChange(event);
185
203
  };
186
204
 
187
205
  /**
@@ -213,8 +231,8 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
213
231
  var formattedValue = formatValue(_extends({}, formatValueOptions, {
214
232
  value: newValue
215
233
  }));
216
- if (onValueChange && formatValueOnBlur) {
217
- onValueChange(newValue, name, {
234
+ if (onChange && formatValueOnBlur) {
235
+ onChange(newValue, name, {
218
236
  "float": numberValue,
219
237
  formatted: formattedValue,
220
238
  value: newValue
@@ -251,6 +269,34 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
251
269
  onKeyDown && onKeyDown(event);
252
270
  };
253
271
 
272
+ // const handleOnKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
273
+ // lastKeyStrokeRef.current = event.key;
274
+
275
+ // // Handle arrow up/down with step
276
+ // if (step && (event.key === 'ArrowUp' || event.key === 'ArrowDown')) {
277
+ // event.preventDefault();
278
+
279
+ // const currentValue =
280
+ // parseFloat(
281
+ // userValue != null
282
+ // ? String(userValue).replace(decimalSeparator, '.')
283
+ // : cleanValue({ value: stateValue, ...cleanValueOptions })
284
+ // ) || 0;
285
+
286
+ // const newValue = event.key === 'ArrowUp' ? currentValue + step : currentValue - step;
287
+
288
+ // if ((min !== undefined && newValue < Number(min)) || (max !== undefined && newValue > Number(max))) return;
289
+
290
+ // const fixedLength = String(step).includes('.') ? String(step).split('.')[1].length : undefined;
291
+
292
+ // processChange(
293
+ // (fixedLength ? newValue.toFixed(fixedLength) : newValue).replace('.', decimalSeparator)
294
+ // );
295
+ // }
296
+
297
+ // onKeyDown && onKeyDown(event);
298
+ // };
299
+
254
300
  /**
255
301
  * Handle key up event
256
302
  *
@@ -292,13 +338,19 @@ export var InputCurrency = /*#__PURE__*/forwardRef(function (_ref, ref) {
292
338
  * If user has only entered "-" or decimal separator,
293
339
  * keep the char to allow them to enter next value
294
340
  */
341
+ // const getRenderValue = () => {
342
+ // if (userValue != null && stateValue !== '-' && (!decimalSeparator || stateValue !== decimalSeparator)) {
343
+ // // console.log('User Value', userValue);
344
+ // return formatValue({
345
+ // ...formatValueOptions,
346
+ // decimalScale: dirty ? undefined : decimalScale,
347
+ // value: String(userValue)
348
+ // });
349
+ // }
350
+
351
+ // return stateValue;
352
+ // };
295
353
  var getRenderValue = function getRenderValue() {
296
- if (userValue != null && stateValue !== '-' && (!decimalSeparator || stateValue !== decimalSeparator)) {
297
- return formatValue(_extends({}, formatValueOptions, {
298
- decimalScale: dirty ? undefined : decimalScale,
299
- value: String(userValue)
300
- }));
301
- }
302
354
  return stateValue;
303
355
  };
304
356
  var inputProps = _extends({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helpdice/ui",
3
- "version": "2.6.1-beta.1",
3
+ "version": "2.6.1-beta.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "esm/index.d.ts",
6
6
  "unpkg": "dist/index.min.js",