@codacy/ui-components 0.61.12 → 0.62.1

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.
@@ -125,7 +125,7 @@ var Input = React.forwardRef(function (_ref2, ref) {
125
125
  onClick: !disabled ? onClick : undefined
126
126
  })), !label && moreInfo && /*#__PURE__*/React.createElement(MoreInfo, {
127
127
  ml: 2,
128
- title: moreInfo,
128
+ helpContent: moreInfo,
129
129
  textAlignment: "center"
130
130
  })), help && !hasError && /*#__PURE__*/React.createElement(InputHelp, {
131
131
  id: "help-".concat(id)
@@ -1,6 +1,14 @@
1
1
  import { InputPropsState, ValidatedInputPropsState, InputType, NumberInputPropsState } from './types';
2
2
  import { TextareaPropsState } from '../Textarea/Textarea.types';
3
3
  import { TagsInputPropsState } from '../TagsInput/types';
4
+ interface Validation {
5
+ callback: (value: string) => boolean;
6
+ errorMessage: string;
7
+ }
8
+ interface AsyncValidation {
9
+ callback: (value: string) => Promise<boolean>;
10
+ errorMessage: string;
11
+ }
4
12
  export declare function useInput(initialValue: string): InputPropsState;
5
13
  export declare function useNumberInput(initialValue: number | null, min?: number, max?: number): NumberInputPropsState;
6
14
  export declare function useRequiredInput(initialValue: string, requiredMessage?: string): InputPropsState;
@@ -10,5 +18,6 @@ export declare function useDebounce<T = string>(value: T | null, delay: number):
10
18
  export declare const useEmailInput: (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
11
19
  export declare const useUrlInput: (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
12
20
  export declare const useRegexInput: (regex: RegExp, errorMessage: string, inputType?: InputType) => (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
13
- export declare const useValidatedInput: (validationCallback: (value: string) => boolean, errorMessage: string, inputType?: InputType, hideSuccess?: boolean) => (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
14
- export declare const useAsyncValidatedInput: (validationCallback: (value: string) => Promise<boolean>, errorMessage: string, inputType?: InputType, hideSuccess?: boolean) => (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
21
+ export declare const useValidatedInput: (validations: Validation[], inputType?: InputType, hideSuccess?: boolean) => (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
22
+ export declare const useAsyncValidatedInput: (validations: AsyncValidation[], inputType?: InputType, hideSuccess?: boolean) => (initialValue: string | null, isRequired: boolean) => ValidatedInputPropsState;
23
+ export {};
@@ -35,8 +35,7 @@ export function useNumberInput(initialValue, min, max) {
35
35
  }, []);
36
36
  var hasValue = value !== null && value !== undefined;
37
37
  var hasMin = min !== undefined;
38
- var hasMax = max !== undefined; //@ts-ignore
39
-
38
+ var hasMax = max !== undefined;
40
39
  var hasError = hasValue && (hasMin && value < min || hasMax && value > max);
41
40
  return {
42
41
  value: value,
@@ -123,7 +122,7 @@ export function useDebounce(value, delay) {
123
122
  return debouncedValue;
124
123
  }
125
124
 
126
- function generateInputValidator(validationCallback, errorMessage, inputType, hideSuccess) {
125
+ function generateInputValidator(validations, inputType, hideSuccess) {
127
126
  return function (initialValue, isRequired) {
128
127
  var didMountRef = useRef(false);
129
128
 
@@ -137,6 +136,11 @@ function generateInputValidator(validationCallback, errorMessage, inputType, hid
137
136
  valid = _useState16[0],
138
137
  setValid = _useState16[1];
139
138
 
139
+ var _useState17 = useState(),
140
+ _useState18 = _slicedToArray(_useState17, 2),
141
+ errorMessage = _useState18[0],
142
+ setErrorMessage = _useState18[1];
143
+
140
144
  var debouncedValue = useDebounce(value, 500);
141
145
  var handleChange = useCallback(function (e) {
142
146
  setValue(e.currentTarget.value || '');
@@ -149,7 +153,12 @@ function generateInputValidator(validationCallback, errorMessage, inputType, hid
149
153
  if (!val) {
150
154
  setValid(isRequired ? false : null);
151
155
  } else {
152
- setValid(validationCallback(val));
156
+ var validationFailed = validations.find(function (_ref) {
157
+ var callback = _ref.callback;
158
+ return !callback(val);
159
+ });
160
+ setErrorMessage(validationFailed === null || validationFailed === void 0 ? void 0 : validationFailed.errorMessage);
161
+ setValid(!validationFailed);
153
162
  }
154
163
  } else {
155
164
  didMountRef.current = true;
@@ -168,7 +177,7 @@ function generateInputValidator(validationCallback, errorMessage, inputType, hid
168
177
  onBlur: isRequired ? doValidation : undefined,
169
178
  hasError: valid !== null && !valid,
170
179
  hasSuccess: !hideSuccess && valid !== null && !!valid,
171
- errorMessage: !valid ? errorMessage : undefined,
180
+ errorMessage: !valid && !!errorMessage ? errorMessage : undefined,
172
181
  type: inputType || 'text',
173
182
  validate: function validate() {
174
183
  return doValidation();
@@ -177,41 +186,42 @@ function generateInputValidator(validationCallback, errorMessage, inputType, hid
177
186
  };
178
187
  }
179
188
 
180
- function generateAsyncInputValidator(validationCallback, errorMessage, inputType, hideSuccess) {
189
+ function generateAsyncInputValidator(validations, inputType, hideSuccess) {
181
190
  return function (initialValue, isRequired) {
182
191
  var didMountRef = useRef(false);
183
192
 
184
- var _useState17 = useState(initialValue),
185
- _useState18 = _slicedToArray(_useState17, 2),
186
- value = _useState18[0],
187
- setValue = _useState18[1];
188
-
189
- var _useState19 = useState(null),
193
+ var _useState19 = useState(initialValue),
190
194
  _useState20 = _slicedToArray(_useState19, 2),
191
- valid = _useState20[0],
192
- setValid = _useState20[1];
195
+ value = _useState20[0],
196
+ setValue = _useState20[1];
193
197
 
194
- var _useState21 = useState(),
198
+ var _useState21 = useState(null),
195
199
  _useState22 = _slicedToArray(_useState21, 2),
196
- isLoading = _useState22[0],
197
- setIsLoading = _useState22[1];
200
+ valid = _useState22[0],
201
+ setValid = _useState22[1];
198
202
 
199
- var _useState23 = useState(null),
203
+ var _useState23 = useState(),
200
204
  _useState24 = _slicedToArray(_useState23, 2),
201
- customErrorMessage = _useState24[0],
202
- setCustomErrorMessage = _useState24[1];
205
+ isLoading = _useState24[0],
206
+ setIsLoading = _useState24[1];
207
+
208
+ var _useState25 = useState(),
209
+ _useState26 = _slicedToArray(_useState25, 2),
210
+ errorMessage = _useState26[0],
211
+ setErrorMessage = _useState26[1];
203
212
 
204
213
  var debouncedValue = useDebounce(value, 500);
205
214
 
206
215
  var doValidation = /*#__PURE__*/function () {
207
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
208
- var val, result;
216
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
217
+ var val, _validationFailed, index, validationFailed, isValid;
218
+
209
219
  return _regeneratorRuntime().wrap(function _callee$(_context) {
210
220
  while (1) {
211
221
  switch (_context.prev = _context.next) {
212
222
  case 0:
213
223
  if (!didMountRef.current) {
214
- _context.next = 24;
224
+ _context.next = 28;
215
225
  break;
216
226
  }
217
227
 
@@ -224,49 +234,62 @@ function generateAsyncInputValidator(validationCallback, errorMessage, inputType
224
234
 
225
235
  setValid(isRequired ? false : null);
226
236
  setIsLoading(false);
227
- _context.next = 22;
237
+ _context.next = 26;
228
238
  break;
229
239
 
230
240
  case 7:
231
241
  setIsLoading(true);
232
242
  _context.prev = 8;
233
- _context.next = 11;
234
- return validationCallback(val);
243
+ index = 0;
235
244
 
236
- case 11:
237
- result = _context.sent;
238
- setValid(result);
239
- _context.next = 19;
240
- break;
245
+ case 10:
246
+ _context.next = 12;
247
+ return validations[index].callback(val);
248
+
249
+ case 12:
250
+ isValid = _context.sent;
251
+ if (isValid) index++;else validationFailed = validations[index];
252
+
253
+ case 14:
254
+ if (index < validations.length && !validationFailed) {
255
+ _context.next = 10;
256
+ break;
257
+ }
241
258
 
242
259
  case 15:
243
- _context.prev = 15;
244
- _context.t0 = _context["catch"](8);
245
- setValid(false);
246
- setCustomErrorMessage(_context.t0.message);
260
+ setValid(!validationFailed);
261
+ setErrorMessage((_validationFailed = validationFailed) === null || _validationFailed === void 0 ? void 0 : _validationFailed.errorMessage);
262
+ _context.next = 23;
263
+ break;
247
264
 
248
265
  case 19:
249
266
  _context.prev = 19;
267
+ _context.t0 = _context["catch"](8);
268
+ setValid(false);
269
+ setErrorMessage(_context.t0.message);
270
+
271
+ case 23:
272
+ _context.prev = 23;
250
273
  setIsLoading(false);
251
- return _context.finish(19);
274
+ return _context.finish(23);
252
275
 
253
- case 22:
254
- _context.next = 25;
276
+ case 26:
277
+ _context.next = 29;
255
278
  break;
256
279
 
257
- case 24:
280
+ case 28:
258
281
  didMountRef.current = true;
259
282
 
260
- case 25:
283
+ case 29:
261
284
  case "end":
262
285
  return _context.stop();
263
286
  }
264
287
  }
265
- }, _callee, null, [[8, 15, 19, 22]]);
288
+ }, _callee, null, [[8, 19, 23, 26]]);
266
289
  }));
267
290
 
268
291
  return function doValidation() {
269
- return _ref.apply(this, arguments);
292
+ return _ref2.apply(this, arguments);
270
293
  };
271
294
  }();
272
295
 
@@ -290,7 +313,7 @@ function generateAsyncInputValidator(validationCallback, errorMessage, inputType
290
313
  onBlur: isRequired ? doValidation : undefined,
291
314
  hasError: valid !== null && !valid,
292
315
  hasSuccess: !hideSuccess && valid !== null && !!valid,
293
- errorMessage: !!customErrorMessage ? customErrorMessage : !valid ? errorMessage : undefined,
316
+ errorMessage: !valid && !!errorMessage ? errorMessage : undefined,
294
317
  type: inputType || 'text',
295
318
  validate: function validate() {
296
319
  return doValidation();
@@ -300,9 +323,12 @@ function generateAsyncInputValidator(validationCallback, errorMessage, inputType
300
323
  }
301
324
 
302
325
  function generateRegexInputValidator(regex, errorMessage, inputType) {
303
- return generateInputValidator(function (value) {
304
- return regex.test(value);
305
- }, errorMessage, inputType);
326
+ return generateInputValidator([{
327
+ callback: function callback(value) {
328
+ return regex.test(value);
329
+ },
330
+ errorMessage: errorMessage
331
+ }], inputType);
306
332
  }
307
333
 
308
334
  export var useEmailInput = generateRegexInputValidator(/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/, 'Insert a valid email address', 'email');
@@ -310,9 +336,9 @@ export var useUrlInput = generateRegexInputValidator(/((([A-Za-z]{3,9}:(?:\/\/)?
310
336
  export var useRegexInput = function useRegexInput(regex, errorMessage, inputType) {
311
337
  return generateRegexInputValidator(regex, errorMessage, inputType);
312
338
  };
313
- export var useValidatedInput = function useValidatedInput(validationCallback, errorMessage, inputType, hideSuccess) {
314
- return generateInputValidator(validationCallback, errorMessage, inputType, hideSuccess);
339
+ export var useValidatedInput = function useValidatedInput(validations, inputType, hideSuccess) {
340
+ return generateInputValidator(validations, inputType, hideSuccess);
315
341
  };
316
- export var useAsyncValidatedInput = function useAsyncValidatedInput(validationCallback, errorMessage, inputType, hideSuccess) {
317
- return generateAsyncInputValidator(validationCallback, errorMessage, inputType, hideSuccess);
342
+ export var useAsyncValidatedInput = function useAsyncValidatedInput(validations, inputType, hideSuccess) {
343
+ return generateAsyncInputValidator(validations, inputType, hideSuccess);
318
344
  };
@@ -3,6 +3,7 @@ import { ControlProps } from '../types';
3
3
  import { Size } from '../theme';
4
4
  import { SpaceProps } from '../system-props/types';
5
5
  import { IconProps, IconFile } from '../Icon/Icon.types';
6
+ import { MoreInfoProps } from '../MoreInfo/types';
6
7
  export declare type InputType = 'text' | 'email' | 'password' | 'number' | 'search' | 'tel' | 'url' | 'date';
7
8
  export interface InputProps extends ControlProps, SpaceProps {
8
9
  /** Display input as a required input field */
@@ -14,7 +15,7 @@ export interface InputProps extends ControlProps, SpaceProps {
14
15
  /** The label of the input */
15
16
  label?: string;
16
17
  /** Display an information icon next to the input */
17
- moreInfo?: string;
18
+ moreInfo?: MoreInfoProps['helpContent'];
18
19
  /** Specifies the maximum number of character for an input field */
19
20
  maxLength?: number;
20
21
  /** Type of input */
@@ -23,7 +23,7 @@ export var Labeled = function Labeled(_ref) {
23
23
  htmlFor: forId
24
24
  }, label, !required && /*#__PURE__*/React.createElement(Optional, null, "(optional)"), moreInfo && /*#__PURE__*/React.createElement(MoreInfo, {
25
25
  ml: 2,
26
- title: moreInfo,
26
+ helpContent: moreInfo,
27
27
  textAlignment: "center"
28
28
  })), children);
29
29
  };
@@ -1,6 +1,7 @@
1
1
  import { ContainerComponentProps } from '../types';
2
2
  import { SpaceProps } from '../system-props/types';
3
3
  import { Size } from '../theme';
4
+ import { MoreInfoProps } from '../MoreInfo/types';
4
5
  export interface LabeledProps extends ContainerComponentProps, SpaceProps {
5
6
  /** Is this control required? */
6
7
  required?: boolean;
@@ -11,5 +12,5 @@ export interface LabeledProps extends ContainerComponentProps, SpaceProps {
11
12
  /** ID of the associated component */
12
13
  forId?: string;
13
14
  /** Display an information icon next to the label */
14
- moreInfo?: string;
15
+ moreInfo?: MoreInfoProps['helpContent'];
15
16
  }
@@ -1,5 +1,5 @@
1
1
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
- var _excluded = ["title", "textAlignment", "placement"];
2
+ var _excluded = ["helpContent", "textAlignment", "placement"];
3
3
  import React from 'react';
4
4
  import { Tooltip } from '../Tooltip';
5
5
  import { Icon } from '..';
@@ -31,13 +31,13 @@ var HelpIcon = function HelpIcon() {
31
31
 
32
32
 
33
33
  var MoreInfo = function MoreInfo(_ref) {
34
- var title = _ref.title,
34
+ var helpContent = _ref.helpContent,
35
35
  textAlignment = _ref.textAlignment,
36
36
  placement = _ref.placement,
37
37
  props = _objectWithoutProperties(_ref, _excluded);
38
38
 
39
39
  return /*#__PURE__*/React.createElement(Tooltip, {
40
- content: title,
40
+ content: helpContent,
41
41
  textAlignment: textAlignment,
42
42
  placement: placement
43
43
  }, /*#__PURE__*/React.createElement(Icon, Object.assign({
@@ -1,6 +1,5 @@
1
- import { TooltipPositionProps } from '../Tooltip/types';
1
+ import { TooltipContentProps, TooltipPositionProps } from '../Tooltip/types';
2
2
  import { IconProps } from '../Icon/Icon.types';
3
3
  export interface MoreInfoProps extends TooltipPositionProps, Omit<IconProps, 'icon'> {
4
- /** The content to display whhen hovering the icon */
5
- title: string;
4
+ helpContent: TooltipContentProps['content'];
6
5
  }
@@ -1,4 +1,5 @@
1
1
  /// <reference types="react" />
2
+ import { MoreInfoProps } from '../MoreInfo/types';
2
3
  import { Size } from '../theme';
3
4
  import { ComponentProps } from '../types';
4
5
  declare type Tags = string[];
@@ -9,7 +10,7 @@ export interface TagsInputProps extends ComponentProps {
9
10
  required?: boolean;
10
11
  label?: string;
11
12
  size?: Size;
12
- moreInfo?: string;
13
+ moreInfo?: MoreInfoProps['helpContent'];
13
14
  }
14
15
  export interface TagsInputPropsState extends TagsInputProps {
15
16
  setTags: (tag: React.SetStateAction<string[]>) => void;
@@ -54,7 +54,7 @@ export var Textarea = function Textarea(_ref) {
54
54
  icon: ErrorIcon
55
55
  })), !label && moreInfo && /*#__PURE__*/React.createElement(MoreInfo, {
56
56
  ml: 2,
57
- title: moreInfo
57
+ helpContent: moreInfo
58
58
  })), help && !hasError && /*#__PURE__*/React.createElement(TextareaHelp, {
59
59
  id: "help-".concat(id)
60
60
  }, help), hasError && /*#__PURE__*/React.createElement(TextareaErrorMessage, {
@@ -2,6 +2,7 @@
2
2
  import { ControlProps } from '../types';
3
3
  import { SpaceProps } from '../system-props/types';
4
4
  import { Size } from '../theme';
5
+ import { MoreInfoProps } from '../MoreInfo/types';
5
6
  export interface TextareaProps extends ControlProps, SpaceProps {
6
7
  /** Display input as a required input field */
7
8
  required?: boolean;
@@ -12,7 +13,7 @@ export interface TextareaProps extends ControlProps, SpaceProps {
12
13
  /** The label of the input */
13
14
  label?: string;
14
15
  /** Display an information icon next to the input */
15
- moreInfo?: string;
16
+ moreInfo?: MoreInfoProps['helpContent'];
16
17
  /** Specifies the maximum number of character for an input field */
17
18
  maxLength?: number;
18
19
  /** Size of input */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codacy/ui-components",
3
- "version": "0.61.12",
3
+ "version": "0.62.1",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",