@codacy/ui-components 0.61.11 → 0.62.0
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.
- package/lib/Dropdown/Dropdown.js +1 -1
- package/lib/Input/hooks.d.ts +11 -2
- package/lib/Input/hooks.js +76 -50
- package/package.json +1 -1
package/lib/Dropdown/Dropdown.js
CHANGED
|
@@ -103,7 +103,7 @@ export var Dropdown = function Dropdown(_ref) {
|
|
|
103
103
|
open: open,
|
|
104
104
|
'aria-haspopup': 'true',
|
|
105
105
|
'aria-expanded': open.toString()
|
|
106
|
-
}), containerElement ? ReactDOM.createPortal(content, containerElement) : content));
|
|
106
|
+
}), open && (containerElement ? ReactDOM.createPortal(content, containerElement) : content)));
|
|
107
107
|
};
|
|
108
108
|
export var useDropdownContext = function useDropdownContext() {
|
|
109
109
|
var dropdownContext = useContext(DropdownContext);
|
package/lib/Input/hooks.d.ts
CHANGED
|
@@ -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: (
|
|
14
|
-
export declare const useAsyncValidatedInput: (
|
|
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 {};
|
package/lib/Input/hooks.js
CHANGED
|
@@ -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;
|
|
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(
|
|
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
|
-
|
|
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(
|
|
189
|
+
function generateAsyncInputValidator(validations, inputType, hideSuccess) {
|
|
181
190
|
return function (initialValue, isRequired) {
|
|
182
191
|
var didMountRef = useRef(false);
|
|
183
192
|
|
|
184
|
-
var
|
|
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
|
-
|
|
192
|
-
|
|
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
|
-
|
|
197
|
-
|
|
200
|
+
valid = _useState22[0],
|
|
201
|
+
setValid = _useState22[1];
|
|
198
202
|
|
|
199
|
-
var _useState23 = useState(
|
|
203
|
+
var _useState23 = useState(),
|
|
200
204
|
_useState24 = _slicedToArray(_useState23, 2),
|
|
201
|
-
|
|
202
|
-
|
|
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
|
|
208
|
-
var val,
|
|
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 =
|
|
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 =
|
|
237
|
+
_context.next = 26;
|
|
228
238
|
break;
|
|
229
239
|
|
|
230
240
|
case 7:
|
|
231
241
|
setIsLoading(true);
|
|
232
242
|
_context.prev = 8;
|
|
233
|
-
|
|
234
|
-
return validationCallback(val);
|
|
243
|
+
index = 0;
|
|
235
244
|
|
|
236
|
-
case
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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(
|
|
274
|
+
return _context.finish(23);
|
|
252
275
|
|
|
253
|
-
case
|
|
254
|
-
_context.next =
|
|
276
|
+
case 26:
|
|
277
|
+
_context.next = 29;
|
|
255
278
|
break;
|
|
256
279
|
|
|
257
|
-
case
|
|
280
|
+
case 28:
|
|
258
281
|
didMountRef.current = true;
|
|
259
282
|
|
|
260
|
-
case
|
|
283
|
+
case 29:
|
|
261
284
|
case "end":
|
|
262
285
|
return _context.stop();
|
|
263
286
|
}
|
|
264
287
|
}
|
|
265
|
-
}, _callee, null, [[8,
|
|
288
|
+
}, _callee, null, [[8, 19, 23, 26]]);
|
|
266
289
|
}));
|
|
267
290
|
|
|
268
291
|
return function doValidation() {
|
|
269
|
-
return
|
|
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:
|
|
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(
|
|
304
|
-
|
|
305
|
-
|
|
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(
|
|
314
|
-
return generateInputValidator(
|
|
339
|
+
export var useValidatedInput = function useValidatedInput(validations, inputType, hideSuccess) {
|
|
340
|
+
return generateInputValidator(validations, inputType, hideSuccess);
|
|
315
341
|
};
|
|
316
|
-
export var useAsyncValidatedInput = function useAsyncValidatedInput(
|
|
317
|
-
return generateAsyncInputValidator(
|
|
342
|
+
export var useAsyncValidatedInput = function useAsyncValidatedInput(validations, inputType, hideSuccess) {
|
|
343
|
+
return generateAsyncInputValidator(validations, inputType, hideSuccess);
|
|
318
344
|
};
|