rhf-stepper 0.2.5 → 1.0.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.
- package/README.md +201 -156
- package/dist/index.d.mts +19 -22
- package/dist/index.d.ts +19 -22
- package/dist/index.js +234 -288
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +234 -289
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,92 +1,10 @@
|
|
|
1
|
-
import { createContext,
|
|
2
|
-
import { Controller as Controller$1 } from 'react-hook-form';
|
|
1
|
+
import { createContext, useState, useRef, useMemo, useCallback, useContext, useEffect } from 'react';
|
|
2
|
+
import { useFormContext, Controller as Controller$1, useController as useController$1 } from 'react-hook-form';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
5
5
|
// src/controller.tsx
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return useContext(StepContext);
|
|
9
|
-
}
|
|
10
|
-
function Step({ children }) {
|
|
11
|
-
const stepContext = useStep();
|
|
12
|
-
const formContext = useInternalFormContext();
|
|
13
|
-
const {
|
|
14
|
-
registerStep: registerStepFromParent,
|
|
15
|
-
step: stepFromParent,
|
|
16
|
-
rebuildSteps: rebuildStepsFromParent,
|
|
17
|
-
changeStepAtIndex: changeStepAtIndexFromParent,
|
|
18
|
-
registrationKey: registrationKeyFromParent
|
|
19
|
-
} = stepContext != null ? stepContext : formContext;
|
|
20
|
-
const isRoot = useMemo(() => {
|
|
21
|
-
if (!stepContext) return true;
|
|
22
|
-
return false;
|
|
23
|
-
}, [stepContext]);
|
|
24
|
-
const stepRef = useRef(void 0);
|
|
25
|
-
const [steps, setSteps] = useState([]);
|
|
26
|
-
const [registrationKey, setRegistrationKey] = useState(0);
|
|
27
|
-
const changeStepAtIndex = useCallback((steps2, index) => {
|
|
28
|
-
setSteps((prevSteps) => {
|
|
29
|
-
const newSteps = [...prevSteps];
|
|
30
|
-
newSteps[index] = steps2;
|
|
31
|
-
return newSteps;
|
|
32
|
-
});
|
|
33
|
-
}, []);
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
if (isRoot && steps.length === 0) return;
|
|
36
|
-
const stepList = steps.length ? steps : [""];
|
|
37
|
-
if (stepRef.current !== void 0) {
|
|
38
|
-
changeStepAtIndexFromParent(stepList, stepRef.current);
|
|
39
|
-
} else {
|
|
40
|
-
registerStepFromParent(stepList, stepRef);
|
|
41
|
-
}
|
|
42
|
-
}, [registrationKeyFromParent, steps, isRoot]);
|
|
43
|
-
useEffect(() => {
|
|
44
|
-
if (stepFromParent !== void 0) {
|
|
45
|
-
rebuildStepsFromParent();
|
|
46
|
-
}
|
|
47
|
-
return () => {
|
|
48
|
-
rebuildStepsFromParent();
|
|
49
|
-
};
|
|
50
|
-
}, []);
|
|
51
|
-
const registerField = useCallback(
|
|
52
|
-
(elements) => {
|
|
53
|
-
setSteps((prevSteps) => {
|
|
54
|
-
return [...prevSteps, ...elements];
|
|
55
|
-
});
|
|
56
|
-
},
|
|
57
|
-
[steps]
|
|
58
|
-
);
|
|
59
|
-
const registerStep = useCallback(
|
|
60
|
-
(elements, stepRef2, step) => {
|
|
61
|
-
setSteps((prevSteps) => {
|
|
62
|
-
const stepNumber = step != null ? step : prevSteps.length;
|
|
63
|
-
stepRef2.current = stepNumber;
|
|
64
|
-
const newSteps = [...prevSteps];
|
|
65
|
-
newSteps.splice(stepNumber, 0, elements);
|
|
66
|
-
return newSteps;
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
[steps]
|
|
70
|
-
);
|
|
71
|
-
const rebuildSteps = useCallback(() => {
|
|
72
|
-
setSteps([]);
|
|
73
|
-
setRegistrationKey((prev) => prev + 1);
|
|
74
|
-
}, []);
|
|
75
|
-
const contextValue = useMemo(
|
|
76
|
-
() => ({
|
|
77
|
-
// eslint-disable-next-line react-hooks/refs
|
|
78
|
-
step: stepRef.current,
|
|
79
|
-
registrationKey,
|
|
80
|
-
changeStepAtIndex,
|
|
81
|
-
registerField,
|
|
82
|
-
registerStep,
|
|
83
|
-
rebuildSteps
|
|
84
|
-
}),
|
|
85
|
-
[registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps]
|
|
86
|
-
);
|
|
87
|
-
return /* @__PURE__ */ jsx(StepContext.Provider, { value: contextValue, children });
|
|
88
|
-
}
|
|
89
|
-
Step.displayName = "Step";
|
|
6
|
+
|
|
7
|
+
// src/helper.ts
|
|
90
8
|
function buildNestedValues(fields, values) {
|
|
91
9
|
const result = {};
|
|
92
10
|
for (let i = 0; i < fields.length; i++) {
|
|
@@ -105,243 +23,182 @@ function buildNestedValues(fields, values) {
|
|
|
105
23
|
}
|
|
106
24
|
return result;
|
|
107
25
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const ai = (_a = a[i]) != null ? _a : -1;
|
|
112
|
-
const bi = (_b = b[i]) != null ? _b : -1;
|
|
113
|
-
if (ai !== bi) return ai - bi;
|
|
114
|
-
}
|
|
115
|
-
return 0;
|
|
116
|
-
}
|
|
117
|
-
function getNodeAtPath(tree, path) {
|
|
118
|
-
let current = tree;
|
|
119
|
-
for (const index of path) {
|
|
120
|
-
if (!Array.isArray(current) || index >= current.length) return void 0;
|
|
121
|
-
current = current[index];
|
|
26
|
+
function generateGUID() {
|
|
27
|
+
function s4() {
|
|
28
|
+
return Math.floor((1 + Math.random()) * 65536).toString(16).substring(1);
|
|
122
29
|
}
|
|
123
|
-
return
|
|
30
|
+
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
|
|
124
31
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const node = getNodeAtPath(tree, path);
|
|
130
|
-
if (node === void 0) return null;
|
|
131
|
-
if (isLeafParent(node)) return path;
|
|
132
|
-
if (Array.isArray(node) && node.length > 0) {
|
|
133
|
-
for (let i = 0; i < node.length; i++) {
|
|
134
|
-
const found = getFirstLeafParentPath(tree, [...path, i]);
|
|
135
|
-
if (found) return found;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
function getLastLeafParentPath(tree, path) {
|
|
141
|
-
const node = getNodeAtPath(tree, path);
|
|
142
|
-
if (node === void 0) return null;
|
|
143
|
-
if (isLeafParent(node)) return path;
|
|
144
|
-
if (Array.isArray(node) && node.length > 0) {
|
|
145
|
-
for (let i = node.length - 1; i >= 0; i--) {
|
|
146
|
-
const found = getLastLeafParentPath(tree, [...path, i]);
|
|
147
|
-
if (found) return found;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
function getPrevLeafParentPath(tree, path) {
|
|
153
|
-
const current = [...path];
|
|
154
|
-
while (current.length > 0) {
|
|
155
|
-
const lastIndex = current[current.length - 1];
|
|
156
|
-
const parentPath = current.slice(0, -1);
|
|
157
|
-
const parent = getNodeAtPath(tree, parentPath);
|
|
158
|
-
if (Array.isArray(parent)) {
|
|
159
|
-
for (let i = lastIndex - 1; i >= 0; i--) {
|
|
160
|
-
const found = getLastLeafParentPath(tree, [...parentPath, i]);
|
|
161
|
-
if (found) return found;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
current.pop();
|
|
165
|
-
}
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
function getNextLeafParentPath(tree, path) {
|
|
169
|
-
const current = [...path];
|
|
170
|
-
while (current.length > 0) {
|
|
171
|
-
const lastIndex = current[current.length - 1];
|
|
172
|
-
const parentPath = current.slice(0, -1);
|
|
173
|
-
const parent = getNodeAtPath(tree, parentPath);
|
|
174
|
-
if (Array.isArray(parent)) {
|
|
175
|
-
for (let i = lastIndex + 1; i < parent.length; i++) {
|
|
176
|
-
const found = getFirstLeafParentPath(tree, [...parentPath, i]);
|
|
177
|
-
if (found) return found;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
current.pop();
|
|
181
|
-
}
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
var FormContext = createContext(null);
|
|
185
|
-
var InternalFormContext = createContext(null);
|
|
186
|
-
function useFormContext() {
|
|
187
|
-
const context = useContext(FormContext);
|
|
188
|
-
if (!context) {
|
|
189
|
-
throw new Error("useFormContext must be used within a <Form>");
|
|
190
|
-
}
|
|
191
|
-
return context;
|
|
192
|
-
}
|
|
193
|
-
function useInternalFormContext() {
|
|
194
|
-
const context = useContext(InternalFormContext);
|
|
195
|
-
if (!context) {
|
|
196
|
-
throw new Error("useInternalFormContext must be used within a <Form>");
|
|
197
|
-
}
|
|
32
|
+
var StepperContext = createContext(null);
|
|
33
|
+
var InternalStepperContext = createContext(null);
|
|
34
|
+
function useInternalStepperContext() {
|
|
35
|
+
const context = useContext(InternalStepperContext);
|
|
198
36
|
return context;
|
|
199
37
|
}
|
|
200
|
-
function
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
38
|
+
function Stepper({
|
|
39
|
+
form: formProps,
|
|
40
|
+
stepValidationMode = "forward",
|
|
41
|
+
children
|
|
42
|
+
}) {
|
|
43
|
+
const formContext = useFormContext();
|
|
44
|
+
const form = formProps != null ? formProps : formContext;
|
|
45
|
+
const [steps, setSteps] = useState({});
|
|
46
|
+
const [stepOrder, setStepOrder] = useState([]);
|
|
47
|
+
const [activeStep, setActiveStep] = useState(-1);
|
|
48
|
+
const [validSteps, setValidSteps] = useState([]);
|
|
204
49
|
const [registrationKey, setRegistrationKey] = useState(0);
|
|
205
50
|
const stepRef = useRef(void 0);
|
|
206
|
-
const
|
|
207
|
-
if (!
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
);
|
|
215
|
-
const currentStepArr = useMemo(() => {
|
|
216
|
-
if (!currentStepNode) return null;
|
|
217
|
-
return isLeafParent(currentStepNode) ? currentStepNode : null;
|
|
218
|
-
}, [currentStepNode]);
|
|
219
|
-
const _setCurrentStep = useCallback(
|
|
220
|
-
async (step, beforeStepChange) => {
|
|
221
|
-
const path = typeof step === "number" ? [0, step] : [0, ...step];
|
|
222
|
-
if (currentStep && currentStepArr && stepValidationMode !== "none") {
|
|
223
|
-
const isForward = comparePaths(path, currentStep) > 0;
|
|
51
|
+
const fields = useMemo(() => {
|
|
52
|
+
if (activeStep === -1 || !steps[stepOrder[activeStep]]) return null;
|
|
53
|
+
return Array.from(new Set(steps[stepOrder[activeStep]]));
|
|
54
|
+
}, [steps, stepOrder, activeStep]);
|
|
55
|
+
const jumpTo = useCallback(
|
|
56
|
+
async (step, onLeave) => {
|
|
57
|
+
if (activeStep !== -1 && fields && stepValidationMode !== "none") {
|
|
58
|
+
const isForward = step > activeStep;
|
|
224
59
|
const shouldValidate = stepValidationMode === "all" || stepValidationMode === "forward" && isForward;
|
|
225
60
|
if (shouldValidate) {
|
|
226
|
-
const isValid = await form.trigger(
|
|
61
|
+
const isValid = await form.trigger(fields);
|
|
227
62
|
if (!isValid) {
|
|
228
|
-
|
|
63
|
+
setValidSteps((prev2) => prev2.filter((s) => s !== activeStep));
|
|
229
64
|
return false;
|
|
230
65
|
}
|
|
231
|
-
|
|
66
|
+
setValidSteps((prev2) => [.../* @__PURE__ */ new Set([...prev2, activeStep])]);
|
|
232
67
|
}
|
|
233
68
|
}
|
|
234
|
-
if (
|
|
235
|
-
const watchedValues = form.watch(
|
|
236
|
-
const values = buildNestedValues(
|
|
237
|
-
|
|
69
|
+
if (onLeave && fields) {
|
|
70
|
+
const watchedValues = form.watch(fields);
|
|
71
|
+
const values = buildNestedValues(
|
|
72
|
+
fields,
|
|
73
|
+
watchedValues
|
|
74
|
+
);
|
|
75
|
+
await onLeave(values);
|
|
238
76
|
}
|
|
239
|
-
|
|
77
|
+
setActiveStep(step);
|
|
240
78
|
return true;
|
|
241
79
|
},
|
|
242
|
-
[
|
|
80
|
+
[activeStep, fields, form, stepValidationMode]
|
|
243
81
|
);
|
|
244
82
|
const isFirstStep = useMemo(
|
|
245
|
-
() =>
|
|
246
|
-
[
|
|
83
|
+
() => activeStep === -1 || activeStep === 0,
|
|
84
|
+
[activeStep]
|
|
247
85
|
);
|
|
248
86
|
const isLastStep = useMemo(
|
|
249
|
-
() =>
|
|
250
|
-
[
|
|
87
|
+
() => activeStep === -1 || activeStep === stepOrder.length - 1,
|
|
88
|
+
[activeStep, stepOrder.length]
|
|
251
89
|
);
|
|
252
|
-
const next = useCallback(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
90
|
+
const next = useCallback(
|
|
91
|
+
async (onLeave) => {
|
|
92
|
+
const callback = typeof onLeave === "function" ? onLeave : void 0;
|
|
93
|
+
if (activeStep === -1 || activeStep >= stepOrder.length - 1) return false;
|
|
94
|
+
if (fields && stepValidationMode !== "none") {
|
|
95
|
+
const isValid = await form.trigger(fields);
|
|
96
|
+
if (!isValid) {
|
|
97
|
+
setValidSteps((prev2) => prev2.filter((s) => s !== activeStep));
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
setValidSteps((prev2) => [.../* @__PURE__ */ new Set([...prev2, activeStep])]);
|
|
260
101
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
await
|
|
102
|
+
if (callback && fields) {
|
|
103
|
+
const watchedValues = form.watch(fields);
|
|
104
|
+
const values = buildNestedValues(
|
|
105
|
+
fields,
|
|
106
|
+
watchedValues
|
|
107
|
+
);
|
|
108
|
+
await callback(values);
|
|
268
109
|
}
|
|
269
|
-
|
|
110
|
+
setActiveStep(activeStep + 1);
|
|
270
111
|
return true;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const prev = useCallback(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
112
|
+
},
|
|
113
|
+
[stepOrder.length, activeStep, fields, form, stepValidationMode]
|
|
114
|
+
);
|
|
115
|
+
const prev = useCallback(
|
|
116
|
+
async (onLeave) => {
|
|
117
|
+
const callback = typeof onLeave === "function" ? onLeave : void 0;
|
|
118
|
+
if (activeStep === -1 || activeStep <= 0) return false;
|
|
119
|
+
if (fields && stepValidationMode === "all") {
|
|
120
|
+
const isValid = await form.trigger(fields);
|
|
121
|
+
if (!isValid) {
|
|
122
|
+
setValidSteps((prev2) => prev2.filter((s) => s !== activeStep));
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
setValidSteps((prev2) => [.../* @__PURE__ */ new Set([...prev2, activeStep])]);
|
|
282
126
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
await
|
|
127
|
+
if (callback && fields) {
|
|
128
|
+
const watchedValues = form.watch(fields);
|
|
129
|
+
const values = buildNestedValues(
|
|
130
|
+
fields,
|
|
131
|
+
watchedValues
|
|
132
|
+
);
|
|
133
|
+
await callback(values);
|
|
290
134
|
}
|
|
291
|
-
|
|
135
|
+
setActiveStep(activeStep - 1);
|
|
292
136
|
return true;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
137
|
+
},
|
|
138
|
+
[activeStep, fields, form, stepValidationMode]
|
|
139
|
+
);
|
|
296
140
|
const registerStep = useCallback(
|
|
297
|
-
(elements,
|
|
141
|
+
(elements, id) => {
|
|
298
142
|
setSteps((prevSteps) => {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
newSteps.
|
|
303
|
-
|
|
304
|
-
const firstLeafParent = getFirstLeafParentPath(newSteps, [0]);
|
|
305
|
-
if (firstLeafParent) {
|
|
306
|
-
setCurrentStep(firstLeafParent);
|
|
307
|
-
}
|
|
143
|
+
stepRef.current = id;
|
|
144
|
+
const newSteps = { ...prevSteps };
|
|
145
|
+
newSteps[id] = elements;
|
|
146
|
+
if (activeStep === -1 && Object.keys(newSteps).length > 0) {
|
|
147
|
+
setActiveStep(0);
|
|
308
148
|
}
|
|
309
149
|
return newSteps;
|
|
310
150
|
});
|
|
311
151
|
},
|
|
312
|
-
[
|
|
152
|
+
[activeStep]
|
|
153
|
+
);
|
|
154
|
+
const registerStepOrder = useCallback(
|
|
155
|
+
(id) => {
|
|
156
|
+
setStepOrder((prevSteps) => {
|
|
157
|
+
return Array.from(/* @__PURE__ */ new Set([...prevSteps, id]));
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
[activeStep]
|
|
161
|
+
);
|
|
162
|
+
useCallback(
|
|
163
|
+
(id) => {
|
|
164
|
+
setSteps((prevSteps) => {
|
|
165
|
+
const newSteps = { ...prevSteps };
|
|
166
|
+
delete newSteps[id];
|
|
167
|
+
return newSteps;
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
[activeStep]
|
|
313
171
|
);
|
|
314
172
|
const rebuildSteps = useCallback(() => {
|
|
315
|
-
|
|
173
|
+
setStepOrder([]);
|
|
316
174
|
setRegistrationKey((prev2) => prev2 + 1);
|
|
317
175
|
}, []);
|
|
318
|
-
const changeStepAtIndex = useCallback(
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
176
|
+
const changeStepAtIndex = useCallback(
|
|
177
|
+
(elements, index) => {
|
|
178
|
+
setSteps((prevSteps) => {
|
|
179
|
+
const newSteps = { ...prevSteps };
|
|
180
|
+
prevSteps[index] = elements;
|
|
181
|
+
return newSteps;
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
[]
|
|
185
|
+
);
|
|
325
186
|
const publicContextValue = useMemo(
|
|
326
187
|
() => ({
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
validatedFields,
|
|
188
|
+
activeStep,
|
|
189
|
+
jumpTo,
|
|
190
|
+
fields,
|
|
191
|
+
validSteps,
|
|
332
192
|
isFirstStep,
|
|
333
193
|
isLastStep,
|
|
334
|
-
form,
|
|
335
194
|
next,
|
|
336
195
|
prev
|
|
337
196
|
}),
|
|
338
197
|
[
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
currentStepArr,
|
|
344
|
-
validatedFields,
|
|
198
|
+
activeStep,
|
|
199
|
+
jumpTo,
|
|
200
|
+
fields,
|
|
201
|
+
validSteps,
|
|
345
202
|
isFirstStep,
|
|
346
203
|
isLastStep,
|
|
347
204
|
next,
|
|
@@ -354,29 +211,100 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
|
|
|
354
211
|
step: stepRef.current,
|
|
355
212
|
registrationKey,
|
|
356
213
|
rebuildSteps,
|
|
214
|
+
registerStepOrder,
|
|
357
215
|
registerStep,
|
|
358
216
|
changeStepAtIndex
|
|
359
217
|
}),
|
|
360
|
-
[
|
|
218
|
+
[
|
|
219
|
+
registrationKey,
|
|
220
|
+
rebuildSteps,
|
|
221
|
+
registerStepOrder,
|
|
222
|
+
registerStep,
|
|
223
|
+
changeStepAtIndex
|
|
224
|
+
]
|
|
361
225
|
);
|
|
362
226
|
const resolvedChildren = (
|
|
363
227
|
// eslint-disable-next-line react-hooks/refs
|
|
364
|
-
typeof children === "function" ? children(
|
|
228
|
+
typeof children === "function" ? children(
|
|
229
|
+
publicContextValue
|
|
230
|
+
) : children
|
|
365
231
|
);
|
|
366
232
|
return (
|
|
367
233
|
// eslint-disable-next-line react-hooks/refs
|
|
368
|
-
/* @__PURE__ */ jsx(
|
|
234
|
+
/* @__PURE__ */ jsx(StepperContext.Provider, { value: publicContextValue, children: /* @__PURE__ */ jsx(InternalStepperContext.Provider, { value: internalContextValue, children: resolvedChildren }) })
|
|
369
235
|
);
|
|
370
236
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
237
|
+
function useStepper() {
|
|
238
|
+
const context = useContext(StepperContext);
|
|
239
|
+
return context;
|
|
240
|
+
}
|
|
241
|
+
var StepContext = createContext(null);
|
|
242
|
+
function useStep() {
|
|
243
|
+
return useContext(StepContext);
|
|
244
|
+
}
|
|
245
|
+
function Step({ children }) {
|
|
246
|
+
const formContext = useInternalStepperContext();
|
|
247
|
+
const {
|
|
248
|
+
registerStep: registerStepFromParent,
|
|
249
|
+
step: stepFromParent,
|
|
250
|
+
rebuildSteps: rebuildStepsFromParent,
|
|
251
|
+
registerStepOrder,
|
|
252
|
+
changeStepAtIndex: changeStepAtIndexFromParent,
|
|
253
|
+
registrationKey: registrationKeyFromParent
|
|
254
|
+
} = formContext;
|
|
255
|
+
const stepRef = useRef(void 0);
|
|
256
|
+
const [id] = useState(generateGUID());
|
|
257
|
+
const [steps, setSteps] = useState([]);
|
|
258
|
+
const [registrationKey, setRegistrationKey] = useState(0);
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
const stepList = steps.length ? steps : [""];
|
|
261
|
+
registerStepFromParent(stepList, id);
|
|
262
|
+
registerStepOrder(id);
|
|
263
|
+
}, [steps]);
|
|
264
|
+
useEffect(() => {
|
|
265
|
+
if (registrationKeyFromParent) {
|
|
266
|
+
registerStepOrder(id);
|
|
267
|
+
}
|
|
268
|
+
}, [registrationKeyFromParent]);
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
if (stepFromParent !== void 0) {
|
|
271
|
+
rebuildStepsFromParent();
|
|
272
|
+
}
|
|
273
|
+
return () => {
|
|
274
|
+
rebuildStepsFromParent();
|
|
275
|
+
};
|
|
276
|
+
}, []);
|
|
277
|
+
const registerField = useCallback(
|
|
278
|
+
(element) => {
|
|
279
|
+
setSteps((prevSteps) => {
|
|
280
|
+
return Array.from(/* @__PURE__ */ new Set([...prevSteps, element]));
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
[steps]
|
|
284
|
+
);
|
|
285
|
+
const rebuildSteps = useCallback(() => {
|
|
286
|
+
stepRef.current = void 0;
|
|
287
|
+
setSteps([]);
|
|
288
|
+
setRegistrationKey((prev) => prev + 1);
|
|
289
|
+
}, []);
|
|
290
|
+
const contextValue = useMemo(
|
|
291
|
+
() => ({
|
|
292
|
+
// eslint-disable-next-line react-hooks/refs
|
|
293
|
+
step: stepRef.current,
|
|
294
|
+
registrationKey,
|
|
295
|
+
registerField,
|
|
296
|
+
rebuildSteps
|
|
297
|
+
}),
|
|
298
|
+
[registrationKey, registerField, rebuildSteps]
|
|
299
|
+
);
|
|
300
|
+
return /* @__PURE__ */ jsx(StepContext.Provider, { value: contextValue, children });
|
|
301
|
+
}
|
|
302
|
+
Step.displayName = "Step";
|
|
303
|
+
function Controller({ name, ...rest }) {
|
|
375
304
|
const stepContext = useStep();
|
|
376
|
-
const resolvedControl = control != null ? control : formContext.form.control;
|
|
377
305
|
useEffect(() => {
|
|
378
306
|
if (stepContext) {
|
|
379
|
-
stepContext.registerField(
|
|
307
|
+
stepContext.registerField(name);
|
|
380
308
|
}
|
|
381
309
|
}, [stepContext == null ? void 0 : stepContext.registrationKey]);
|
|
382
310
|
useEffect(() => {
|
|
@@ -387,10 +315,27 @@ function Controller({ name, control, ...rest }) {
|
|
|
387
315
|
stepContext == null ? void 0 : stepContext.rebuildSteps();
|
|
388
316
|
};
|
|
389
317
|
}, []);
|
|
390
|
-
return /* @__PURE__ */ jsx(Controller$1, { name,
|
|
318
|
+
return /* @__PURE__ */ jsx(Controller$1, { name, ...rest });
|
|
391
319
|
}
|
|
392
320
|
Controller.displayName = "Controller";
|
|
321
|
+
function useController(props) {
|
|
322
|
+
const stepContext = useStep();
|
|
323
|
+
useEffect(() => {
|
|
324
|
+
if (stepContext) {
|
|
325
|
+
stepContext.registerField(props.name);
|
|
326
|
+
}
|
|
327
|
+
}, [stepContext == null ? void 0 : stepContext.registrationKey]);
|
|
328
|
+
useEffect(() => {
|
|
329
|
+
if ((stepContext == null ? void 0 : stepContext.step) !== void 0) {
|
|
330
|
+
stepContext == null ? void 0 : stepContext.rebuildSteps();
|
|
331
|
+
}
|
|
332
|
+
return () => {
|
|
333
|
+
stepContext == null ? void 0 : stepContext.rebuildSteps();
|
|
334
|
+
};
|
|
335
|
+
}, []);
|
|
336
|
+
return useController$1({ ...props });
|
|
337
|
+
}
|
|
393
338
|
|
|
394
|
-
export { Controller,
|
|
339
|
+
export { Controller, Step, Stepper, useController, useStepper };
|
|
395
340
|
//# sourceMappingURL=index.mjs.map
|
|
396
341
|
//# sourceMappingURL=index.mjs.map
|