rhf-stepper 0.1.8 → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Omer Kosar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A lightweight, headless 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. Bring your own UI — rhf-stepper handles the logic.
4
4
 
5
+ [Documentation](https://rhf-stepper-docs-git-master-omerrkosars-projects.vercel.app/docs)
6
+
5
7
  ## Installation
6
8
 
7
9
  ```bash
package/dist/index.d.mts CHANGED
@@ -19,14 +19,14 @@ type StepValidationMode = 'all' | 'forward' | 'none';
19
19
  type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
20
20
  form: UseFormReturn<TFieldValues>;
21
21
  currentStep: number | number[] | null;
22
- setCurrentStep: (step: number | number[]) => Promise<boolean>;
22
+ setCurrentStep: (step: number | number[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
23
23
  currentStepNode: StepTree | undefined;
24
24
  currentStepArr: string[] | null;
25
25
  validatedFields: string[];
26
26
  isFirstStep: boolean;
27
27
  isLastStep: boolean;
28
- next: () => Promise<boolean>;
29
- prev: () => Promise<boolean>;
28
+ next: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
29
+ prev: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
30
30
  };
31
31
  declare function useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues>;
32
32
  interface FormProps<TFieldValues extends FieldValues = FieldValues> extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {
package/dist/index.d.ts CHANGED
@@ -19,14 +19,14 @@ type StepValidationMode = 'all' | 'forward' | 'none';
19
19
  type FormContextValue<TFieldValues extends FieldValues = FieldValues> = {
20
20
  form: UseFormReturn<TFieldValues>;
21
21
  currentStep: number | number[] | null;
22
- setCurrentStep: (step: number | number[]) => Promise<boolean>;
22
+ setCurrentStep: (step: number | number[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
23
23
  currentStepNode: StepTree | undefined;
24
24
  currentStepArr: string[] | null;
25
25
  validatedFields: string[];
26
26
  isFirstStep: boolean;
27
27
  isLastStep: boolean;
28
- next: () => Promise<boolean>;
29
- prev: () => Promise<boolean>;
28
+ next: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
29
+ prev: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>;
30
30
  };
31
31
  declare function useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues>;
32
32
  interface FormProps<TFieldValues extends FieldValues = FieldValues> extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {
package/dist/index.js CHANGED
@@ -89,6 +89,24 @@ function Step({ children }) {
89
89
  return /* @__PURE__ */ jsxRuntime.jsx(StepContext.Provider, { value: contextValue, children });
90
90
  }
91
91
  Step.displayName = "Step";
92
+ function buildNestedValues(fields, values) {
93
+ const result = {};
94
+ for (let i = 0; i < fields.length; i++) {
95
+ const segments = fields[i].split(".");
96
+ let current = result;
97
+ for (let j = 0; j < segments.length - 1; j++) {
98
+ const segment = segments[j];
99
+ const nextSegment = segments[j + 1];
100
+ const isNextNumeric = /^\d+$/.test(nextSegment);
101
+ if (current[segment] === void 0) {
102
+ current[segment] = isNextNumeric ? [] : {};
103
+ }
104
+ current = current[segment];
105
+ }
106
+ current[segments[segments.length - 1]] = values[i];
107
+ }
108
+ return result;
109
+ }
92
110
  function comparePaths(a, b) {
93
111
  var _a, _b;
94
112
  for (let i = 0; i < Math.max(a.length, b.length); i++) {
@@ -201,7 +219,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
201
219
  return isLeafParent(currentStepNode) ? currentStepNode : null;
202
220
  }, [currentStepNode]);
203
221
  const _setCurrentStep = react.useCallback(
204
- async (step) => {
222
+ async (step, beforeStepChange) => {
205
223
  const path = typeof step === "number" ? [0, step] : [0, ...step];
206
224
  if (currentStep && currentStepArr && stepValidationMode !== "none") {
207
225
  const isForward = comparePaths(path, currentStep) > 0;
@@ -215,6 +233,11 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
215
233
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
216
234
  }
217
235
  }
236
+ if (beforeStepChange && currentStepArr) {
237
+ const watchedValues = form.watch(currentStepArr);
238
+ const values = buildNestedValues(currentStepArr, watchedValues);
239
+ await beforeStepChange(values);
240
+ }
218
241
  setCurrentStep(path);
219
242
  return true;
220
243
  },
@@ -228,7 +251,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
228
251
  () => currentStep ? getNextLeafParentPath(steps, currentStep) === null : true,
229
252
  [steps, currentStep]
230
253
  );
231
- const next = react.useCallback(async () => {
254
+ const next = react.useCallback(async (beforeStepChange) => {
232
255
  if (!currentStep) return false;
233
256
  const nextPath = getNextLeafParentPath(steps, currentStep);
234
257
  if (currentStepArr && stepValidationMode !== "none") {
@@ -240,12 +263,17 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
240
263
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
241
264
  }
242
265
  if (nextPath) {
266
+ if (beforeStepChange && currentStepArr) {
267
+ const watchedValues = form.watch(currentStepArr);
268
+ const values = buildNestedValues(currentStepArr, watchedValues);
269
+ await beforeStepChange(values);
270
+ }
243
271
  setCurrentStep(nextPath);
244
272
  return true;
245
273
  }
246
274
  return false;
247
275
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
248
- const prev = react.useCallback(async () => {
276
+ const prev = react.useCallback(async (beforeStepChange) => {
249
277
  if (!currentStep) return false;
250
278
  const prevPath = getPrevLeafParentPath(steps, currentStep);
251
279
  if (currentStepArr && stepValidationMode === "all") {
@@ -257,6 +285,11 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
257
285
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
258
286
  }
259
287
  if (prevPath) {
288
+ if (beforeStepChange && currentStepArr) {
289
+ const watchedValues = form.watch(currentStepArr);
290
+ const values = buildNestedValues(currentStepArr, watchedValues);
291
+ await beforeStepChange(values);
292
+ }
260
293
  setCurrentStep(prevPath);
261
294
  return true;
262
295
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["createContext","useContext","useMemo","useRef","useState","useCallback","steps","useEffect","stepRef","prev","jsx","forwardRef","RHFController"],"mappings":";;;;;;;AAcA,IAAM,WAAA,GAAcA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AC,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,eAAe,CAAC,CAAA;AAEjC,EAAAA,gBAAU,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,uBAAOG,cAAAA,CAACE,wBAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,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\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\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 (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\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<boolean>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => Promise<boolean>\n prev: () => Promise<boolean>\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[]): Promise<boolean> => {\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n return true\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 (): Promise<boolean> => {\n if (!currentStep) return false\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(async (): Promise<boolean> => {\n if (!currentStep) return false\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode === 'all') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n setCurrentStep(prevPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\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, FormProps, InternalFormContextValue }\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","useMemo","useRef","useState","useCallback","steps","useEffect","stepRef","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;AAEnB,EAAA,MAAM,MAAA,GAASC,cAAQ,MAAM;AAC3B,IAAA,IAAI,CAAC,aAAa,OAAO,IAAA;AACzB,IAAA,OAAO,KAAA;AAAA,EACT,CAAA,EAAG,CAAC,WAAW,CAAC,CAAA;AAChB,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,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A,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOG,cAAAA,CAACE,wBAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,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\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\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 (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\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 buildNestedValues(fields: string[], values: unknown[]): Record<string, unknown> {\n const result: Record<string, unknown> = {}\n for (let i = 0; i < fields.length; i++) {\n const segments = fields[i].split('.')\n let current: Record<string, unknown> = result\n for (let j = 0; j < segments.length - 1; j++) {\n const segment = segments[j]\n const nextSegment = segments[j + 1]\n const isNextNumeric = /^\\d+$/.test(nextSegment)\n if (current[segment] === undefined) {\n current[segment] = isNextNumeric ? [] : {}\n }\n current = current[segment] as Record<string, unknown>\n }\n current[segments[segments.length - 1]] = values[i]\n }\n return result\n}\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[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\n prev: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\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[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(path)\n return true\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 (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\n if (!currentStep) return false\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(nextPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(async (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\n if (!currentStep) return false\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode === 'all') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(prevPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\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, FormProps, InternalFormContextValue }\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
@@ -87,6 +87,24 @@ function Step({ children }) {
87
87
  return /* @__PURE__ */ jsx(StepContext.Provider, { value: contextValue, children });
88
88
  }
89
89
  Step.displayName = "Step";
90
+ function buildNestedValues(fields, values) {
91
+ const result = {};
92
+ for (let i = 0; i < fields.length; i++) {
93
+ const segments = fields[i].split(".");
94
+ let current = result;
95
+ for (let j = 0; j < segments.length - 1; j++) {
96
+ const segment = segments[j];
97
+ const nextSegment = segments[j + 1];
98
+ const isNextNumeric = /^\d+$/.test(nextSegment);
99
+ if (current[segment] === void 0) {
100
+ current[segment] = isNextNumeric ? [] : {};
101
+ }
102
+ current = current[segment];
103
+ }
104
+ current[segments[segments.length - 1]] = values[i];
105
+ }
106
+ return result;
107
+ }
90
108
  function comparePaths(a, b) {
91
109
  var _a, _b;
92
110
  for (let i = 0; i < Math.max(a.length, b.length); i++) {
@@ -199,7 +217,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
199
217
  return isLeafParent(currentStepNode) ? currentStepNode : null;
200
218
  }, [currentStepNode]);
201
219
  const _setCurrentStep = useCallback(
202
- async (step) => {
220
+ async (step, beforeStepChange) => {
203
221
  const path = typeof step === "number" ? [0, step] : [0, ...step];
204
222
  if (currentStep && currentStepArr && stepValidationMode !== "none") {
205
223
  const isForward = comparePaths(path, currentStep) > 0;
@@ -213,6 +231,11 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
213
231
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
214
232
  }
215
233
  }
234
+ if (beforeStepChange && currentStepArr) {
235
+ const watchedValues = form.watch(currentStepArr);
236
+ const values = buildNestedValues(currentStepArr, watchedValues);
237
+ await beforeStepChange(values);
238
+ }
216
239
  setCurrentStep(path);
217
240
  return true;
218
241
  },
@@ -226,7 +249,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
226
249
  () => currentStep ? getNextLeafParentPath(steps, currentStep) === null : true,
227
250
  [steps, currentStep]
228
251
  );
229
- const next = useCallback(async () => {
252
+ const next = useCallback(async (beforeStepChange) => {
230
253
  if (!currentStep) return false;
231
254
  const nextPath = getNextLeafParentPath(steps, currentStep);
232
255
  if (currentStepArr && stepValidationMode !== "none") {
@@ -238,12 +261,17 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
238
261
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
239
262
  }
240
263
  if (nextPath) {
264
+ if (beforeStepChange && currentStepArr) {
265
+ const watchedValues = form.watch(currentStepArr);
266
+ const values = buildNestedValues(currentStepArr, watchedValues);
267
+ await beforeStepChange(values);
268
+ }
241
269
  setCurrentStep(nextPath);
242
270
  return true;
243
271
  }
244
272
  return false;
245
273
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
246
- const prev = useCallback(async () => {
274
+ const prev = useCallback(async (beforeStepChange) => {
247
275
  if (!currentStep) return false;
248
276
  const prevPath = getPrevLeafParentPath(steps, currentStep);
249
277
  if (currentStepArr && stepValidationMode === "all") {
@@ -255,6 +283,11 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
255
283
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
256
284
  }
257
285
  if (prevPath) {
286
+ if (beforeStepChange && currentStepArr) {
287
+ const watchedValues = form.watch(currentStepArr);
288
+ const values = buildNestedValues(currentStepArr, watchedValues);
289
+ await beforeStepChange(values);
290
+ }
258
291
  setCurrentStep(prevPath);
259
292
  return true;
260
293
  }
@@ -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,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;AAEnB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A,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\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\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 (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\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<boolean>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => Promise<boolean>\n prev: () => Promise<boolean>\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[]): Promise<boolean> => {\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n return true\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 (): Promise<boolean> => {\n if (!currentStep) return false\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(async (): Promise<boolean> => {\n if (!currentStep) return false\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode === 'all') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n setCurrentStep(prevPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\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, FormProps, InternalFormContextValue }\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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AK,EAAyC;AAClE,EAAA,MAAM,cAAc,cAAA,EAA6B;AACjD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,eAAA,GAAkB,OAAA,IAAA,IAAA,GAAA,OAAA,GAAY,WAAA,CAAY,IAAA,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\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\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 (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\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 buildNestedValues(fields: string[], values: unknown[]): Record<string, unknown> {\n const result: Record<string, unknown> = {}\n for (let i = 0; i < fields.length; i++) {\n const segments = fields[i].split('.')\n let current: Record<string, unknown> = result\n for (let j = 0; j < segments.length - 1; j++) {\n const segment = segments[j]\n const nextSegment = segments[j + 1]\n const isNextNumeric = /^\\d+$/.test(nextSegment)\n if (current[segment] === undefined) {\n current[segment] = isNextNumeric ? [] : {}\n }\n current = current[segment] as Record<string, unknown>\n }\n current[segments[segments.length - 1]] = values[i]\n }\n return result\n}\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[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\n prev: (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>) => Promise<boolean>\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[], beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(path)\n return true\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 (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\n if (!currentStep) return false\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 false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(nextPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(async (beforeStepChange?: (values: Partial<TFieldValues>) => Promise<void>): Promise<boolean> => {\n if (!currentStep) return false\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode === 'all') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return false\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n if (beforeStepChange && currentStepArr) {\n const watchedValues = form.watch(currentStepArr as Path<TFieldValues>[])\n const values = buildNestedValues(currentStepArr, watchedValues) as Partial<TFieldValues>\n await beforeStepChange(values)\n }\n setCurrentStep(prevPath)\n return true\n }\n return false\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\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, FormProps, InternalFormContextValue }\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.8",
3
+ "version": "0.2.1",
4
4
  "description": "A multi-step form helper for react-hook-form",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",