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