@m4l/components 9.3.16-JT19092025.beta.1 → 9.3.16
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/components/Stepper/Stepper.styles.js +1 -6
- package/components/Stepper/hooks/useStepperActions/index.js +3 -21
- package/components/Stepper/subcomponents/StepArea/index.js +0 -4
- package/components/Stepper/subcomponents/StepperButtons/StepperCancelButton/index.d.ts +1 -2
- package/components/Stepper/subcomponents/StepperButtons/StepperCancelButton/index.js +6 -39
- package/components/Stepper/subcomponents/StepperButtons/StepperNextButton/index.d.ts +1 -2
- package/components/Stepper/subcomponents/StepperButtons/StepperNextButton/index.js +3 -5
- package/components/Stepper/subcomponents/StepperButtons/StepperPrevButton/index.d.ts +1 -2
- package/components/Stepper/subcomponents/StepperButtons/StepperPrevButton/index.js +3 -5
- package/components/Stepper/subcomponents/StepperButtons/StepperSubmitButton/index.d.ts +1 -2
- package/components/Stepper/subcomponents/StepperButtons/StepperSubmitButton/index.js +3 -5
- package/components/Stepper/subcomponents/StepperFooter/subcomponents/StepperFooterRightActions/index.js +9 -17
- package/components/Stepper/types.d.ts +0 -7
- package/components/hook-form/RHFormContext/index.d.ts +1 -1
- package/components/hook-form/RHFormContext/index.js +4 -29
- package/package.json +1 -1
- package/components/Stepper/hooks/useDynamicValidation/index.d.ts +0 -9
- package/components/Stepper/hooks/useDynamicValidation/index.js +0 -57
|
@@ -24,13 +24,8 @@ const stepperStyles = {
|
|
|
24
24
|
flexDirection: ownerState?.orientation === "vertical" || theme.generalSettings.isMobile ? "column" : "row",
|
|
25
25
|
gap: theme.vars.size.baseSpacings.sp8
|
|
26
26
|
}),
|
|
27
|
-
/**
|
|
28
|
-
* Estilos para el contenido del paso dentro del Stepper.
|
|
29
|
-
*/
|
|
30
27
|
stepContent: ({ ownerState }) => ({
|
|
31
|
-
|
|
32
|
-
display: ownerState?.isStepVisible ? "flex" : "none",
|
|
33
|
-
flexDirection: "column"
|
|
28
|
+
display: ownerState?.isStepVisible ? "block" : "none"
|
|
34
29
|
}),
|
|
35
30
|
/**
|
|
36
31
|
* Estilos para la sección que contiene los botones de acción del Stepper.
|
|
@@ -2,10 +2,8 @@ import { useCallback } from "react";
|
|
|
2
2
|
import { u as useStepper } from "../useStepper/index.js";
|
|
3
3
|
import { useFormContext } from "react-hook-form";
|
|
4
4
|
import { shallow } from "zustand/shallow";
|
|
5
|
-
import { u as useDynamicValidation } from "../useDynamicValidation/index.js";
|
|
6
5
|
function useStepperActions() {
|
|
7
6
|
const { trigger, clearErrors, getValues, reset } = useFormContext();
|
|
8
|
-
const { activateFieldsValidation, clearAllValidation } = useDynamicValidation();
|
|
9
7
|
const {
|
|
10
8
|
nextStep,
|
|
11
9
|
prevStep,
|
|
@@ -43,22 +41,9 @@ function useStepperActions() {
|
|
|
43
41
|
if (fieldsToValidate.length === 0) {
|
|
44
42
|
return true;
|
|
45
43
|
}
|
|
46
|
-
|
|
47
|
-
if (!result) {
|
|
48
|
-
activateFieldsValidation(fieldsToValidate);
|
|
49
|
-
}
|
|
50
|
-
return result;
|
|
44
|
+
return await trigger(fieldsToValidate);
|
|
51
45
|
};
|
|
52
46
|
const success = await nextStep(validateFn, formData);
|
|
53
|
-
if (success) {
|
|
54
|
-
const currentStepData = steps[currentStep - 1];
|
|
55
|
-
const fieldsJustValidated = currentStepData?.validationFields || [];
|
|
56
|
-
if (fieldsJustValidated.length > 0) {
|
|
57
|
-
setTimeout(() => {
|
|
58
|
-
clearAllValidation();
|
|
59
|
-
}, 100);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
47
|
if (success && futureFields.length > 0) {
|
|
63
48
|
clearErrors(futureFields);
|
|
64
49
|
setTimeout(() => {
|
|
@@ -77,16 +62,13 @@ function useStepperActions() {
|
|
|
77
62
|
steps,
|
|
78
63
|
currentStep,
|
|
79
64
|
trigger,
|
|
80
|
-
getValues
|
|
81
|
-
activateFieldsValidation,
|
|
82
|
-
clearAllValidation
|
|
65
|
+
getValues
|
|
83
66
|
]);
|
|
84
67
|
const cancelAction = useCallback(() => {
|
|
85
68
|
reset();
|
|
86
69
|
clearErrors();
|
|
87
|
-
clearAllValidation();
|
|
88
70
|
resetStepper();
|
|
89
|
-
}, [reset, clearErrors,
|
|
71
|
+
}, [reset, clearErrors, resetStepper]);
|
|
90
72
|
return { prevStepAction, nextStepAction, cancelAction };
|
|
91
73
|
}
|
|
92
74
|
export {
|
|
@@ -5,12 +5,10 @@ import { u as useStepper } from "../../hooks/useStepper/index.js";
|
|
|
5
5
|
import { e as StepAreaStyled, f as StepStyled, g as StepNameStyled } from "../../slots/StepperSlot.js";
|
|
6
6
|
import { I as Indicator } from "./subcomponents/Inidicator/index.js";
|
|
7
7
|
import { shallow } from "zustand/shallow";
|
|
8
|
-
import { u as useDynamicValidation } from "../../hooks/useDynamicValidation/index.js";
|
|
9
8
|
import { e as evaluateVisibilityStepCondition } from "../../helpers/evaluateVisibilityStepCondition/index.js";
|
|
10
9
|
function StepArea() {
|
|
11
10
|
const { trigger, clearErrors } = useFormContext();
|
|
12
11
|
const formValues = useWatch();
|
|
13
|
-
const { activateFieldsValidation } = useDynamicValidation();
|
|
14
12
|
const {
|
|
15
13
|
currentStep,
|
|
16
14
|
steps,
|
|
@@ -66,7 +64,6 @@ function StepArea() {
|
|
|
66
64
|
if (!isValid) {
|
|
67
65
|
setCurrentStep(stepOriginalIndex);
|
|
68
66
|
setStepValidationStatus(stepOriginalIndex, false);
|
|
69
|
-
activateFieldsValidation(step.validationFields || []);
|
|
70
67
|
return;
|
|
71
68
|
}
|
|
72
69
|
setStepValidationStatus(stepOriginalIndex, true);
|
|
@@ -79,7 +76,6 @@ function StepArea() {
|
|
|
79
76
|
const isCurrentValid = await trigger(currentStepData.validationFields);
|
|
80
77
|
if (!isCurrentValid) {
|
|
81
78
|
setStepValidationStatus(currentStepOriginalIndex, false);
|
|
82
|
-
activateFieldsValidation(currentStepData.validationFields || []);
|
|
83
79
|
return;
|
|
84
80
|
}
|
|
85
81
|
setStepValidationStatus(currentStepOriginalIndex, true);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { StepperButtonProps } from '../../../types';
|
|
2
1
|
/**
|
|
3
2
|
* Botón modular para cancelar el proceso del Stepper
|
|
4
3
|
*/
|
|
5
|
-
export declare function StepperCancelButton(
|
|
4
|
+
export declare function StepperCancelButton(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,55 +1,22 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useCallback } from "react";
|
|
3
|
-
import { useFormContext, useFormState } from "react-hook-form";
|
|
4
2
|
import { u as useStepperActions } from "../../../hooks/useStepperActions/index.js";
|
|
5
|
-
import { D as DICTIONARY
|
|
6
|
-
import { D as DICTIONARY } from "../../../../CommonActions/dictionary.js";
|
|
3
|
+
import { D as DICTIONARY } from "../../../dictionary.js";
|
|
7
4
|
import { useModuleDictionary } from "@m4l/core";
|
|
8
|
-
import { u as useModal } from "../../../../../hooks/useModal/index.js";
|
|
9
|
-
import { u as useWindowToolsMF } from "../../../../WindowBase/hooks/useWindowToolsMF/index.js";
|
|
10
|
-
import { W as WindowConfirm } from "../../../../WindowConfirm/WindowConfirm.js";
|
|
11
5
|
import { B as Button } from "../../../../mui_extended/Button/Button.js";
|
|
12
|
-
function StepperCancelButton(
|
|
13
|
-
const { label, ...rest } = props;
|
|
6
|
+
function StepperCancelButton() {
|
|
14
7
|
const { getLabel } = useModuleDictionary();
|
|
15
8
|
const { cancelAction } = useStepperActions();
|
|
16
|
-
const
|
|
17
|
-
const { close: closeWindow } = useWindowToolsMF();
|
|
18
|
-
const { control } = useFormContext();
|
|
19
|
-
const { isDirty } = useFormState({
|
|
20
|
-
control
|
|
21
|
-
});
|
|
22
|
-
const onConfirmQuit = useCallback(() => {
|
|
9
|
+
const handleCancel = () => {
|
|
23
10
|
cancelAction();
|
|
24
|
-
|
|
25
|
-
}, [cancelAction, closeWindow]);
|
|
26
|
-
const handleCancel = useCallback(() => {
|
|
27
|
-
if (isDirty) {
|
|
28
|
-
openModal({
|
|
29
|
-
window: /* @__PURE__ */ jsx(
|
|
30
|
-
WindowConfirm,
|
|
31
|
-
{
|
|
32
|
-
variant: "warning",
|
|
33
|
-
title: getLabel(DICTIONARY.CONFIRM_QUIT_TITLE),
|
|
34
|
-
msg: getLabel(DICTIONARY.CONFIRM_QUIT_MSG),
|
|
35
|
-
onClickIntro: onConfirmQuit
|
|
36
|
-
}
|
|
37
|
-
),
|
|
38
|
-
variant: "warning"
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
onConfirmQuit();
|
|
42
|
-
}
|
|
43
|
-
}, [getLabel, isDirty, openModal, onConfirmQuit]);
|
|
11
|
+
};
|
|
44
12
|
return /* @__PURE__ */ jsx(
|
|
45
13
|
Button,
|
|
46
14
|
{
|
|
47
15
|
type: "button",
|
|
48
|
-
label:
|
|
16
|
+
label: getLabel(DICTIONARY.LABEL_CANCEL_BUTTON),
|
|
49
17
|
variant: "outlined",
|
|
50
18
|
color: "default",
|
|
51
|
-
onClick: handleCancel
|
|
52
|
-
...rest
|
|
19
|
+
onClick: handleCancel
|
|
53
20
|
}
|
|
54
21
|
);
|
|
55
22
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { StepperButtonProps } from '../../../types';
|
|
2
1
|
/**
|
|
3
2
|
* Botón modular para avanzar al siguiente step del Stepper
|
|
4
3
|
*/
|
|
5
|
-
export declare function StepperNextButton(
|
|
4
|
+
export declare function StepperNextButton(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -6,8 +6,7 @@ import { D as DICTIONARY } from "../../../dictionary.js";
|
|
|
6
6
|
import { useModuleDictionary, useEnvironment } from "@m4l/core";
|
|
7
7
|
import { I as IconButton } from "../../../../mui_extended/IconButton/IconButton.js";
|
|
8
8
|
import { B as Button } from "../../../../mui_extended/Button/Button.js";
|
|
9
|
-
function StepperNextButton(
|
|
10
|
-
const { label, ...rest } = props;
|
|
9
|
+
function StepperNextButton() {
|
|
11
10
|
const { nextStepAction } = useStepperActions();
|
|
12
11
|
const isMobile = useIsMobile();
|
|
13
12
|
const { getLabel } = useModuleDictionary();
|
|
@@ -29,13 +28,12 @@ function StepperNextButton(props) {
|
|
|
29
28
|
Button,
|
|
30
29
|
{
|
|
31
30
|
type: "button",
|
|
32
|
-
label:
|
|
31
|
+
label: getLabel(DICTIONARY.LABEL_NEXT_BUTTON),
|
|
33
32
|
variant: "contained",
|
|
34
33
|
color: "primary",
|
|
35
34
|
onClick: handleNext,
|
|
36
35
|
endIcon: `${host_static_assets}/${environment_assets}/${pathIcons.arrowRight}`,
|
|
37
|
-
"data-testid": "stepper-next-button"
|
|
38
|
-
...rest
|
|
36
|
+
"data-testid": "stepper-next-button"
|
|
39
37
|
}
|
|
40
38
|
);
|
|
41
39
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { StepperButtonProps } from '../../../types';
|
|
2
1
|
/**
|
|
3
2
|
* Botón modular para ir al step anterior del Stepper
|
|
4
3
|
*/
|
|
5
|
-
export declare function StepperPrevButton(
|
|
4
|
+
export declare function StepperPrevButton(): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -7,8 +7,7 @@ import { useIsMobile } from "@m4l/graphics";
|
|
|
7
7
|
import { D as DICTIONARY } from "../../../dictionary.js";
|
|
8
8
|
import { I as IconButton } from "../../../../mui_extended/IconButton/IconButton.js";
|
|
9
9
|
import { B as Button } from "../../../../mui_extended/Button/Button.js";
|
|
10
|
-
function StepperPrevButton(
|
|
11
|
-
const { label, ...rest } = props;
|
|
10
|
+
function StepperPrevButton() {
|
|
12
11
|
const { currentStep } = useStepper((state) => ({
|
|
13
12
|
currentStep: state.currentStep
|
|
14
13
|
}));
|
|
@@ -36,13 +35,12 @@ function StepperPrevButton(props) {
|
|
|
36
35
|
Button,
|
|
37
36
|
{
|
|
38
37
|
type: "button",
|
|
39
|
-
label:
|
|
38
|
+
label: !isMobile ? getLabel(DICTIONARY.LABEL_PREV_BUTTON) : "",
|
|
40
39
|
variant: "outlined",
|
|
41
40
|
color: "default",
|
|
42
41
|
onClick: handlePrev,
|
|
43
42
|
startIcon: `${host_static_assets}/${environment_assets}/${pathIcons.arrowLeft}`,
|
|
44
|
-
"data-testid": "stepper-prev-button"
|
|
45
|
-
...rest
|
|
43
|
+
"data-testid": "stepper-prev-button"
|
|
46
44
|
}
|
|
47
45
|
);
|
|
48
46
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { StepperButtonProps } from '../../../types';
|
|
2
1
|
/**
|
|
3
2
|
* Botón modular para finalizar el Stepper y ejecutar onFinalSubmit
|
|
4
3
|
*/
|
|
5
|
-
export declare function StepperSubmitButton(
|
|
4
|
+
export declare function StepperSubmitButton(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -2,18 +2,16 @@ import { jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { D as DICTIONARY } from "../../../dictionary.js";
|
|
3
3
|
import { useModuleDictionary } from "@m4l/core";
|
|
4
4
|
import { B as Button } from "../../../../mui_extended/Button/Button.js";
|
|
5
|
-
function StepperSubmitButton(
|
|
6
|
-
const { label, ...rest } = props;
|
|
5
|
+
function StepperSubmitButton() {
|
|
7
6
|
const { getLabel } = useModuleDictionary();
|
|
8
7
|
return /* @__PURE__ */ jsx(
|
|
9
8
|
Button,
|
|
10
9
|
{
|
|
11
10
|
type: "submit",
|
|
12
|
-
label:
|
|
11
|
+
label: getLabel(DICTIONARY.LABEL_SUBMIT_BUTTON),
|
|
13
12
|
variant: "contained",
|
|
14
13
|
color: "primary",
|
|
15
|
-
"data-testid": "stepper-submit-button"
|
|
16
|
-
...rest
|
|
14
|
+
"data-testid": "stepper-submit-button"
|
|
17
15
|
}
|
|
18
16
|
);
|
|
19
17
|
}
|
|
@@ -2,26 +2,18 @@ import { jsx, jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import React, { useMemo } from "react";
|
|
3
3
|
import { u as useIsLastVisibleValidStep } from "../../../../hooks/useIsLastVisibleValidStep/index.js";
|
|
4
4
|
import { m as StepperFooterRightActionsStyled } from "../../../../slots/StepperSlot.js";
|
|
5
|
-
import { S as StepperSubmitButton } from "../../../StepperButtons/StepperSubmitButton/index.js";
|
|
6
5
|
import { S as StepperNextButton } from "../../../StepperButtons/StepperNextButton/index.js";
|
|
6
|
+
import { S as StepperSubmitButton } from "../../../StepperButtons/StepperSubmitButton/index.js";
|
|
7
7
|
function StepperFooterRightActions(props) {
|
|
8
8
|
const { children } = props;
|
|
9
9
|
const isLastVisibleValidStep = useIsLastVisibleValidStep();
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
return false;
|
|
16
|
-
});
|
|
17
|
-
}, [children]);
|
|
18
|
-
const filteredChildren = useMemo(() => {
|
|
10
|
+
const childrenWithoutNextButton = useMemo(() => {
|
|
11
|
+
if (!isLastVisibleValidStep) {
|
|
12
|
+
return children;
|
|
13
|
+
}
|
|
19
14
|
return React.Children.toArray(children).filter((child) => {
|
|
20
15
|
if (React.isValidElement(child)) {
|
|
21
|
-
if (
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
if (isLastVisibleValidStep && child.type === StepperNextButton) {
|
|
16
|
+
if (child.type === StepperNextButton) {
|
|
25
17
|
return false;
|
|
26
18
|
}
|
|
27
19
|
}
|
|
@@ -29,10 +21,10 @@ function StepperFooterRightActions(props) {
|
|
|
29
21
|
});
|
|
30
22
|
}, [children, isLastVisibleValidStep]);
|
|
31
23
|
const submitButton = useMemo(() => {
|
|
32
|
-
return isLastVisibleValidStep
|
|
33
|
-
}, [isLastVisibleValidStep
|
|
24
|
+
return isLastVisibleValidStep ? /* @__PURE__ */ jsx(StepperSubmitButton, {}) : null;
|
|
25
|
+
}, [isLastVisibleValidStep]);
|
|
34
26
|
return /* @__PURE__ */ jsxs(StepperFooterRightActionsStyled, { children: [
|
|
35
|
-
|
|
27
|
+
childrenWithoutNextButton,
|
|
36
28
|
submitButton
|
|
37
29
|
] });
|
|
38
30
|
}
|
|
@@ -5,7 +5,6 @@ import { ContentAreaSlots, ContentSlots, StepperFooterSlots, StepperSlots } from
|
|
|
5
5
|
import { STEPPER_PREFIX_NAME } from './constants';
|
|
6
6
|
import { M4LOverridesStyleRules } from '../../@types/augmentations';
|
|
7
7
|
import { Step as StepComponent } from './subcomponents/StepperContent/subcomponents/Step';
|
|
8
|
-
import { ButtonProps } from '../mui_extended/Button';
|
|
9
8
|
export type Orientation = 'horizontal' | 'vertical';
|
|
10
9
|
export type IndicatorType = 'number' | 'dot';
|
|
11
10
|
export type FormData = Record<string, string | number | boolean | null | undefined>;
|
|
@@ -146,12 +145,6 @@ export interface StepperFooterProps {
|
|
|
146
145
|
export interface StepperFooterLeftActionsProps {
|
|
147
146
|
children?: ReactNode;
|
|
148
147
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Props para los botones del Stepper que extienden las props de Button de mui_extended
|
|
151
|
-
*/
|
|
152
|
-
export interface StepperButtonProps extends Omit<ButtonProps, 'label'> {
|
|
153
|
-
label?: string;
|
|
154
|
-
}
|
|
155
148
|
/**
|
|
156
149
|
* Props del StepperFooterRightActions
|
|
157
150
|
*/
|
|
@@ -3,7 +3,7 @@ import { CustomFormArguments, FormProviderCustomProps, FormProviderProps } from
|
|
|
3
3
|
/**
|
|
4
4
|
* TODO: Documentar
|
|
5
5
|
*/
|
|
6
|
-
export declare function useCustomForm({ validationSchema, values, statusLoad, mode
|
|
6
|
+
export declare function useCustomForm({ validationSchema, values, statusLoad, mode }: CustomFormArguments): import('react-hook-form').UseFormReturn<FieldValues, any, FieldValues>;
|
|
7
7
|
/**
|
|
8
8
|
* TODO: Documentar
|
|
9
9
|
*/
|
|
@@ -6,12 +6,7 @@ import { yupResolver } from "@hookform/resolvers/yup";
|
|
|
6
6
|
import { F as FormProviderRoot } from "./styles.js";
|
|
7
7
|
import { R as RHFormProviderUtilityClasses } from "./classes/index.js";
|
|
8
8
|
const classes = RHFormProviderUtilityClasses();
|
|
9
|
-
function useCustomForm({
|
|
10
|
-
validationSchema,
|
|
11
|
-
values,
|
|
12
|
-
statusLoad,
|
|
13
|
-
mode
|
|
14
|
-
}) {
|
|
9
|
+
function useCustomForm({ validationSchema, values, statusLoad, mode }) {
|
|
15
10
|
const formMethods = useForm({
|
|
16
11
|
resolver: yupResolver(validationSchema),
|
|
17
12
|
defaultValues: values,
|
|
@@ -44,31 +39,11 @@ function useCustomForm({
|
|
|
44
39
|
}
|
|
45
40
|
function FormProviderCustom(props) {
|
|
46
41
|
const { children, onSubmit, className, handleSubmit } = props;
|
|
47
|
-
return /* @__PURE__ */ jsx(FormProvider, { ...props, children: /* @__PURE__ */ jsx(
|
|
48
|
-
FormProviderRoot,
|
|
49
|
-
{
|
|
50
|
-
className: clsx(classes.root, className),
|
|
51
|
-
onSubmit: handleSubmit(onSubmit),
|
|
52
|
-
children
|
|
53
|
-
}
|
|
54
|
-
) });
|
|
42
|
+
return /* @__PURE__ */ jsx(FormProvider, { ...props, children: /* @__PURE__ */ jsx(FormProviderRoot, { className: clsx(classes.root, className), onSubmit: handleSubmit(onSubmit), children }) });
|
|
55
43
|
}
|
|
56
44
|
function RHFormProvider(props) {
|
|
57
|
-
const {
|
|
58
|
-
|
|
59
|
-
onSubmit,
|
|
60
|
-
values,
|
|
61
|
-
validationSchema,
|
|
62
|
-
statusLoad = "ready",
|
|
63
|
-
className,
|
|
64
|
-
mode
|
|
65
|
-
} = props;
|
|
66
|
-
const formMethods = useCustomForm({
|
|
67
|
-
validationSchema,
|
|
68
|
-
statusLoad,
|
|
69
|
-
values,
|
|
70
|
-
mode
|
|
71
|
-
});
|
|
45
|
+
const { children, onSubmit, values, validationSchema, statusLoad = "ready", className, mode } = props;
|
|
46
|
+
const formMethods = useCustomForm({ validationSchema, statusLoad, values, mode });
|
|
72
47
|
return /* @__PURE__ */ jsx(
|
|
73
48
|
FormProviderCustom,
|
|
74
49
|
{
|
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hook que simula validación onChange de campos específicos del Stepper, manteniendo el formulario en modo onSubmit para preservar el rendimiento.
|
|
3
|
-
* @returns Objeto con funciones para manejar la validación dinámica
|
|
4
|
-
*/
|
|
5
|
-
export declare function useDynamicValidation(): {
|
|
6
|
-
activateFieldsValidation: (fields: string[]) => void;
|
|
7
|
-
clearAllValidation: () => void;
|
|
8
|
-
activeFields: string[];
|
|
9
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { useState, useRef, useEffect, useCallback } from "react";
|
|
2
|
-
import { useFormContext, useWatch } from "react-hook-form";
|
|
3
|
-
function useDynamicValidation() {
|
|
4
|
-
const { trigger } = useFormContext();
|
|
5
|
-
const [activeFields, setActiveFields] = useState([]);
|
|
6
|
-
const watchValues = useWatch({
|
|
7
|
-
name: activeFields.length > 0 ? activeFields : ["__dummy__"]
|
|
8
|
-
});
|
|
9
|
-
const lastValuesRef = useRef({});
|
|
10
|
-
useEffect(() => {
|
|
11
|
-
if (activeFields.length === 0) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
const currentValues = activeFields.length === 1 ? { [activeFields[0]]: watchValues } : activeFields.reduce((acc, field, index) => {
|
|
15
|
-
acc[field] = Array.isArray(watchValues) ? watchValues[index] : watchValues;
|
|
16
|
-
return acc;
|
|
17
|
-
}, {});
|
|
18
|
-
const changedFields = activeFields.filter((field) => {
|
|
19
|
-
const currentValue = currentValues[field];
|
|
20
|
-
const lastValue = lastValuesRef.current[field];
|
|
21
|
-
const hasChanged = JSON.stringify(currentValue) !== JSON.stringify(lastValue);
|
|
22
|
-
if (hasChanged) {
|
|
23
|
-
lastValuesRef.current[field] = currentValue;
|
|
24
|
-
}
|
|
25
|
-
return hasChanged;
|
|
26
|
-
});
|
|
27
|
-
if (changedFields.length > 0) {
|
|
28
|
-
const timeoutId = setTimeout(() => {
|
|
29
|
-
trigger(changedFields);
|
|
30
|
-
}, 100);
|
|
31
|
-
return () => clearTimeout(timeoutId);
|
|
32
|
-
}
|
|
33
|
-
}, [activeFields, trigger, watchValues]);
|
|
34
|
-
const activateFieldsValidation = useCallback((fields) => {
|
|
35
|
-
setActiveFields((prev) => {
|
|
36
|
-
const newFields = fields.filter((field) => !prev.includes(field));
|
|
37
|
-
return [...prev, ...newFields];
|
|
38
|
-
});
|
|
39
|
-
fields.forEach((field) => {
|
|
40
|
-
if (!lastValuesRef.current.hasOwnProperty(field)) {
|
|
41
|
-
lastValuesRef.current[field] = void 0;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}, []);
|
|
45
|
-
const clearAllValidation = useCallback(() => {
|
|
46
|
-
setActiveFields([]);
|
|
47
|
-
lastValuesRef.current = {};
|
|
48
|
-
}, []);
|
|
49
|
-
return {
|
|
50
|
-
activateFieldsValidation,
|
|
51
|
-
clearAllValidation,
|
|
52
|
-
activeFields
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export {
|
|
56
|
-
useDynamicValidation as u
|
|
57
|
-
};
|