rhf-stepper 0.1.7 → 0.2.0

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<void>;
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: () => void;
29
- prev: () => void;
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<void>;
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: () => void;
29
- prev: () => void;
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
@@ -201,7 +201,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
201
201
  return isLeafParent(currentStepNode) ? currentStepNode : null;
202
202
  }, [currentStepNode]);
203
203
  const _setCurrentStep = react.useCallback(
204
- async (step) => {
204
+ async (step, beforeStepChange) => {
205
205
  const path = typeof step === "number" ? [0, step] : [0, ...step];
206
206
  if (currentStep && currentStepArr && stepValidationMode !== "none") {
207
207
  const isForward = comparePaths(path, currentStep) > 0;
@@ -210,12 +210,21 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
210
210
  const isValid = await form.trigger(currentStepArr);
211
211
  if (!isValid) {
212
212
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
213
- return;
213
+ return false;
214
214
  }
215
215
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
216
216
  }
217
217
  }
218
+ if (beforeStepChange && currentStepArr) {
219
+ const watchedValues = form.watch(currentStepArr);
220
+ const values = currentStepArr.reduce((acc, field, i) => {
221
+ acc[field] = watchedValues[i];
222
+ return acc;
223
+ }, {});
224
+ await beforeStepChange(values);
225
+ }
218
226
  setCurrentStep(path);
227
+ return true;
219
228
  },
220
229
  [currentStep, currentStepArr, form, stepValidationMode]
221
230
  );
@@ -227,35 +236,55 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
227
236
  () => currentStep ? getNextLeafParentPath(steps, currentStep) === null : true,
228
237
  [steps, currentStep]
229
238
  );
230
- const next = react.useCallback(async () => {
231
- if (!currentStep) return;
239
+ const next = react.useCallback(async (beforeStepChange) => {
240
+ if (!currentStep) return false;
232
241
  const nextPath = getNextLeafParentPath(steps, currentStep);
233
242
  if (currentStepArr && stepValidationMode !== "none") {
234
243
  const isValid = await form.trigger(currentStepArr);
235
244
  if (!isValid) {
236
245
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
237
- return;
246
+ return false;
238
247
  }
239
248
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
240
249
  }
241
250
  if (nextPath) {
251
+ if (beforeStepChange && currentStepArr) {
252
+ const watchedValues = form.watch(currentStepArr);
253
+ const values = currentStepArr.reduce((acc, field, i) => {
254
+ acc[field] = watchedValues[i];
255
+ return acc;
256
+ }, {});
257
+ await beforeStepChange(values);
258
+ }
242
259
  setCurrentStep(nextPath);
260
+ return true;
243
261
  }
262
+ return false;
244
263
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
245
- const prev = react.useCallback(async () => {
246
- if (!currentStep) return;
264
+ const prev = react.useCallback(async (beforeStepChange) => {
265
+ if (!currentStep) return false;
247
266
  const prevPath = getPrevLeafParentPath(steps, currentStep);
248
267
  if (currentStepArr && stepValidationMode === "all") {
249
268
  const isValid = await form.trigger(currentStepArr);
250
269
  if (!isValid) {
251
270
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
252
- return;
271
+ return false;
253
272
  }
254
273
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
255
274
  }
256
275
  if (prevPath) {
276
+ if (beforeStepChange && currentStepArr) {
277
+ const watchedValues = form.watch(currentStepArr);
278
+ const values = currentStepArr.reduce((acc, field, i) => {
279
+ acc[field] = watchedValues[i];
280
+ return acc;
281
+ }, {});
282
+ await beforeStepChange(values);
283
+ }
257
284
  setCurrentStep(prevPath);
285
+ return true;
258
286
  }
287
+ return false;
259
288
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
260
289
  const registerStep = react.useCallback(
261
290
  (elements, stepRef2, step) => {
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,oBAAuC,IAAI,CAA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W;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<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(async () => {\n if (!currentStep) return\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\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n setCurrentStep(prevPath)\n }\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,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,CAAA;AAC9B,MAAA,QAAA,CAAS,KAAK,CAAA,GAAIA,MAAAA;AACl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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,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[], 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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
@@ -199,7 +199,7 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
199
199
  return isLeafParent(currentStepNode) ? currentStepNode : null;
200
200
  }, [currentStepNode]);
201
201
  const _setCurrentStep = useCallback(
202
- async (step) => {
202
+ async (step, beforeStepChange) => {
203
203
  const path = typeof step === "number" ? [0, step] : [0, ...step];
204
204
  if (currentStep && currentStepArr && stepValidationMode !== "none") {
205
205
  const isForward = comparePaths(path, currentStep) > 0;
@@ -208,12 +208,21 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
208
208
  const isValid = await form.trigger(currentStepArr);
209
209
  if (!isValid) {
210
210
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
211
- return;
211
+ return false;
212
212
  }
213
213
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
214
214
  }
215
215
  }
216
+ if (beforeStepChange && currentStepArr) {
217
+ const watchedValues = form.watch(currentStepArr);
218
+ const values = currentStepArr.reduce((acc, field, i) => {
219
+ acc[field] = watchedValues[i];
220
+ return acc;
221
+ }, {});
222
+ await beforeStepChange(values);
223
+ }
216
224
  setCurrentStep(path);
225
+ return true;
217
226
  },
218
227
  [currentStep, currentStepArr, form, stepValidationMode]
219
228
  );
@@ -225,35 +234,55 @@ function FormInner({ form, onSubmit, stepValidationMode = "forward", children, .
225
234
  () => currentStep ? getNextLeafParentPath(steps, currentStep) === null : true,
226
235
  [steps, currentStep]
227
236
  );
228
- const next = useCallback(async () => {
229
- if (!currentStep) return;
237
+ const next = useCallback(async (beforeStepChange) => {
238
+ if (!currentStep) return false;
230
239
  const nextPath = getNextLeafParentPath(steps, currentStep);
231
240
  if (currentStepArr && stepValidationMode !== "none") {
232
241
  const isValid = await form.trigger(currentStepArr);
233
242
  if (!isValid) {
234
243
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
235
- return;
244
+ return false;
236
245
  }
237
246
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
238
247
  }
239
248
  if (nextPath) {
249
+ if (beforeStepChange && currentStepArr) {
250
+ const watchedValues = form.watch(currentStepArr);
251
+ const values = currentStepArr.reduce((acc, field, i) => {
252
+ acc[field] = watchedValues[i];
253
+ return acc;
254
+ }, {});
255
+ await beforeStepChange(values);
256
+ }
240
257
  setCurrentStep(nextPath);
258
+ return true;
241
259
  }
260
+ return false;
242
261
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
243
- const prev = useCallback(async () => {
244
- if (!currentStep) return;
262
+ const prev = useCallback(async (beforeStepChange) => {
263
+ if (!currentStep) return false;
245
264
  const prevPath = getPrevLeafParentPath(steps, currentStep);
246
265
  if (currentStepArr && stepValidationMode === "all") {
247
266
  const isValid = await form.trigger(currentStepArr);
248
267
  if (!isValid) {
249
268
  setValidatedFields((prev2) => prev2.filter((field) => !currentStepArr.includes(field)));
250
- return;
269
+ return false;
251
270
  }
252
271
  setValidatedFields((prev2) => [.../* @__PURE__ */ new Set([...prev2, ...currentStepArr])]);
253
272
  }
254
273
  if (prevPath) {
274
+ if (beforeStepChange && currentStepArr) {
275
+ const watchedValues = form.watch(currentStepArr);
276
+ const values = currentStepArr.reduce((acc, field, i) => {
277
+ acc[field] = watchedValues[i];
278
+ return acc;
279
+ }, {});
280
+ await beforeStepChange(values);
281
+ }
255
282
  setCurrentStep(prevPath);
283
+ return true;
256
284
  }
285
+ return false;
257
286
  }, [steps, currentStep, currentStepArr, form, stepValidationMode]);
258
287
  const registerStep = useCallback(
259
288
  (elements, stepRef2, step) => {
@@ -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,OAA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A;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 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(async () => {\n if (!currentStep) return\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\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (prevPath) {\n setCurrentStep(prevPath)\n }\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,C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,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 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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 = currentStepArr.reduce((acc, field, i) => { acc[field] = watchedValues[i]; return acc }, {} as Record<string, unknown>) 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.7",
3
+ "version": "0.2.0",
4
4
  "description": "A multi-step form helper for react-hook-form",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",