rhf-stepper 0.1.0 → 0.1.2

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 ADDED
@@ -0,0 +1,369 @@
1
+ # rhf-stepper
2
+
3
+ A lightweight multi-step form helper for [react-hook-form](https://react-hook-form.com). Build wizards, multi-step forms, and stepped workflows with automatic per-step validation.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install rhf-stepper
9
+ # or
10
+ pnpm add rhf-stepper
11
+ # or
12
+ yarn add rhf-stepper
13
+ ```
14
+
15
+ ### Peer Dependencies
16
+
17
+ ```json
18
+ {
19
+ "react": ">=17.0.0",
20
+ "react-hook-form": ">=7.0.0"
21
+ }
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ > **Important:** All `<Step>` and `<Controller>` components must always be mounted in the tree. They register fields on mount, so conditionally rendering them (`{currentStep === 0 && <Step>...}`) will break registration. Control visibility with `hidden` or CSS instead.
27
+
28
+ ```tsx
29
+ import { useForm } from 'react-hook-form'
30
+ import { Form, Step, Controller, useFormContext } from 'rhf-stepper'
31
+
32
+ type FormValues = {
33
+ name: string
34
+ email: string
35
+ address: string
36
+ city: string
37
+ }
38
+
39
+ function MyMultiStepForm() {
40
+ const form = useForm<FormValues>()
41
+
42
+ return (
43
+ <Form form={form} onSubmit={(data) => console.log(data)}>
44
+ {({ currentStep }) => (
45
+ <>
46
+ <div hidden={currentStep !== 0}>
47
+ <Step>
48
+ <Controller name="name" render={({ field }) => <input {...field} placeholder="Name" />} />
49
+ <Controller name="email" render={({ field }) => <input {...field} placeholder="Email" />} />
50
+ </Step>
51
+ </div>
52
+
53
+ <div hidden={currentStep !== 1}>
54
+ <Step>
55
+ <Controller name="address" render={({ field }) => <input {...field} placeholder="Address" />} />
56
+ <Controller name="city" render={({ field }) => <input {...field} placeholder="City" />} />
57
+ </Step>
58
+ </div>
59
+
60
+ <StepNavigation />
61
+ </>
62
+ )}
63
+ </Form>
64
+ )
65
+ }
66
+
67
+ function StepNavigation() {
68
+ const { next, prev, isFirstStep, isLastStep } = useFormContext()
69
+
70
+ return (
71
+ <div>
72
+ {!isFirstStep && <button type="button" onClick={prev}>Back</button>}
73
+ {!isLastStep && <button type="button" onClick={next}>Next</button>}
74
+ {isLastStep && <button type="submit">Submit</button>}
75
+ </div>
76
+ )
77
+ }
78
+ ```
79
+
80
+ ## API Reference
81
+
82
+ ### `<Form>`
83
+
84
+ The root component that replaces the standard `<form>` element. Manages step state and validation.
85
+
86
+ ```tsx
87
+ <Form
88
+ form={form}
89
+ onSubmit={handleSubmit}
90
+ stepValidationMode="forward"
91
+ >
92
+ {children}
93
+ </Form>
94
+ ```
95
+
96
+ #### Props
97
+
98
+ | Prop | Type | Default | Description |
99
+ |------|------|---------|-------------|
100
+ | `form` | `UseFormReturn<T>` | *required* | The form instance from `useForm()` |
101
+ | `onSubmit` | `(values: T) => void` | *required* | Called with form values on valid submission |
102
+ | `stepValidationMode` | `'forward' \| 'all' \| 'none'` | `'forward'` | When to validate step fields (see below) |
103
+ | `children` | `ReactNode \| (context) => ReactNode` | *required* | Form content. Accepts a render function for context access |
104
+ | `ref` | `Ref<HTMLFormElement>` | - | Ref forwarded to the underlying `<form>` element |
105
+
106
+ All other props are passed through to the underlying `<form>` element (e.g., `className`, `id`).
107
+
108
+ #### Step Validation Modes
109
+
110
+ | Mode | Description |
111
+ |------|-------------|
112
+ | `'forward'` | Validates current step fields only when navigating forward (via `next()` or `setCurrentStep` to a later step) |
113
+ | `'all'` | Validates current step fields on every navigation (forward and backward) |
114
+ | `'none'` | No automatic validation. Navigate freely between steps |
115
+
116
+ #### Render Function
117
+
118
+ When using a render function as children, you receive the full `FormContextValue`:
119
+
120
+ ```tsx
121
+ <Form form={form} onSubmit={handleSubmit}>
122
+ {({ currentStep, isFirstStep, isLastStep, next, prev }) => (
123
+ // render steps based on currentStep
124
+ )}
125
+ </Form>
126
+ ```
127
+
128
+ ---
129
+
130
+ ### `<Step>`
131
+
132
+ Groups `<Controller>` fields into a logical step. Fields inside a `<Step>` are automatically registered and validated together.
133
+
134
+ > **Important:** `<Step>` components must always be mounted (rendered) in the tree. Use `hidden` or CSS to control visibility -- do not conditionally render them.
135
+
136
+ ```tsx
137
+ <div hidden={currentStep !== 0}>
138
+ <Step>
139
+ <Controller name="firstName" render={({ field }) => <input {...field} />} />
140
+ <Controller name="lastName" render={({ field }) => <input {...field} />} />
141
+ </Step>
142
+ </div>
143
+ ```
144
+
145
+ Steps can be nested for sub-step grouping:
146
+
147
+ ```tsx
148
+ <Step>
149
+ <div hidden={currentStep !== 0}>
150
+ <Step>
151
+ <Controller name="a" render={({ field }) => <input {...field} />} />
152
+ </Step>
153
+ </div>
154
+ <div hidden={currentStep !== 1}>
155
+ <Step>
156
+ <Controller name="b" render={({ field }) => <input {...field} />} />
157
+ </Step>
158
+ </div>
159
+ </Step>
160
+ ```
161
+
162
+ ---
163
+
164
+ ### `<Controller>`
165
+
166
+ A drop-in replacement for react-hook-form's `Controller`. It automatically registers the field with the nearest `<Step>` for step-aware validation.
167
+
168
+ ```tsx
169
+ <Controller
170
+ name="email"
171
+ rules={{ required: 'Email is required' }}
172
+ render={({ field, fieldState }) => (
173
+ <div>
174
+ <input {...field} />
175
+ {fieldState.error && <span>{fieldState.error.message}</span>}
176
+ </div>
177
+ )}
178
+ />
179
+ ```
180
+
181
+ #### Props
182
+
183
+ Same as [react-hook-form's Controller](https://react-hook-form.com/docs/usecontroller/controller). The `control` prop is optional -- it's automatically resolved from the `<Form>` context.
184
+
185
+ ---
186
+
187
+ ### `useFormContext()`
188
+
189
+ Hook to access the stepper state from any component inside `<Form>`.
190
+
191
+ ```tsx
192
+ const {
193
+ form,
194
+ currentStep,
195
+ setCurrentStep,
196
+ currentStepNode,
197
+ currentStepArr,
198
+ validatedFields,
199
+ isFirstStep,
200
+ isLastStep,
201
+ next,
202
+ prev,
203
+ } = useFormContext<MyFormValues>()
204
+ ```
205
+
206
+ #### Return Value (`FormContextValue<T>`)
207
+
208
+ | Property | Type | Description |
209
+ |----------|------|-------------|
210
+ | `form` | `UseFormReturn<T>` | The react-hook-form instance |
211
+ | `currentStep` | `number \| number[] \| null` | Current step index. `number[]` for nested steps |
212
+ | `setCurrentStep` | `(step: number \| number[]) => Promise<void>` | Navigate to a specific step. Validates before navigating (based on `stepValidationMode`) |
213
+ | `currentStepNode` | `StepTree \| undefined` | The current step's node in the step tree |
214
+ | `currentStepArr` | `string[] \| null` | Field names registered in the current step |
215
+ | `validatedFields` | `string[]` | Field names that have been validated via step navigation |
216
+ | `isFirstStep` | `boolean` | `true` if on the first step |
217
+ | `isLastStep` | `boolean` | `true` if on the last step |
218
+ | `next` | `() => void` | Navigate to the next step (validates current step first) |
219
+ | `prev` | `() => void` | Navigate to the previous step |
220
+
221
+ ---
222
+
223
+ ## Examples
224
+
225
+ ### Basic Two-Step Form
226
+
227
+ ```tsx
228
+ import { useForm } from 'react-hook-form'
229
+ import { Form, Step, Controller, useFormContext } from 'rhf-stepper'
230
+
231
+ type SignupForm = {
232
+ email: string
233
+ password: string
234
+ firstName: string
235
+ lastName: string
236
+ }
237
+
238
+ function SignupWizard() {
239
+ const form = useForm<SignupForm>()
240
+
241
+ return (
242
+ <Form form={form} onSubmit={(data) => console.log(data)}>
243
+ {({ currentStep }) => (
244
+ <>
245
+ <div hidden={currentStep !== 0}>
246
+ <Step>
247
+ <Controller
248
+ name="email"
249
+ rules={{ required: 'Email is required' }}
250
+ render={({ field, fieldState }) => (
251
+ <div>
252
+ <input {...field} type="email" placeholder="Email" />
253
+ {fieldState.error && <p>{fieldState.error.message}</p>}
254
+ </div>
255
+ )}
256
+ />
257
+ <Controller
258
+ name="password"
259
+ rules={{ required: 'Password is required', minLength: { value: 8, message: 'Min 8 characters' } }}
260
+ render={({ field, fieldState }) => (
261
+ <div>
262
+ <input {...field} type="password" placeholder="Password" />
263
+ {fieldState.error && <p>{fieldState.error.message}</p>}
264
+ </div>
265
+ )}
266
+ />
267
+ </Step>
268
+ </div>
269
+
270
+ <div hidden={currentStep !== 1}>
271
+ <Step>
272
+ <Controller
273
+ name="firstName"
274
+ rules={{ required: 'First name is required' }}
275
+ render={({ field }) => <input {...field} placeholder="First Name" />}
276
+ />
277
+ <Controller
278
+ name="lastName"
279
+ rules={{ required: 'Last name is required' }}
280
+ render={({ field }) => <input {...field} placeholder="Last Name" />}
281
+ />
282
+ </Step>
283
+ </div>
284
+
285
+ <Navigation />
286
+ </>
287
+ )}
288
+ </Form>
289
+ )
290
+ }
291
+
292
+ function Navigation() {
293
+ const { next, prev, isFirstStep, isLastStep } = useFormContext()
294
+
295
+ return (
296
+ <div>
297
+ {!isFirstStep && <button type="button" onClick={prev}>Back</button>}
298
+ {!isLastStep && <button type="button" onClick={next}>Next</button>}
299
+ {isLastStep && <button type="submit">Submit</button>}
300
+ </div>
301
+ )
302
+ }
303
+ ```
304
+
305
+ ### Jump to a Specific Step
306
+
307
+ ```tsx
308
+ function StepIndicator() {
309
+ const { currentStep, setCurrentStep } = useFormContext()
310
+
311
+ return (
312
+ <nav>
313
+ <button type="button" onClick={() => setCurrentStep(0)}>Account</button>
314
+ <button type="button" onClick={() => setCurrentStep(1)}>Profile</button>
315
+ <button type="button" onClick={() => setCurrentStep(2)}>Review</button>
316
+ </nav>
317
+ )
318
+ }
319
+ ```
320
+
321
+ ### Skip Validation
322
+
323
+ ```tsx
324
+ <Form form={form} onSubmit={handleSubmit} stepValidationMode="none">
325
+ {/* Users can freely navigate between steps without validation */}
326
+ </Form>
327
+ ```
328
+
329
+ ### Accessing the Form Instance
330
+
331
+ `useFormContext` returns the full `react-hook-form` instance under the `form` property:
332
+
333
+ ```tsx
334
+ function ResetButton() {
335
+ const { form } = useFormContext<MyFormValues>()
336
+
337
+ return (
338
+ <button type="button" onClick={() => form.reset()}>
339
+ Reset
340
+ </button>
341
+ )
342
+ }
343
+ ```
344
+
345
+ ## Types
346
+
347
+ ```ts
348
+ import type {
349
+ FormContextValue,
350
+ FormProps,
351
+ ControllerProps,
352
+ ControllerRenderArgs,
353
+ StepTree,
354
+ StepValidationMode,
355
+ } from 'rhf-stepper'
356
+ ```
357
+
358
+ | Type | Description |
359
+ |------|-------------|
360
+ | `FormContextValue<T>` | Return type of `useFormContext()` |
361
+ | `FormProps<T>` | Props for the `<Form>` component |
362
+ | `ControllerProps<T, N>` | Props for the `<Controller>` component |
363
+ | `ControllerRenderArgs<T, N>` | Arguments passed to the Controller `render` function |
364
+ | `StepTree` | Recursive type representing the step structure (`string \| StepTree[]`) |
365
+ | `StepValidationMode` | `'all' \| 'forward' \| 'none'` |
366
+
367
+ ## License
368
+
369
+ MIT
package/dist/index.d.mts CHANGED
@@ -17,8 +17,7 @@ type StepTree = StepTree[] | string;
17
17
  type StepValidationMode = 'all' | 'forward' | 'none';
18
18
 
19
19
  type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
20
- form: UseFormReturn<TFieldValues, any, any>;
21
- step?: number;
20
+ form: UseFormReturn<TFieldValues>;
22
21
  currentStep: number | number[] | null;
23
22
  setCurrentStep: (step: number | number[]) => Promise<void>;
24
23
  currentStepNode: StepTree | undefined;
@@ -26,16 +25,12 @@ type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
26
25
  validatedFields: string[];
27
26
  isFirstStep: boolean;
28
27
  isLastStep: boolean;
29
- registrationKey: number;
30
28
  next: () => void;
31
29
  prev: () => void;
32
- rebuildSteps: () => void;
33
- registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void;
34
- changeStepAtIndex: (elements: StepTree, index: number) => void;
35
30
  };
36
31
  declare function useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues>;
37
32
  interface FormProps<TFieldValues extends FieldValues = FieldValues> extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {
38
- form: UseFormReturn<TFieldValues, any, any>;
33
+ form: UseFormReturn<TFieldValues>;
39
34
  onSubmit: (values: TFieldValues) => void;
40
35
  stepValidationMode?: StepValidationMode;
41
36
  children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode);
package/dist/index.d.ts CHANGED
@@ -17,8 +17,7 @@ type StepTree = StepTree[] | string;
17
17
  type StepValidationMode = 'all' | 'forward' | 'none';
18
18
 
19
19
  type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
20
- form: UseFormReturn<TFieldValues, any, any>;
21
- step?: number;
20
+ form: UseFormReturn<TFieldValues>;
22
21
  currentStep: number | number[] | null;
23
22
  setCurrentStep: (step: number | number[]) => Promise<void>;
24
23
  currentStepNode: StepTree | undefined;
@@ -26,16 +25,12 @@ type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
26
25
  validatedFields: string[];
27
26
  isFirstStep: boolean;
28
27
  isLastStep: boolean;
29
- registrationKey: number;
30
28
  next: () => void;
31
29
  prev: () => void;
32
- rebuildSteps: () => void;
33
- registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void;
34
- changeStepAtIndex: (elements: StepTree, index: number) => void;
35
30
  };
36
31
  declare function useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues>;
37
32
  interface FormProps<TFieldValues extends FieldValues = FieldValues> extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {
38
- form: UseFormReturn<TFieldValues, any, any>;
33
+ form: UseFormReturn<TFieldValues>;
39
34
  onSubmit: (values: TFieldValues) => void;
40
35
  stepValidationMode?: StepValidationMode;
41
36
  children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode);
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ function useStep() {
11
11
  }
12
12
  function Step({ children }) {
13
13
  const stepContext = useStep();
14
- const formContext = useFormContext();
14
+ const formContext = useInternalFormContext();
15
15
  const {
16
16
  registerStep: registerStepFromParent,
17
17
  step: stepFromParent,
@@ -162,6 +162,7 @@ function getNextLeafParentPath(tree, path) {
162
162
  return null;
163
163
  }
164
164
  var FormContext = react.createContext(null);
165
+ var InternalFormContext = react.createContext(null);
165
166
  function useFormContext() {
166
167
  const context = react.useContext(FormContext);
167
168
  if (!context) {
@@ -169,6 +170,13 @@ function useFormContext() {
169
170
  }
170
171
  return context;
171
172
  }
173
+ function useInternalFormContext() {
174
+ const context = react.useContext(InternalFormContext);
175
+ if (!context) {
176
+ throw new Error("useInternalFormContext must be used within a <Form>");
177
+ }
178
+ return context;
179
+ }
172
180
  function FormInner({ form, onSubmit, stepValidationMode = "forward", children, ...props }, ref) {
173
181
  const [steps, setSteps] = react.useState([]);
174
182
  const [currentStep, setCurrentStep] = react.useState(null);
@@ -266,10 +274,8 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
266
274
  return newSteps;
267
275
  });
268
276
  }, []);
269
- const contextValue = react.useMemo(
277
+ const publicContextValue = react.useMemo(
270
278
  () => ({
271
- // eslint-disable-next-line react-hooks/refs
272
- step: stepRef.current,
273
279
  currentStep: _currentStep,
274
280
  setCurrentStep: _setCurrentStep,
275
281
  currentStepNode,
@@ -278,12 +284,8 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
278
284
  isFirstStep,
279
285
  isLastStep,
280
286
  form,
281
- registrationKey,
282
287
  next,
283
- prev,
284
- rebuildSteps,
285
- registerStep,
286
- changeStepAtIndex
288
+ prev
287
289
  }),
288
290
  [
289
291
  form,
@@ -294,21 +296,28 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
294
296
  validatedFields,
295
297
  isFirstStep,
296
298
  isLastStep,
297
- registrationKey,
298
299
  next,
299
- prev,
300
+ prev
301
+ ]
302
+ );
303
+ const internalContextValue = react.useMemo(
304
+ () => ({
305
+ // eslint-disable-next-line react-hooks/refs
306
+ step: stepRef.current,
307
+ registrationKey,
300
308
  rebuildSteps,
301
309
  registerStep,
302
310
  changeStepAtIndex
303
- ]
311
+ }),
312
+ [registrationKey, rebuildSteps, registerStep, changeStepAtIndex]
304
313
  );
305
314
  const resolvedChildren = (
306
315
  // eslint-disable-next-line react-hooks/refs
307
- typeof children === "function" ? children(contextValue) : children
316
+ typeof children === "function" ? children(publicContextValue) : children
308
317
  );
309
318
  return (
310
319
  // eslint-disable-next-line react-hooks/refs
311
- /* @__PURE__ */ jsxRuntime.jsx(FormContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsx("form", { ref, onSubmit: form.handleSubmit(onSubmit), ...props, children: /* @__PURE__ */ jsxRuntime.jsx(Step, { children: resolvedChildren }) }) })
320
+ /* @__PURE__ */ jsxRuntime.jsx(FormContext.Provider, { value: publicContextValue, children: /* @__PURE__ */ jsxRuntime.jsx(InternalFormContext.Provider, { value: internalContextValue, children: /* @__PURE__ */ jsxRuntime.jsx("form", { ref, onSubmit: form.handleSubmit(onSubmit), ...props, children: /* @__PURE__ */ jsxRuntime.jsx(Step, { children: resolvedChildren }) }) }) })
312
321
  );
313
322
  }
314
323
  var Form = react.forwardRef(FormInner);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["createContext","useContext","useRef","useState","useCallback","steps","useEffect","stepRef","useMemo","prev","jsx","forwardRef","RHFController"],"mappings":";;;;;;;AAcA,IAAM,WAAA,GAAcA,oBAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAOC,iBAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,cAAA,EAAe;AAEnC,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AACnB,EAAA,MAAM,OAAA,GAAUC,aAA2B,MA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nimport { useFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues, any, any>\n step?: number\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n registrationKey: number\n next: () => void\n prev: () => void\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues, any, any>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const contextValue = useMemo<FormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues, any, any>,\n registrationKey,\n next,\n prev,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n registrationKey,\n next,\n prev,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n ],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(contextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={contextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext }\nexport type { FormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
1
+ {"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["createContext","useContext","useRef","useState","useCallback","steps","useEffect","stepRef","useMemo","prev","jsx","forwardRef","RHFController"],"mappings":";;;;;;;AAcA,IAAM,WAAA,GAAcA,oBAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAOC,iBAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,sBAAA,EAAuB;AAE3C,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AACnB,EAAA,MAAM,OAAA,GAAUC,aAA2B,MAAS,CAAA;AACpD,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIC,cAAA,CAAmB,EAAE,CAAA;AAC/C,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAkB,CAAA,GAAIA,eAAS,CAAC,CAAA;AAExD,EAAA,MAAM,iBAAA,GAAoBC,iBAAA,CAAY,CAACC,MAAAA,EAAiB,KAAA,KAAwB;AAC9E,IAAA,QAAA,CAAS,CAAC,SAAA,KAAc;AACtB,MAAA,MAAM,QAAA,GAAW,CAAC,GAAG,SAAS,C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UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.js","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, InternalFormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
package/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ function useStep() {
9
9
  }
10
10
  function Step({ children }) {
11
11
  const stepContext = useStep();
12
- const formContext = useFormContext();
12
+ const formContext = useInternalFormContext();
13
13
  const {
14
14
  registerStep: registerStepFromParent,
15
15
  step: stepFromParent,
@@ -160,6 +160,7 @@ function getNextLeafParentPath(tree, path) {
160
160
  return null;
161
161
  }
162
162
  var FormContext = createContext(null);
163
+ var InternalFormContext = createContext(null);
163
164
  function useFormContext() {
164
165
  const context = useContext(FormContext);
165
166
  if (!context) {
@@ -167,6 +168,13 @@ function useFormContext() {
167
168
  }
168
169
  return context;
169
170
  }
171
+ function useInternalFormContext() {
172
+ const context = useContext(InternalFormContext);
173
+ if (!context) {
174
+ throw new Error("useInternalFormContext must be used within a <Form>");
175
+ }
176
+ return context;
177
+ }
170
178
  function FormInner({ form, onSubmit, stepValidationMode = "forward", children, ...props }, ref) {
171
179
  const [steps, setSteps] = useState([]);
172
180
  const [currentStep, setCurrentStep] = useState(null);
@@ -264,10 +272,8 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
264
272
  return newSteps;
265
273
  });
266
274
  }, []);
267
- const contextValue = useMemo(
275
+ const publicContextValue = useMemo(
268
276
  () => ({
269
- // eslint-disable-next-line react-hooks/refs
270
- step: stepRef.current,
271
277
  currentStep: _currentStep,
272
278
  setCurrentStep: _setCurrentStep,
273
279
  currentStepNode,
@@ -276,12 +282,8 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
276
282
  isFirstStep,
277
283
  isLastStep,
278
284
  form,
279
- registrationKey,
280
285
  next,
281
- prev,
282
- rebuildSteps,
283
- registerStep,
284
- changeStepAtIndex
286
+ prev
285
287
  }),
286
288
  [
287
289
  form,
@@ -292,21 +294,28 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
292
294
  validatedFields,
293
295
  isFirstStep,
294
296
  isLastStep,
295
- registrationKey,
296
297
  next,
297
- prev,
298
+ prev
299
+ ]
300
+ );
301
+ const internalContextValue = useMemo(
302
+ () => ({
303
+ // eslint-disable-next-line react-hooks/refs
304
+ step: stepRef.current,
305
+ registrationKey,
298
306
  rebuildSteps,
299
307
  registerStep,
300
308
  changeStepAtIndex
301
- ]
309
+ }),
310
+ [registrationKey, rebuildSteps, registerStep, changeStepAtIndex]
302
311
  );
303
312
  const resolvedChildren = (
304
313
  // eslint-disable-next-line react-hooks/refs
305
- typeof children === "function" ? children(contextValue) : children
314
+ typeof children === "function" ? children(publicContextValue) : children
306
315
  );
307
316
  return (
308
317
  // eslint-disable-next-line react-hooks/refs
309
- /* @__PURE__ */ jsx(FormContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx("form", { ref, onSubmit: form.handleSubmit(onSubmit), ...props, children: /* @__PURE__ */ jsx(Step, { children: resolvedChildren }) }) })
318
+ /* @__PURE__ */ jsx(FormContext.Provider, { value: publicContextValue, children: /* @__PURE__ */ jsx(InternalFormContext.Provider, { value: internalContextValue, children: /* @__PURE__ */ jsx("form", { ref, onSubmit: form.handleSubmit(onSubmit), ...props, children: /* @__PURE__ */ jsx(Step, { children: resolvedChildren }) }) }) })
310
319
  );
311
320
  }
312
321
  var Form = forwardRef(FormInner);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["steps","stepRef","createContext","useContext","useState","useRef","useMemo","useCallback","prev","jsx","useEffect","RHFController"],"mappings":";;;;;AAcA,IAAM,WAAA,GAAc,cAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAO,WAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,cAAA,EAAe;AAEnC,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AACnB,EAAA,MAAM,OAAA,GAAU,OAA2B,MAAS,CAAA;AACpD,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,QAAA,CAAmB,EAAE,CAAA;AAC/C,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAkB,CAAA,GAAI,SAAS,CAAC,CAAA;AAExD,EAAA,MAAM,iBAAA,GAAoB,WAAA,CAAY,CAACA,MAAAA,EAAiB,KAAA,KAAwB;AAC9E,IAAA,QAAA,CAAS,CAAC,SAAA,KAAc;AACtB,MAAA,MAAM,QAAA,GAAW,CAAC,GAAG,SAAS,CAAA;AAC9B,MAAA,QAAA,CAAS,KAAK,CAAA,GAAIA,MAAAA;AAClB,MAAA,OAAO,QAAA;AAAA,IACT,CAAC,CAAA;AAAA,EACH,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,IAAI,WAAA,EAAa;AACf,MAAA,WAAA,CAAY,aAAA,CAAc,CAAC,IAAc,CAAC,CAAA;AAAA,IAC5C;AAAA,EACF,CAAA,EAAG,CAAC,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,eAAe,CAAC,CAAA;AAEjC,EAAAA,UAAU,MAAM;AACd,IAAA,IAAA,CAAI,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,UAAS,MAAA,EAAW;AACnC,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOD,GAAAA,CAACE,YAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.mjs","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues, any, any>\n step?: number\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n registrationKey: number\n next: () => void\n prev: () => void\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues, any, any>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const contextValue = useMemo<FormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues, any, any>,\n registrationKey,\n next,\n prev,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n registrationKey,\n next,\n prev,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n ],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(contextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={contextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext }\nexport type { FormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
1
+ {"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["steps","stepRef","createContext","useContext","useState","useRef","useMemo","useCallback","prev","jsx","useEffect","RHFController"],"mappings":";;;;;AAcA,IAAM,WAAA,GAAc,cAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAO,WAAW,WAAW,CAAA;AAC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CAAK,OAAA;AAErD,EAAAC,UAAU,MAAM;AACd,IAAA,IAAI,WAAA,EAAa;AACf,MAAA,WAAA,CAAY,aAAA,CAAc,CAAC,IAAc,CAAC,CAAA;AAAA,IAC5C;AAAA,EACF,CAAA,EAAG,CAAC,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,eAAe,CAAC,CAAA;AAEjC,EAAAA,UAAU,MAAM;AACd,IAAA,IAAA,CAAI,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,UAAS,MAAA,EAAW;AACnC,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOD,GAAAA,CAACE,YAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.mjs","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, InternalFormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhf-stepper",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A multi-step form helper for react-hook-form",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",