intelicoreact 0.0.82 → 0.0.83

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.
@@ -58,7 +58,9 @@ var Input = function Input(_ref) {
58
58
  error = _ref.error,
59
59
  icon = _ref.icon,
60
60
  symbolsLimit = _ref.symbolsLimit,
61
- blinkTime = _ref.blinkTime;
61
+ blinkTime = _ref.blinkTime,
62
+ _ref$isFocusDefault = _ref.isFocusDefault,
63
+ isFocusDefault = _ref$isFocusDefault === void 0 ? false : _ref$isFocusDefault;
62
64
  var DEFAULT_BLINK_TIME = 100; // STATES
63
65
 
64
66
  var _useState = (0, _react.useState)(false),
@@ -109,19 +111,19 @@ var Input = function Input(_ref) {
109
111
  },
110
112
  focus: function focus(e) {
111
113
  setIsFocused(true);
112
- if (isPriceInput && isOnlyNumber && !isTwoDigitAfterDot) onChange(removeComma(value));
114
+ if (isPriceInput && isOnlyNumber) onChange(removeComma(value));
113
115
  if (onFocus) onFocus(e);
114
116
  },
115
117
  blur: function blur(e) {
116
118
  setIsFocused(false);
117
119
  setEditing(false);
118
120
 
119
- if (isPriceInput && isOnlyNumber && !isTwoDigitAfterDot) {
120
- onChange(addCommas(value));
121
+ if (isTwoDigitAfterDot) {
122
+ onChange(cutOffsingleDot(value));
121
123
  }
122
124
 
123
- if (isTwoDigitAfterDot && !isPriceInput) {
124
- onChange(cutOffsingleDot(value));
125
+ if (isPriceInput && isOnlyNumber) {
126
+ onChange(addCommas(value));
125
127
  }
126
128
 
127
129
  if (onBlur) onBlur(e);
@@ -138,7 +140,6 @@ var Input = function Input(_ref) {
138
140
  }();
139
141
 
140
142
  if (!_index.KEYBOARD_SERVICE_KEYS.includes(e.key) && changedValue === previousValue) setIsAttemptToChange(true);
141
- console.log(changedValue, previousValue);
142
143
  if (_index.KEYBOARD_SERVICE_KEYS.includes(e.key) || !currentSet) previousValueRef.current = value;else previousValueRef.current = previousValue + currentSet[0];
143
144
  }
144
145
 
@@ -147,7 +148,7 @@ var Input = function Input(_ref) {
147
148
  };
148
149
 
149
150
  function cutOffsingleDot(value) {
150
- return value.slice(-1) === '.' ? value.slice(0, -1) : value;
151
+ return value.toString().slice(-1) === '.' ? value.slice(0, -1) : value;
151
152
  }
152
153
 
153
154
  (0, _react.useEffect)(function () {
@@ -192,6 +193,9 @@ var Input = function Input(_ref) {
192
193
  }, blinkTime || DEFAULT_BLINK_TIME);
193
194
  }
194
195
  }, [isAttemptToChange]);
196
+ (0, _react.useEffect)(function () {
197
+ if (inputRef !== null && inputRef !== void 0 && inputRef.current && typeof isFocusDefault === 'boolean') setIsFocused(isFocusDefault);
198
+ }, [inputRef, isFocusDefault]);
195
199
  return /*#__PURE__*/_react.default.createElement("div", {
196
200
  className: (0, _classnames.default)("input__wrap", (0, _defineProperty2.default)({}, "input__wrap_focus", isFocused), (0, _defineProperty2.default)({}, "input__wrap_error", error || isToHighlightError), (0, _defineProperty2.default)({}, "input__wrap_disabled", disabled))
197
201
  }, renderInput(), icon, withDelete && /*#__PURE__*/_react.default.createElement("span", {
@@ -49,6 +49,9 @@ var _default = {
49
49
  isTwoDigitAfterDot: {
50
50
  description: 'boolean - only two digits after dot'
51
51
  },
52
+ isFocusDefault: {
53
+ description: 'boolean - if true, input will be focused on mount'
54
+ },
52
55
  placeholder: {
53
56
  description: 'text'
54
57
  },
@@ -103,6 +106,7 @@ var InputTemplate = Template.bind({});
103
106
  exports.InputTemplate = InputTemplate;
104
107
  InputTemplate.args = {
105
108
  type: 'text',
109
+ isFocusDefault: false,
106
110
  isOnlyNumber: false,
107
111
  isOnlyString: false,
108
112
  isPriceInput: false,
@@ -37,6 +37,9 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
37
37
 
38
38
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
39
39
 
40
+ var timerOutside;
41
+ var timerFocus;
42
+
40
43
  var NumericInput = function NumericInput(_ref) {
41
44
  var onChange = _ref.onChange,
42
45
  disabled = _ref.disabled,
@@ -49,8 +52,6 @@ var NumericInput = function NumericInput(_ref) {
49
52
  value = _ref.value,
50
53
  placeholder = _ref.placeholder,
51
54
  className = _ref.className,
52
- _ref$type = _ref.type,
53
- type = _ref$type === void 0 ? 'number' : _ref$type,
54
55
  onBlur = _ref.onBlur,
55
56
  onFocus = _ref.onFocus,
56
57
  onKeyUp = _ref.onKeyUp,
@@ -62,38 +63,51 @@ var NumericInput = function NumericInput(_ref) {
62
63
  symbolsLimit = _ref.symbolsLimit,
63
64
  isNotBlinkErrors = _ref.isNotBlinkErrors,
64
65
  blinkTime = _ref.blinkTime,
65
- isPriceInput = _ref.isPriceInput;
66
- var DEFAULT_BLINK_TIME = 200; // STATES
67
-
68
- var _useState = (0, _react.useState)(false),
69
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
70
- isFocused = _useState2[0],
71
- setIsFocused = _useState2[1];
72
-
73
- var _useState3 = (0, _react.useState)(false),
74
- _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
75
- isEditing = _useState4[0],
76
- setEditing = _useState4[1];
66
+ isPriceInput = _ref.isPriceInput,
67
+ _ref$isFocusDefault = _ref.isFocusDefault,
68
+ isFocusDefault = _ref$isFocusDefault === void 0 ? false : _ref$isFocusDefault;
69
+ var DEFAULT_BLINK_TIME = 200; //REFS
77
70
 
78
71
  var inputRef = (0, _react.useRef)(null);
79
72
  var decRef = (0, _react.useRef)(null);
80
73
  var incRef = (0, _react.useRef)(null);
81
- var previousValueRef = (0, _react.useRef)(value);
74
+ var wrapRef = (0, _react.useRef)(null);
75
+ var previousValueRef = (0, _react.useRef)(value); // STATES
76
+
77
+ var _useState = (0, _react.useState)(value || min || ''),
78
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
79
+ inputValue = _useState2[0],
80
+ setInputValue = _useState2[1];
81
+
82
+ var _useState3 = (0, _react.useState)(inputValue),
83
+ _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
84
+ inputValueFormated = _useState4[0],
85
+ setInputValueFormated = _useState4[1];
82
86
 
83
- var _useState5 = (0, _react.useState)(false),
87
+ var _useState5 = (0, _react.useState)(0),
84
88
  _useState6 = (0, _slicedToArray2.default)(_useState5, 2),
85
- isAttemptToChange = _useState6[0],
86
- setIsAttemptToChange = _useState6[1];
89
+ intMemoVal = _useState6[0],
90
+ setIntMemoVal = _useState6[1];
87
91
 
88
92
  var _useState7 = (0, _react.useState)(false),
89
93
  _useState8 = (0, _slicedToArray2.default)(_useState7, 2),
90
- isToHighlightError = _useState8[0],
91
- setIsToHighlightError = _useState8[1];
94
+ isFocused = _useState8[0],
95
+ setIsFocused = _useState8[1];
92
96
 
93
- var _useState9 = (0, _react.useState)(0),
97
+ var _useState9 = (0, _react.useState)(false),
94
98
  _useState10 = (0, _slicedToArray2.default)(_useState9, 2),
95
- intMemoVal = _useState10[0],
96
- setIntMemoVal = _useState10[1];
99
+ isEditing = _useState10[0],
100
+ setEditing = _useState10[1];
101
+
102
+ var _useState11 = (0, _react.useState)(false),
103
+ _useState12 = (0, _slicedToArray2.default)(_useState11, 2),
104
+ isAttemptToChange = _useState12[0],
105
+ setIsAttemptToChange = _useState12[1];
106
+
107
+ var _useState13 = (0, _react.useState)(false),
108
+ _useState14 = (0, _slicedToArray2.default)(_useState13, 2),
109
+ isToHighlightError = _useState14[0],
110
+ setIsToHighlightError = _useState14[1];
97
111
 
98
112
  var onlyNumbers = _inputExecutor.formatInput.onlyNumbers;
99
113
  var _formatInput$priceInp = _inputExecutor.formatInput.priceInput,
@@ -104,8 +118,8 @@ var NumericInput = function NumericInput(_ref) {
104
118
  change: function change(e) {
105
119
  var inputValue = e.target ? onlyNumbers(e.target.value) : e;
106
120
 
107
- if (inputValue !== '') {
108
- inputValue = parseFloat(inputValue) || '';
121
+ if (inputValue && (decRef.current.contains(e.target) || incRef.current.contains(e.target))) {
122
+ inputValue = parseFloat(inputValue);
109
123
 
110
124
  if (min && +min > inputValue) {
111
125
  inputValue = min;
@@ -116,37 +130,27 @@ var NumericInput = function NumericInput(_ref) {
116
130
  inputValue = inputValue.toString().substring(0, +symbolsLimit);
117
131
  }
118
132
 
119
- setIntMemoVal(parseFloat(inputValue));
120
- onChange(inputValue.toString());
133
+ setInputValue(inputValue.toString());
121
134
  },
122
- toggleEdit: function toggleEdit() {
123
- setEditing(!isEditing);
124
- onChange('');
135
+ clear: function clear() {
136
+ handle.change(min || '');
125
137
  },
126
138
  focus: function focus(e) {
139
+ if (isFocused) return;
127
140
  setIsFocused(true);
128
- if (isPriceInput) onChange(removeComma(value));
129
141
  if (onFocus) onFocus(e);
130
142
  },
131
143
  blur: function blur(e) {
144
+ if (!isFocused) return;
132
145
  setIsFocused(false);
133
146
  setEditing(false);
134
-
135
- if (isPriceInput) {
136
- if (!isFinite(value)) {
137
- value = intMemoVal;
138
- }
139
-
140
- onChange(addCommas(value));
141
- }
142
-
143
147
  if (onBlur) onBlur(e);
144
148
  },
145
149
  keyUp: function keyUp(e) {
146
150
  if (!isNotBlinkErrors) {
147
- var _value, _previousValueRef$cur;
151
+ var _previousValueRef$cur;
148
152
 
149
- var changedValue = '' + ((_value = value) !== null && _value !== void 0 ? _value : '');
153
+ var changedValue = '' + (value !== null && value !== void 0 ? value : '');
150
154
  var previousValue = '' + ((_previousValueRef$cur = previousValueRef.current) !== null && _previousValueRef$cur !== void 0 ? _previousValueRef$cur : '');
151
155
 
152
156
  var currentSet = function () {
@@ -159,23 +163,26 @@ var NumericInput = function NumericInput(_ref) {
159
163
 
160
164
  if (onKeyUp) onKeyUp(e.keyCode, e.target.value);
161
165
  },
162
- decrement: function decrement() {
166
+ decrement: function decrement(e) {
163
167
  handle.change(intMemoVal - +numStep);
164
168
  },
165
- increment: function increment() {
169
+ increment: function increment(e) {
166
170
  handle.change(intMemoVal + +numStep);
167
171
  }
168
- };
172
+ }; //Check Outside Click
169
173
 
170
- var handleClickOutside = function handleClickOutside(event) {
171
- if (inputRef.current && !inputRef.current.contains(event.target) && !decRef.current.contains(event.target) && !incRef.current.contains(event.target)) {
172
- setTimeout(function () {
173
- inputRef.current.focus();
174
- inputRef.current.blur();
175
- }, 0);
176
- }
177
- };
174
+ (0, _react.useEffect)(function () {
175
+ var handleClickOutside = function handleClickOutside(event) {
176
+ if (!wrapRef.current.contains(event.target)) {
177
+ setIsFocused(false);
178
+ }
179
+ };
178
180
 
181
+ document.addEventListener('mousedown', handleClickOutside, true);
182
+ return function () {
183
+ return document.removeEventListener('mousedown', handleClickOutside, true);
184
+ };
185
+ }, []);
179
186
  (0, _react.useEffect)(function () {
180
187
  if (!isNotBlinkErrors && isAttemptToChange) {
181
188
  setIsAttemptToChange(false);
@@ -184,55 +191,82 @@ var NumericInput = function NumericInput(_ref) {
184
191
  setIsToHighlightError(false);
185
192
  }, blinkTime || DEFAULT_BLINK_TIME);
186
193
  }
187
- }, [isAttemptToChange]);
194
+ }, [isAttemptToChange]); //On Input Value Change
195
+
188
196
  (0, _react.useEffect)(function () {
189
- document.addEventListener('click', handleClickOutside, true);
190
- return function () {
191
- return document.removeEventListener('click', handleClickOutside, true);
192
- };
193
- }, []);
197
+ if (inputValue !== value) setIsFocused(true);
198
+ setInputValueFormated(isPriceInput ? isFocused ? removeComma(inputValue) : addCommas(inputValue) : inputValue);
199
+ setIntMemoVal(parseInt(inputValue));
200
+ if (typeof onChange === 'function') onChange(inputValue === null || inputValue === void 0 ? void 0 : inputValue.toString());
201
+ }, [inputValue]); //On Integer Value Change
202
+
203
+ (0, _react.useEffect)(function () {
204
+ if (isNaN(intMemoVal)) setIntMemoVal(min || 0);
205
+ }, [intMemoVal]); //On Focuse Change
206
+
194
207
  (0, _react.useEffect)(function () {
195
- if (isNaN(intMemoVal)) setIntMemoVal(min || '');
196
- }, [intMemoVal]);
208
+ setInputValueFormated(isPriceInput ? isFocused ? removeComma(inputValue) : addCommas(inputValue) : inputValue);
209
+
210
+ if (isFocused) {
211
+ var _inputRef$current;
212
+
213
+ if (typeof onFocus === 'function') onFocus({
214
+ target: inputRef === null || inputRef === void 0 ? void 0 : inputRef.current
215
+ });
216
+ inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
217
+ } else {
218
+ var _inputRef$current2;
219
+
220
+ if (typeof onBlur === 'function') onBlur({
221
+ target: inputRef === null || inputRef === void 0 ? void 0 : inputRef.current
222
+ });
223
+ inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.blur();
224
+ }
225
+ }, [isFocused]);
197
226
  (0, _react.useEffect)(function () {
198
- if (isEditing || isFocused) inputRef.current.focus();
199
- }, [isEditing, isFocused]);
200
-
201
- var uniProps = _objectSpread(_objectSpread({
202
- className: "input ".concat(className),
203
- placeholder: placeholder,
204
- value: value || '',
205
- disabled: disabled,
206
- onChange: handle.change,
207
- onFocus: handle.focus,
208
- onBlur: handle.blur,
209
- onKeyUp: handle.keyUp,
210
- min: min,
211
- max: max
212
- }, maskChar ? {
213
- maskChar: maskChar
214
- } : {}), formatChars ? {
215
- formatChars: formatChars
216
- } : {});
227
+ if (inputRef !== null && inputRef !== void 0 && inputRef.current && typeof isFocusDefault === 'boolean') setIsFocused(isFocusDefault);
228
+ }, [inputRef, isFocusDefault]);
217
229
 
218
230
  function renderInput() {
231
+ var uniProps = _objectSpread(_objectSpread({
232
+ className: "input ".concat(className || ''),
233
+ placeholder: placeholder,
234
+ value: inputValueFormated,
235
+ disabled: disabled,
236
+ onChange: handle.change,
237
+ onFocus: function onFocus() {
238
+ setIsFocused(true);
239
+ setEditing(true);
240
+ },
241
+ onBlur: function onBlur() {
242
+ return setEditing(false);
243
+ },
244
+ onKeyUp: handle.keyUp,
245
+ min: min,
246
+ max: max
247
+ }, maskChar ? {
248
+ maskChar: maskChar
249
+ } : {}), formatChars ? {
250
+ formatChars: formatChars
251
+ } : {});
252
+
219
253
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("input", (0, _extends2.default)({}, uniProps, {
220
254
  ref: inputRef,
221
- type: type
255
+ type: "text"
222
256
  })), /*#__PURE__*/_react.default.createElement("div", {
223
257
  className: "input__nums"
224
258
  }, /*#__PURE__*/_react.default.createElement("button", {
225
259
  ref: decRef,
226
- onClick: function onClick() {
227
- return handle.decrement();
260
+ onMouseDown: function onMouseDown(e) {
261
+ return handle.decrement(e);
228
262
  },
229
263
  className: (0, _classnames.default)("input__num-btn", {
230
264
  disabled: +value <= min
231
265
  })
232
266
  }, /*#__PURE__*/_react.default.createElement(_reactFeather.Minus, null)), /*#__PURE__*/_react.default.createElement("button", {
233
267
  ref: incRef,
234
- onClick: function onClick() {
235
- return handle.increment();
268
+ onMouseDown: function onMouseDown(e) {
269
+ return handle.increment(e);
236
270
  },
237
271
  className: (0, _classnames.default)("input__num-btn", {
238
272
  disabled: +value >= max
@@ -241,12 +275,15 @@ var NumericInput = function NumericInput(_ref) {
241
275
  }
242
276
 
243
277
  return /*#__PURE__*/_react.default.createElement("div", {
278
+ ref: wrapRef,
244
279
  className: (0, _classnames.default)("input__wrap", (0, _defineProperty2.default)({}, "input__wrap_focus", isFocused), (0, _defineProperty2.default)({}, "input__wrap_error", error || isToHighlightError), (0, _defineProperty2.default)({}, "input__wrap_disabled", disabled))
245
280
  }, renderInput(), icon, withDelete && /*#__PURE__*/_react.default.createElement("span", {
246
281
  className: (0, _classnames.default)("input__close", {
247
- hidden: !value
282
+ hidden: !inputValue
248
283
  }),
249
- onClick: handle.toggleEdit
284
+ onClick: function onClick() {
285
+ return handle.clear();
286
+ }
250
287
  }));
251
288
  };
252
289
 
@@ -31,6 +31,9 @@ var _default = {
31
31
  disabled: {
32
32
  description: 'boolean'
33
33
  },
34
+ isFocusDefault: {
35
+ description: 'boolean - if true, input will be focused on mount'
36
+ },
34
37
  isInitialFocus: {
35
38
  description: 'boolean - if true, the input will be focused on mount'
36
39
  },
@@ -55,13 +58,6 @@ var _default = {
55
58
  placeholder: {
56
59
  description: 'text'
57
60
  },
58
- type: {
59
- description: "'text', 'number', 'password', 'color', 'date', 'datetime-local', 'month', 'time', 'email', 'range'",
60
- control: {
61
- type: 'select',
62
- options: ['text', 'number', 'password', 'color', 'date', 'datetime-local', 'month', 'time', 'email', 'range']
63
- }
64
- },
65
61
  icon: {
66
62
  description: 'JSX'
67
63
  },
@@ -91,7 +87,7 @@ var _default = {
91
87
  exports.default = _default;
92
88
 
93
89
  var Template = function Template(args) {
94
- var _useState = (0, _react.useState)(''),
90
+ var _useState = (0, _react.useState)('15000'),
95
91
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
96
92
  value = _useState2[0],
97
93
  setValue = _useState2[1];
@@ -105,8 +101,8 @@ var Template = function Template(args) {
105
101
  var NumericInputTemplate = Template.bind({});
106
102
  exports.NumericInputTemplate = NumericInputTemplate;
107
103
  NumericInputTemplate.args = {
108
- type: 'text',
109
104
  disabled: false,
105
+ isFocusDefault: false,
110
106
  error: '',
111
107
  isPriceInput: false,
112
108
  mask: '',
@@ -115,7 +111,7 @@ NumericInputTemplate.args = {
115
111
  numStep: 100,
116
112
  min: '0',
117
113
  max: '15000',
118
- symbolsLimit: 2,
114
+ symbolsLimit: 5,
119
115
  placeholder: 'Placeholder',
120
116
  icon: /*#__PURE__*/_react.default.createElement(_reactFeather.User, null)
121
117
  };
@@ -21,7 +21,7 @@ var formatInput = {
21
21
  return isFraction ? intPart + value.slice(value.indexOf('.')) : intPart;
22
22
  },
23
23
  removeComma: function removeComma(value) {
24
- return parseFloat(value.toString().replace(/\,/g, ''));
24
+ return parseInt(value.toString().replace(/\,/g, ''));
25
25
  }
26
26
  },
27
27
  onlyNumbers: function onlyNumbers(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intelicoreact",
3
- "version": "0.0.82",
3
+ "version": "0.0.83",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -28,7 +28,8 @@ const Input = ({
28
28
  error,
29
29
  icon,
30
30
  symbolsLimit,
31
- blinkTime
31
+ blinkTime,
32
+ isFocusDefault = false
32
33
  }) => {
33
34
  const DEFAULT_BLINK_TIME = 100;
34
35
  // STATES
@@ -62,20 +63,19 @@ const Input = ({
62
63
  },
63
64
  focus: (e) => {
64
65
  setIsFocused(true);
65
- if (isPriceInput && isOnlyNumber && !isTwoDigitAfterDot)
66
- onChange(removeComma(value));
66
+ if (isPriceInput && isOnlyNumber) onChange(removeComma(value));
67
67
 
68
68
  if (onFocus) onFocus(e);
69
69
  },
70
70
  blur: (e) => {
71
71
  setIsFocused(false);
72
72
  setEditing(false);
73
- if (isPriceInput && isOnlyNumber && !isTwoDigitAfterDot) {
74
- onChange(addCommas(value));
75
- }
76
- if (isTwoDigitAfterDot && !isPriceInput) {
73
+ if (isTwoDigitAfterDot) {
77
74
  onChange(cutOffsingleDot(value));
78
75
  }
76
+ if (isPriceInput && isOnlyNumber) {
77
+ onChange(addCommas(value));
78
+ }
79
79
  if (onBlur) onBlur(e);
80
80
  },
81
81
  keyUp: (e) => {
@@ -94,7 +94,6 @@ const Input = ({
94
94
  )
95
95
  setIsAttemptToChange(true);
96
96
 
97
- console.log(changedValue, previousValue);
98
97
  if (KEYBOARD_SERVICE_KEYS.includes(e.key) || !currentSet)
99
98
  previousValueRef.current = value;
100
99
  else previousValueRef.current = previousValue + currentSet[0];
@@ -105,7 +104,7 @@ const Input = ({
105
104
  };
106
105
 
107
106
  function cutOffsingleDot(value) {
108
- return value.slice(-1) === '.' ? value.slice(0, -1) : value;
107
+ return value.toString().slice(-1) === '.' ? value.slice(0, -1) : value;
109
108
  }
110
109
 
111
110
  useEffect(() => {
@@ -145,6 +144,11 @@ const Input = ({
145
144
  }
146
145
  }, [isAttemptToChange]);
147
146
 
147
+ useEffect(() => {
148
+ if (inputRef?.current && typeof isFocusDefault === 'boolean')
149
+ setIsFocused(isFocusDefault);
150
+ }, [inputRef, isFocusDefault]);
151
+
148
152
  return (
149
153
  <div
150
154
  className={cn(
@@ -29,6 +29,9 @@ export default {
29
29
  isTwoDigitAfterDot: {
30
30
  description: 'boolean - only two digits after dot'
31
31
  },
32
+ isFocusDefault: {
33
+ description: 'boolean - if true, input will be focused on mount'
34
+ },
32
35
  placeholder: {
33
36
  description: 'text'
34
37
  },
@@ -76,6 +79,7 @@ export const InputTemplate = Template.bind({});
76
79
 
77
80
  InputTemplate.args = {
78
81
  type: 'text',
82
+ isFocusDefault: false,
79
83
  isOnlyNumber: false,
80
84
  isOnlyString: false,
81
85
  isPriceInput: false,
@@ -7,6 +7,9 @@ import { formatInput } from '../../../Functions/inputExecutor';
7
7
 
8
8
  import './NumericInput.scss';
9
9
 
10
+ let timerOutside;
11
+ let timerFocus;
12
+
10
13
  const NumericInput = ({
11
14
  onChange,
12
15
  disabled,
@@ -17,7 +20,6 @@ const NumericInput = ({
17
20
  value,
18
21
  placeholder,
19
22
  className,
20
- type = 'number',
21
23
  onBlur,
22
24
  onFocus,
23
25
  onKeyUp,
@@ -29,21 +31,30 @@ const NumericInput = ({
29
31
  symbolsLimit,
30
32
  isNotBlinkErrors,
31
33
  blinkTime,
32
- isPriceInput
34
+ isPriceInput,
35
+ isFocusDefault = false
33
36
  }) => {
34
37
  const DEFAULT_BLINK_TIME = 200;
35
- // STATES
36
- const [isFocused, setIsFocused] = useState(false);
37
- const [isEditing, setEditing] = useState(false);
38
+
39
+ //REFS
38
40
  const inputRef = useRef(null);
39
41
  const decRef = useRef(null);
40
42
  const incRef = useRef(null);
43
+ const wrapRef = useRef(null);
44
+
41
45
  const previousValueRef = useRef(value);
42
- const [isAttemptToChange, setIsAttemptToChange] = useState(false);
43
- const [isToHighlightError, setIsToHighlightError] = useState(false);
44
46
 
47
+ // STATES
48
+ const [inputValue, setInputValue] = useState(value || min || '');
49
+ const [inputValueFormated, setInputValueFormated] = useState(inputValue);
45
50
  const [intMemoVal, setIntMemoVal] = useState(0);
46
51
 
52
+ const [isFocused, setIsFocused] = useState(false);
53
+ const [isEditing, setEditing] = useState(false);
54
+
55
+ const [isAttemptToChange, setIsAttemptToChange] = useState(false);
56
+ const [isToHighlightError, setIsToHighlightError] = useState(false);
57
+
47
58
  const { onlyNumbers } = formatInput;
48
59
  const { addCommas, removeComma } = formatInput.priceInput;
49
60
 
@@ -51,8 +62,12 @@ const NumericInput = ({
51
62
  const handle = {
52
63
  change: (e) => {
53
64
  let inputValue = e.target ? onlyNumbers(e.target.value) : e;
54
- if (inputValue !== '') {
55
- inputValue = parseFloat(inputValue) || '';
65
+
66
+ if (
67
+ inputValue &&
68
+ (decRef.current.contains(e.target) || incRef.current.contains(e.target))
69
+ ) {
70
+ inputValue = parseFloat(inputValue);
56
71
  if (min && +min > inputValue) {
57
72
  inputValue = min;
58
73
  } else if (max && +max < inputValue) inputValue = max;
@@ -60,28 +75,21 @@ const NumericInput = ({
60
75
  if (symbolsLimit) {
61
76
  inputValue = inputValue.toString().substring(0, +symbolsLimit);
62
77
  }
63
-
64
- setIntMemoVal(parseFloat(inputValue));
65
- onChange(inputValue.toString());
78
+ setInputValue(inputValue.toString());
66
79
  },
67
- toggleEdit: () => {
68
- setEditing(!isEditing);
69
- onChange('');
80
+ clear: () => {
81
+ handle.change(min || '');
70
82
  },
71
83
  focus: (e) => {
84
+ if (isFocused) return;
72
85
  setIsFocused(true);
73
- if (isPriceInput) onChange(removeComma(value));
74
86
  if (onFocus) onFocus(e);
75
87
  },
76
88
  blur: (e) => {
89
+ if (!isFocused) return;
90
+
77
91
  setIsFocused(false);
78
92
  setEditing(false);
79
- if (isPriceInput) {
80
- if (!isFinite(value)) {
81
- value = intMemoVal;
82
- }
83
- onChange(addCommas(value));
84
- }
85
93
 
86
94
  if (onBlur) onBlur(e);
87
95
  },
@@ -110,27 +118,26 @@ const NumericInput = ({
110
118
 
111
119
  if (onKeyUp) onKeyUp(e.keyCode, e.target.value);
112
120
  },
113
- decrement: () => {
121
+ decrement: (e) => {
114
122
  handle.change(intMemoVal - +numStep);
115
123
  },
116
- increment: () => {
124
+ increment: (e) => {
117
125
  handle.change(intMemoVal + +numStep);
118
126
  }
119
127
  };
120
128
 
121
- const handleClickOutside = (event) => {
122
- if (
123
- inputRef.current &&
124
- !inputRef.current.contains(event.target) &&
125
- !decRef.current.contains(event.target) &&
126
- !incRef.current.contains(event.target)
127
- ) {
128
- setTimeout(() => {
129
- inputRef.current.focus();
130
- inputRef.current.blur();
131
- }, 0);
132
- }
133
- };
129
+ //Check Outside Click
130
+ useEffect(() => {
131
+ const handleClickOutside = (event) => {
132
+ if (!wrapRef.current.contains(event.target)) {
133
+ setIsFocused(false);
134
+ }
135
+ };
136
+
137
+ document.addEventListener('mousedown', handleClickOutside, true);
138
+ return () =>
139
+ document.removeEventListener('mousedown', handleClickOutside, true);
140
+ }, []);
134
141
 
135
142
  useEffect(() => {
136
143
  if (!isNotBlinkErrors && isAttemptToChange) {
@@ -142,51 +149,85 @@ const NumericInput = ({
142
149
  }
143
150
  }, [isAttemptToChange]);
144
151
 
152
+ //On Input Value Change
145
153
  useEffect(() => {
146
- document.addEventListener('click', handleClickOutside, true);
147
- return () =>
148
- document.removeEventListener('click', handleClickOutside, true);
149
- }, []);
154
+ if (inputValue !== value) setIsFocused(true);
150
155
 
156
+ setInputValueFormated(
157
+ isPriceInput
158
+ ? isFocused
159
+ ? removeComma(inputValue)
160
+ : addCommas(inputValue)
161
+ : inputValue
162
+ );
163
+ setIntMemoVal(parseInt(inputValue));
164
+
165
+ if (typeof onChange === 'function') onChange(inputValue?.toString());
166
+ }, [inputValue]);
167
+
168
+ //On Integer Value Change
151
169
  useEffect(() => {
152
- if (isNaN(intMemoVal)) setIntMemoVal(min || '');
170
+ if (isNaN(intMemoVal)) setIntMemoVal(min || 0);
153
171
  }, [intMemoVal]);
154
172
 
173
+ //On Focuse Change
155
174
  useEffect(() => {
156
- if (isEditing || isFocused) inputRef.current.focus();
157
- }, [isEditing, isFocused]);
158
-
159
- const uniProps = {
160
- className: `input ${className}`,
161
- placeholder,
162
- value: value || '',
163
- disabled,
164
- onChange: handle.change,
165
- onFocus: handle.focus,
166
- onBlur: handle.blur,
167
- onKeyUp: handle.keyUp,
168
- min,
169
- max,
170
- ...(maskChar ? { maskChar } : {}),
171
- ...(formatChars ? { formatChars } : {})
172
- };
175
+ setInputValueFormated(
176
+ isPriceInput
177
+ ? isFocused
178
+ ? removeComma(inputValue)
179
+ : addCommas(inputValue)
180
+ : inputValue
181
+ );
182
+
183
+ if (isFocused) {
184
+ if (typeof onFocus === 'function') onFocus({ target: inputRef?.current });
185
+ inputRef?.current?.focus();
186
+ } else {
187
+ if (typeof onBlur === 'function') onBlur({ target: inputRef?.current });
188
+ inputRef?.current?.blur();
189
+ }
190
+ }, [isFocused]);
191
+
192
+ useEffect(() => {
193
+ if (inputRef?.current && typeof isFocusDefault === 'boolean')
194
+ setIsFocused(isFocusDefault);
195
+ }, [inputRef, isFocusDefault]);
173
196
 
174
197
  function renderInput() {
198
+ const uniProps = {
199
+ className: `input ${className || ''}`,
200
+ placeholder,
201
+ value: inputValueFormated,
202
+ disabled,
203
+ onChange: handle.change,
204
+ onFocus: () => {
205
+ setIsFocused(true);
206
+ setEditing(true);
207
+ },
208
+ onBlur: () => setEditing(false),
209
+ onKeyUp: handle.keyUp,
210
+ min,
211
+ max,
212
+ ...(maskChar ? { maskChar } : {}),
213
+ ...(formatChars ? { formatChars } : {})
214
+ };
215
+
175
216
  return (
176
217
  <>
177
- <input {...uniProps} ref={inputRef} type={type} />
218
+ <input {...uniProps} ref={inputRef} type='text' />
178
219
 
179
220
  <div className='input__nums'>
180
221
  <button
181
222
  ref={decRef}
182
- onClick={() => handle.decrement()}
223
+ onMouseDown={(e) => handle.decrement(e)}
183
224
  className={cn(`input__num-btn`, { disabled: +value <= min })}
184
225
  >
185
226
  <Minus />
186
227
  </button>
187
228
  <button
188
229
  ref={incRef}
189
- onClick={() => handle.increment()}
230
+ onMouseDown={(e) => handle.increment(e)}
190
231
  className={cn(`input__num-btn`, { disabled: +value >= max })}
191
232
  >
192
233
  <Plus />
@@ -198,6 +239,7 @@ const NumericInput = ({
198
239
 
199
240
  return (
200
241
  <div
242
+ ref={wrapRef}
201
243
  className={cn(
202
244
  `input__wrap`,
203
245
  { [`input__wrap_focus`]: isFocused },
@@ -209,8 +251,8 @@ const NumericInput = ({
209
251
  {icon}
210
252
  {withDelete && (
211
253
  <span
212
- className={cn(`input__close`, { hidden: !value })}
213
- onClick={handle.toggleEdit}
254
+ className={cn(`input__close`, { hidden: !inputValue })}
255
+ onClick={() => handle.clear()}
214
256
  />
215
257
  )}
216
258
  </div>
@@ -11,6 +11,9 @@ export default {
11
11
  disabled: {
12
12
  description: 'boolean'
13
13
  },
14
+ isFocusDefault: {
15
+ description: 'boolean - if true, input will be focused on mount'
16
+ },
14
17
  isInitialFocus: {
15
18
  description: 'boolean - if true, the input will be focused on mount'
16
19
  },
@@ -35,25 +38,6 @@ export default {
35
38
  placeholder: {
36
39
  description: 'text'
37
40
  },
38
- type: {
39
- description:
40
- "'text', 'number', 'password', 'color', 'date', 'datetime-local', 'month', 'time', 'email', 'range'",
41
- control: {
42
- type: 'select',
43
- options: [
44
- 'text',
45
- 'number',
46
- 'password',
47
- 'color',
48
- 'date',
49
- 'datetime-local',
50
- 'month',
51
- 'time',
52
- 'email',
53
- 'range'
54
- ]
55
- }
56
- },
57
41
  icon: { description: 'JSX' },
58
42
  value: { description: '(* - required prop)' },
59
43
  className: { description: 'string' },
@@ -71,15 +55,15 @@ export default {
71
55
  };
72
56
 
73
57
  const Template = (args) => {
74
- const [value, setValue] = useState('');
58
+ const [value, setValue] = useState('15000');
75
59
  return <NumericInput {...args} value={value} onChange={setValue} />;
76
60
  };
77
61
 
78
62
  export const NumericInputTemplate = Template.bind({});
79
63
 
80
64
  NumericInputTemplate.args = {
81
- type: 'text',
82
65
  disabled: false,
66
+ isFocusDefault: false,
83
67
  error: '',
84
68
  isPriceInput: false,
85
69
  mask: '',
@@ -88,7 +72,7 @@ NumericInputTemplate.args = {
88
72
  numStep: 100,
89
73
  min: '0',
90
74
  max: '15000',
91
- symbolsLimit: 2,
75
+ symbolsLimit: 5,
92
76
  placeholder: 'Placeholder',
93
77
  icon: <User />
94
78
  };
@@ -4,38 +4,29 @@ export const formatInput = {
4
4
  value = value.toString();
5
5
  const isFraction = value.includes('.');
6
6
 
7
- const valueBeforeDot = isFraction
8
- ? value.slice(0, value.indexOf('.'))
9
- : value;
7
+ const valueBeforeDot = isFraction ? value.slice(0, value.indexOf('.')) : value;
10
8
 
11
9
  const intPart = valueBeforeDot
12
10
  .split('')
13
11
  .reverse()
14
- .reduce(
15
- (acc, item, idx) =>
16
- idx % 3 === 0 && idx !== 0 ? [...acc, ',', item] : [...acc, item],
17
- []
18
- )
12
+ .reduce((acc, item, idx) => (idx % 3 === 0 && idx !== 0 ? [...acc, ',', item] : [...acc, item]), [])
19
13
  .reverse()
20
14
  .join('');
21
15
 
22
16
  return isFraction ? intPart + value.slice(value.indexOf('.')) : intPart;
23
17
  },
24
18
  removeComma: (value) => {
25
- return parseFloat(value.toString().replace(/\,/g, ''));
26
- }
19
+ return parseInt(value.toString().replace(/\,/g, ''));
20
+ },
27
21
  },
28
22
  onlyNumbers: (value, isDot = false) => {
29
- const val =
30
- value.slice(0, 1) !== '0' && value.slice(0, 1) !== '.'
31
- ? value
32
- : value.slice(1);
23
+ const val = value.slice(0, 1) !== '0' && value.slice(0, 1) !== '.' ? value : value.slice(1);
33
24
  if (isDot) return twoDigitAfterDot(val.replace(/[^0-9.]/g, ''));
34
25
  else return +val.toString().replace(/\D/g, '');
35
26
  },
36
27
  onlyString: (value) => {
37
28
  return value.toString().replace(/[^a-z]/gi, '');
38
- }
29
+ },
39
30
  };
40
31
 
41
32
  //обрезает числа после точки до 2х