@juantroconisf/lib 3.5.7 → 3.6.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/dist/index.d.mts +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +59 -41
- package/dist/index.mjs +60 -42
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -78,6 +78,7 @@ interface FormOptions<O extends StateType> {
|
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
type TouchedType<O extends StateType> = Record<keyof O, boolean>;
|
|
81
|
+
type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
|
|
81
82
|
type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
|
|
82
83
|
type BlurFunc<O extends StateType> = (id: keyof O) => void;
|
|
83
84
|
interface ComponentInputProps<O extends StateType> {
|
|
@@ -126,9 +127,22 @@ interface UseFormChangeResponse<O extends StateType> {
|
|
|
126
127
|
errors: React.RefObject<TouchedType<O>>;
|
|
127
128
|
realErrors: React.RefObject<TouchedType<O>>;
|
|
128
129
|
}
|
|
130
|
+
interface UseFormResponse<O extends StateType> {
|
|
131
|
+
onBlur: BlurFunc<O>;
|
|
132
|
+
onValueChange: ValueChangeFunc<O, keyof O>;
|
|
133
|
+
onSelectionChange: ValueChangeFunc<O, keyof O>;
|
|
134
|
+
state: O;
|
|
135
|
+
setState: React.Dispatch<React.SetStateAction<O>>;
|
|
136
|
+
register: RegisterFunc<O>;
|
|
137
|
+
hasInvalidValues: () => boolean;
|
|
138
|
+
touched: TouchedType<O>;
|
|
139
|
+
resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
|
|
140
|
+
errors: React.RefObject<ErrorsType<O>>;
|
|
141
|
+
}
|
|
142
|
+
type ValidateFunc<O extends StateType> = (value: O[keyof O], types?: ValidatorParams, errorMessages?: ValidatorErrorMessage) => NextUIError;
|
|
129
143
|
|
|
130
144
|
declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
|
|
131
145
|
|
|
132
|
-
declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>):
|
|
146
|
+
declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormResponse<O>;
|
|
133
147
|
|
|
134
|
-
export { type BlurFunc, type ComponentInputProps, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseValidationFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
|
|
148
|
+
export { type BlurFunc, type ComponentInputProps, type ErrorsType, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseFormResponse, type UseValidationFunc, type ValidateFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
|
package/dist/index.d.ts
CHANGED
|
@@ -78,6 +78,7 @@ interface FormOptions<O extends StateType> {
|
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
type TouchedType<O extends StateType> = Record<keyof O, boolean>;
|
|
81
|
+
type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
|
|
81
82
|
type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
|
|
82
83
|
type BlurFunc<O extends StateType> = (id: keyof O) => void;
|
|
83
84
|
interface ComponentInputProps<O extends StateType> {
|
|
@@ -126,9 +127,22 @@ interface UseFormChangeResponse<O extends StateType> {
|
|
|
126
127
|
errors: React.RefObject<TouchedType<O>>;
|
|
127
128
|
realErrors: React.RefObject<TouchedType<O>>;
|
|
128
129
|
}
|
|
130
|
+
interface UseFormResponse<O extends StateType> {
|
|
131
|
+
onBlur: BlurFunc<O>;
|
|
132
|
+
onValueChange: ValueChangeFunc<O, keyof O>;
|
|
133
|
+
onSelectionChange: ValueChangeFunc<O, keyof O>;
|
|
134
|
+
state: O;
|
|
135
|
+
setState: React.Dispatch<React.SetStateAction<O>>;
|
|
136
|
+
register: RegisterFunc<O>;
|
|
137
|
+
hasInvalidValues: () => boolean;
|
|
138
|
+
touched: TouchedType<O>;
|
|
139
|
+
resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
|
|
140
|
+
errors: React.RefObject<ErrorsType<O>>;
|
|
141
|
+
}
|
|
142
|
+
type ValidateFunc<O extends StateType> = (value: O[keyof O], types?: ValidatorParams, errorMessages?: ValidatorErrorMessage) => NextUIError;
|
|
129
143
|
|
|
130
144
|
declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
|
|
131
145
|
|
|
132
|
-
declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>):
|
|
146
|
+
declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormResponse<O>;
|
|
133
147
|
|
|
134
|
-
export { type BlurFunc, type ComponentInputProps, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseValidationFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
|
|
148
|
+
export { type BlurFunc, type ComponentInputProps, type ErrorsType, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseFormResponse, type UseValidationFunc, type ValidateFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
|
package/dist/index.js
CHANGED
|
@@ -290,41 +290,61 @@ function useFormChange({
|
|
|
290
290
|
|
|
291
291
|
// src/hooks/useForm.tsx
|
|
292
292
|
var import_react2 = require("react");
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
293
|
+
|
|
294
|
+
// src/hooks/useValidate.tsx
|
|
295
|
+
var validProps = {
|
|
296
|
+
isInvalid: false,
|
|
297
|
+
errorMessage: ""
|
|
298
|
+
};
|
|
299
|
+
function useValidate() {
|
|
300
|
+
const { lang } = useComponentLanguage();
|
|
301
|
+
const performValidations = (value, validationTypes, errorMessages) => {
|
|
302
|
+
if (!validationTypes) return validProps;
|
|
303
|
+
const items = Object.entries(validationTypes), requiredExist = validationTypes?.required !== void 0, isRequired = requiredExist ? validationTypes.required : false;
|
|
304
|
+
if (value === "" && !isRequired) return validProps;
|
|
305
|
+
let errorFound = validProps;
|
|
306
|
+
for (const [key, opts] of items) {
|
|
307
|
+
const { validate, msg } = errors[key], isInvalid = !validate(value, opts);
|
|
308
|
+
if (isInvalid) {
|
|
309
|
+
errorFound = {
|
|
310
|
+
isInvalid,
|
|
311
|
+
errorMessage: errorMessages?.[key] || lang[msg]
|
|
312
|
+
};
|
|
313
|
+
break;
|
|
307
314
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
315
|
+
}
|
|
316
|
+
return errorFound;
|
|
317
|
+
};
|
|
318
|
+
return { performValidations };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/hooks/useForm.tsx
|
|
322
|
+
function useForm(initialState, { validations, errorMessages } = {}) {
|
|
323
|
+
const initial = {
|
|
324
|
+
touched: allToValue(initialState, false),
|
|
325
|
+
errors: allToValue(initialState, {
|
|
326
|
+
isInvalid: false,
|
|
327
|
+
errorMessage: ""
|
|
328
|
+
})
|
|
329
|
+
};
|
|
330
|
+
const [state, setState] = (0, import_react2.useState)(initialState), touched = (0, import_react2.useRef)(initial.touched), errors2 = (0, import_react2.useRef)(initial.errors), { performValidations } = useValidate();
|
|
331
|
+
const validateInput = (id, value) => {
|
|
332
|
+
errors2.current = handleNestedChange({
|
|
333
|
+
state: errors2.current,
|
|
334
|
+
id,
|
|
335
|
+
value: performValidations(value, validations?.[id], errorMessages?.[id])
|
|
321
336
|
});
|
|
322
|
-
if (requirements === void 0) return;
|
|
323
|
-
ux.current[id] = {
|
|
324
|
-
isInvalid: !uiErrors.isValid,
|
|
325
|
-
errorMessage: uiErrors.validationProps.errorMessage
|
|
326
|
-
};
|
|
327
337
|
};
|
|
338
|
+
const getUXProps = (0, import_react2.useCallback)(
|
|
339
|
+
(id) => {
|
|
340
|
+
const inputError = errors2.current[id], isTouched = touched.current[id];
|
|
341
|
+
return {
|
|
342
|
+
isInvalid: isTouched && inputError.isInvalid,
|
|
343
|
+
errorMessage: isTouched ? inputError.errorMessage : ""
|
|
344
|
+
};
|
|
345
|
+
},
|
|
346
|
+
[errors2.current, touched.current]
|
|
347
|
+
);
|
|
328
348
|
const onBlur = (id) => {
|
|
329
349
|
touched.current = handleNestedChange({
|
|
330
350
|
state: touched.current,
|
|
@@ -354,7 +374,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
354
374
|
setState,
|
|
355
375
|
register: {
|
|
356
376
|
input: (id) => ({
|
|
357
|
-
...
|
|
377
|
+
...getUXProps(id),
|
|
358
378
|
id,
|
|
359
379
|
onBlur: () => onBlur(id),
|
|
360
380
|
onValueChange: (v) => onValueChange(id, v),
|
|
@@ -363,7 +383,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
363
383
|
select: (id) => {
|
|
364
384
|
const isString = typeof state[id] === "string" || state[id] === null;
|
|
365
385
|
return {
|
|
366
|
-
...
|
|
386
|
+
...getUXProps(id),
|
|
367
387
|
id,
|
|
368
388
|
onBlur: () => onBlur(id),
|
|
369
389
|
onSelectionChange: (v) => onSelectionChange(
|
|
@@ -374,7 +394,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
374
394
|
};
|
|
375
395
|
},
|
|
376
396
|
autocomplete: (id) => ({
|
|
377
|
-
...
|
|
397
|
+
...getUXProps(id),
|
|
378
398
|
id,
|
|
379
399
|
onBlur: () => onBlur(id),
|
|
380
400
|
onSelectionChange: (v) => onSelectionChange(id, v),
|
|
@@ -383,13 +403,12 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
383
403
|
},
|
|
384
404
|
hasInvalidValues: () => {
|
|
385
405
|
touched.current = allToValue(initialState, true);
|
|
386
|
-
return JSON.stringify(
|
|
406
|
+
return JSON.stringify(errors2.current).includes(":true");
|
|
387
407
|
},
|
|
388
408
|
touched: touched.current,
|
|
389
409
|
resetForm: (preservedKeys) => {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
errors2.current = val;
|
|
410
|
+
touched.current = initial.touched;
|
|
411
|
+
errors2.current = initial.errors;
|
|
393
412
|
setState(
|
|
394
413
|
preservedKeys === void 0 ? initialState : preservedKeys.reduce(
|
|
395
414
|
(acc, key) => ({
|
|
@@ -400,8 +419,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
400
419
|
)
|
|
401
420
|
);
|
|
402
421
|
},
|
|
403
|
-
errors: errors2
|
|
404
|
-
realErrors
|
|
422
|
+
errors: errors2
|
|
405
423
|
};
|
|
406
424
|
}
|
|
407
425
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -269,42 +269,62 @@ function useFormChange({
|
|
|
269
269
|
}
|
|
270
270
|
|
|
271
271
|
// src/hooks/useForm.tsx
|
|
272
|
-
import { useState as useState2, useRef as useRef2 } from "react";
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
272
|
+
import { useState as useState2, useRef as useRef2, useCallback as useCallback2 } from "react";
|
|
273
|
+
|
|
274
|
+
// src/hooks/useValidate.tsx
|
|
275
|
+
var validProps = {
|
|
276
|
+
isInvalid: false,
|
|
277
|
+
errorMessage: ""
|
|
278
|
+
};
|
|
279
|
+
function useValidate() {
|
|
280
|
+
const { lang } = useComponentLanguage();
|
|
281
|
+
const performValidations = (value, validationTypes, errorMessages) => {
|
|
282
|
+
if (!validationTypes) return validProps;
|
|
283
|
+
const items = Object.entries(validationTypes), requiredExist = validationTypes?.required !== void 0, isRequired = requiredExist ? validationTypes.required : false;
|
|
284
|
+
if (value === "" && !isRequired) return validProps;
|
|
285
|
+
let errorFound = validProps;
|
|
286
|
+
for (const [key, opts] of items) {
|
|
287
|
+
const { validate, msg } = errors[key], isInvalid = !validate(value, opts);
|
|
288
|
+
if (isInvalid) {
|
|
289
|
+
errorFound = {
|
|
290
|
+
isInvalid,
|
|
291
|
+
errorMessage: errorMessages?.[key] || lang[msg]
|
|
292
|
+
};
|
|
293
|
+
break;
|
|
287
294
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
295
|
+
}
|
|
296
|
+
return errorFound;
|
|
297
|
+
};
|
|
298
|
+
return { performValidations };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/hooks/useForm.tsx
|
|
302
|
+
function useForm(initialState, { validations, errorMessages } = {}) {
|
|
303
|
+
const initial = {
|
|
304
|
+
touched: allToValue(initialState, false),
|
|
305
|
+
errors: allToValue(initialState, {
|
|
306
|
+
isInvalid: false,
|
|
307
|
+
errorMessage: ""
|
|
308
|
+
})
|
|
309
|
+
};
|
|
310
|
+
const [state, setState] = useState2(initialState), touched = useRef2(initial.touched), errors2 = useRef2(initial.errors), { performValidations } = useValidate();
|
|
311
|
+
const validateInput = (id, value) => {
|
|
312
|
+
errors2.current = handleNestedChange({
|
|
313
|
+
state: errors2.current,
|
|
314
|
+
id,
|
|
315
|
+
value: performValidations(value, validations?.[id], errorMessages?.[id])
|
|
301
316
|
});
|
|
302
|
-
if (requirements === void 0) return;
|
|
303
|
-
ux.current[id] = {
|
|
304
|
-
isInvalid: !uiErrors.isValid,
|
|
305
|
-
errorMessage: uiErrors.validationProps.errorMessage
|
|
306
|
-
};
|
|
307
317
|
};
|
|
318
|
+
const getUXProps = useCallback2(
|
|
319
|
+
(id) => {
|
|
320
|
+
const inputError = errors2.current[id], isTouched = touched.current[id];
|
|
321
|
+
return {
|
|
322
|
+
isInvalid: isTouched && inputError.isInvalid,
|
|
323
|
+
errorMessage: isTouched ? inputError.errorMessage : ""
|
|
324
|
+
};
|
|
325
|
+
},
|
|
326
|
+
[errors2.current, touched.current]
|
|
327
|
+
);
|
|
308
328
|
const onBlur = (id) => {
|
|
309
329
|
touched.current = handleNestedChange({
|
|
310
330
|
state: touched.current,
|
|
@@ -334,7 +354,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
334
354
|
setState,
|
|
335
355
|
register: {
|
|
336
356
|
input: (id) => ({
|
|
337
|
-
...
|
|
357
|
+
...getUXProps(id),
|
|
338
358
|
id,
|
|
339
359
|
onBlur: () => onBlur(id),
|
|
340
360
|
onValueChange: (v) => onValueChange(id, v),
|
|
@@ -343,7 +363,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
343
363
|
select: (id) => {
|
|
344
364
|
const isString = typeof state[id] === "string" || state[id] === null;
|
|
345
365
|
return {
|
|
346
|
-
...
|
|
366
|
+
...getUXProps(id),
|
|
347
367
|
id,
|
|
348
368
|
onBlur: () => onBlur(id),
|
|
349
369
|
onSelectionChange: (v) => onSelectionChange(
|
|
@@ -354,7 +374,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
354
374
|
};
|
|
355
375
|
},
|
|
356
376
|
autocomplete: (id) => ({
|
|
357
|
-
...
|
|
377
|
+
...getUXProps(id),
|
|
358
378
|
id,
|
|
359
379
|
onBlur: () => onBlur(id),
|
|
360
380
|
onSelectionChange: (v) => onSelectionChange(id, v),
|
|
@@ -363,13 +383,12 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
363
383
|
},
|
|
364
384
|
hasInvalidValues: () => {
|
|
365
385
|
touched.current = allToValue(initialState, true);
|
|
366
|
-
return JSON.stringify(
|
|
386
|
+
return JSON.stringify(errors2.current).includes(":true");
|
|
367
387
|
},
|
|
368
388
|
touched: touched.current,
|
|
369
389
|
resetForm: (preservedKeys) => {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
errors2.current = val;
|
|
390
|
+
touched.current = initial.touched;
|
|
391
|
+
errors2.current = initial.errors;
|
|
373
392
|
setState(
|
|
374
393
|
preservedKeys === void 0 ? initialState : preservedKeys.reduce(
|
|
375
394
|
(acc, key) => ({
|
|
@@ -380,8 +399,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
|
|
|
380
399
|
)
|
|
381
400
|
);
|
|
382
401
|
},
|
|
383
|
-
errors: errors2
|
|
384
|
-
realErrors
|
|
402
|
+
errors: errors2
|
|
385
403
|
};
|
|
386
404
|
}
|
|
387
405
|
export {
|