@jfdevelops/react-multi-step-form 1.0.0-alpha.21 → 1.0.0-alpha.23
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/dist/field.cjs +13 -5
- package/dist/field.cjs.map +1 -1
- package/dist/field.d.cts +3 -1
- package/dist/field.d.mts +3 -1
- package/dist/field.mjs +14 -5
- package/dist/field.mjs.map +1 -1
- package/dist/form-config.d.mts +1 -1
- package/dist/hooks/use-selector.cjs +16 -0
- package/dist/hooks/use-selector.cjs.map +1 -0
- package/dist/hooks/use-selector.d.cts +9 -0
- package/dist/hooks/use-selector.d.mts +9 -0
- package/dist/hooks/use-selector.mjs +16 -0
- package/dist/hooks/use-selector.mjs.map +1 -0
- package/dist/selector.cjs +27 -0
- package/dist/selector.cjs.map +1 -0
- package/dist/selector.d.cts +30 -0
- package/dist/selector.d.mts +30 -0
- package/dist/selector.mjs +22 -0
- package/dist/selector.mjs.map +1 -0
- package/dist/step-schema.cjs +59 -52
- package/dist/step-schema.cjs.map +1 -1
- package/dist/step-schema.d.cts +30 -1
- package/dist/step-schema.d.mts +30 -1
- package/dist/step-schema.mjs +60 -53
- package/dist/step-schema.mjs.map +1 -1
- package/dist/utils.cjs +13 -0
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.mjs +13 -1
- package/dist/utils.mjs.map +1 -1
- package/package.json +2 -2
package/dist/step-schema.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"step-schema.cjs","names":["MultiStepFormStepSchemaBase","#internal","MultiStepFormStepSchemaInternal","hookResults: Record<string, unknown>","field","MultiStepFormSchemaConfig","createDefaultValues","MultiStepFormLogger","resolvedCtxCreator"],"sources":["../src/step-schema.ts"],"sourcesContent":["import {\n type AnyStepField,\n type CasingType,\n type Constrain,\n createCtx,\n type CreateHelperFunctionOptionsBase,\n createStep,\n type DefaultCasing,\n type DefaultStorageKey,\n type Expand,\n type GetCurrentStep,\n type GetFieldsForStep,\n type HelperFnChosenSteps,\n type HelperFnCtx,\n type HelperFnInputBase,\n invariant,\n MultiStepFormLogger,\n type MultiStepFormSchemaStepConfig as MultiStepFormSchemaStepBaseConfig,\n MultiStepFormStepSchema as MultiStepFormStepSchemaBase,\n type Relaxed,\n type ResetFn,\n type ResolvedStep as ResolvedCoreStep,\n type Step,\n type StepNumbers,\n type StrippedResolvedStep,\n type UpdateFn,\n type Updater,\n type ValidStepKey,\n} from '@jfdevelops/multi-step-form-core';\nimport { MultiStepFormStepSchemaInternal } from '@jfdevelops/multi-step-form-core/_internals';\nimport type { ComponentPropsWithRef, ReactNode } from 'react';\nimport { field } from './field';\nimport { MultiStepFormSchemaConfig } from './form-config';\nimport { resolvedCtxCreator } from './utils';\n\nexport interface MultiStepFormSchemaStepConfig<\n TStep extends Step<TCasing>,\n TCasing extends CasingType,\n TStorageKey extends string,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object,\n TResolvedStep extends ResolvedStep<TStep, TCasing> = ResolvedStep<\n TStep,\n TCasing\n >\n> extends MultiStepFormSchemaStepBaseConfig<TStep, TCasing, TStorageKey>,\n MultiStepFormSchemaConfig.Form<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n > {}\n\nexport type CreateFunction<TArgs extends any[], TReturn = void> = (\n ...args: TArgs\n) => TReturn;\nexport type CreateComponent<TInput, TProps> = CreateFunction<\n [input: TInput, props: TProps],\n ReactNode\n>;\n\nexport type CreateComponentCallback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps\n> = CreateComponent<\n HelperFnInputBase<TResolvedStep, TSteps, TChosenSteps>,\n TProps\n>;\nexport type CreatedMultiStepFormComponent<TProps> = TProps extends undefined\n ? () => ReactNode\n : (props: TProps) => ReactNode;\nexport type CreateComponentFn<\n TResolvedStep extends AnyResolvedStep,\n // This is needed to make TS happy with all types\n TStepNumbers extends StepNumbers<TResolvedStep>\n> = <\n chosenSteps extends HelperFnChosenSteps<TResolvedStep, TStepNumbers>,\n props = undefined\n>(\n options: CreateHelperFunctionOptionsBase<\n TResolvedStep,\n TStepNumbers,\n chosenSteps\n >,\n fn: CreateComponentCallback<TResolvedStep, TStepNumbers, chosenSteps, props>\n) => CreatedMultiStepFormComponent<props>;\n\nexport namespace StepSpecificComponent {\n // The logic for getting the formCtx only works for step specific `createComponent`\n // (i.e: step1.createComponent(...)) as of now. Reason is because I can't think of a good API for integrating the form\n // ctx into the main `createComponent` since multiple steps can be chosen. In that case\n // how would the logic work for when the form component should be defined in the callback?\n // Ideas:\n // - Make the main `createComponent` return a function that accepts the current step\n export type formComponent<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>\n > = TFormEnabledFor extends MultiStepFormSchemaConfig.defaultEnabledFor\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : TFormEnabledFor extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TFormEnabledFor[number] extends keyof TResolvedStep\n ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TChosenSteps[number] extends keyof TResolvedStep\n ? TChosenSteps[number] extends TFormEnabledFor[number]\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : {}\n : {}\n : {}\n : {}\n : keyof TFormEnabledFor extends keyof TResolvedStep\n ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TChosenSteps[number] extends keyof TResolvedStep\n ? TChosenSteps[number] extends keyof TFormEnabledFor\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : {}\n : {}\n : {}\n : {};\n export type onInputChange<\n TResolvedStep extends AnyResolvedStep,\n TStepNumbers extends StepNumbers<TResolvedStep>,\n TTargetStep extends TStepNumbers\n > = <\n CurrentStepData extends GetCurrentStep<TResolvedStep, TTargetStep>,\n Field extends keyof CurrentStepData\n >(\n field: Field,\n updater: Updater<CurrentStepData[Field], Relaxed<CurrentStepData[Field]>>\n ) => void;\n export type updateWrappers<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TStepNumber extends HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n > = TStepNumber extends TSteps\n ? {\n /**\n * A useful wrapper around `update` to update the specific field.\n */\n onInputChange: UpdateFn.stepSpecific<\n TResolvedStep,\n TSteps,\n ValidStepKey<TStepNumber>\n >;\n /**\n * A useful wrapper for `update` to reset a specific field's value to its\n * original config value.\n * @resetFn\n */\n reset: ResetFn.stepSpecific<\n TResolvedStep,\n TSteps,\n ValidStepKey<TStepNumber>\n >;\n }\n : {};\n export type input<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TAdditionalCtx extends Record<string, unknown>,\n TStepNumber extends HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n > = HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n > = HelperFnInputBase<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n never,\n TAdditionalCtx\n > &\n updateWrappers<TResolvedStep, TSteps, TChosenSteps, TStepNumber> & {\n Field: field.component<\n TResolvedStep,\n ValidStepKey<\n HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n >\n >;\n };\n\n export type callback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TAdditionalInput extends object,\n TAdditionalCtx extends Record<string, unknown>\n > = CreateComponent<\n Expand<\n input<TResolvedStep, TSteps, TChosenSteps, TAdditionalCtx> &\n formComponent<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n > &\n TAdditionalInput\n >,\n TProps\n >;\n export const DEFAULT_FORM_INSTANCE_ALIAS = 'form';\n export type defaultFormInstanceAlias = typeof DEFAULT_FORM_INSTANCE_ALIAS;\n export type formInstanceOptions<\n TAlias extends string,\n TRenderInput,\n TReturn\n > = {\n /**\n * The name of the return value of the `render` method.\n */\n alias?: TAlias;\n /**\n * A function that renders/creates the form instance. This function will be called\n * at the top level of the component, ensuring hooks are called in a valid React context.\n *\n * @param input - The input object containing context and default values\n * @returns The form instance (typically from a hook like `useForm`)\n *\n * @example\n * ```tsx\n * useFormInstance: {\n * render({ defaultValues }) {\n * return useForm({\n * defaultValues,\n * });\n * },\n * }\n * ```\n *\n * **Verification**: The hook call is automatically verified:\n * - Errors are caught and reported with helpful messages\n * - In development, hook calls are logged to console.debug\n * - The hook must be called at the component top level (enforced by the framework)\n */\n render: CreateFunction<[input: TRenderInput], TReturn>;\n };\n\n export interface CtxSelector<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TCtx\n > {\n /**\n * A function to select the data that will be available in the `fn`'s ctx.\n * @param input The available input to create the context with.\n * @returns The created ctx.\n */\n ctxData?: (\n input: HelperFnInputBase<\n TResolvedStep,\n TSteps,\n 'all',\n HelperFnChosenSteps.resolve<TResolvedStep, TSteps, TTargetStep>\n >\n ) => TCtx;\n }\n\n export type options<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormInstanceAlias extends string,\n TFormInstance,\n TCtx\n > = CtxSelector<TResolvedStep, TSteps, TTargetStep, TCtx> & {\n /**\n * If set to `true`, you'll be able to open the {@linkcode console} to view logs.\n */\n debug?: boolean;\n useFormInstance?: formInstanceOptions<\n TFormInstanceAlias,\n HelperFnInputBase<TResolvedStep, TSteps, TTargetStep> & {\n /**\n * An object containing all the default values for the current step.\n */\n defaultValues: Expand<\n ExtractedDefaultValues<TResolvedStep, TSteps, TTargetStep>\n >;\n },\n TFormInstance\n >;\n };\n}\n\nexport type CreateStepSpecificComponentCallback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TAdditionalInput extends object = {},\n TAdditionalCtx extends Record<string, unknown> = {}\n> = StepSpecificComponent.callback<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n TProps,\n TFormAlias,\n TFormProps,\n TFormEnabledFor,\n TAdditionalInput,\n TAdditionalCtx\n>;\n\nexport type ExtractedDefaultValues<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TExtractedStepNumber extends number = HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TTargetStep\n >,\n TFields extends GetFieldsForStep<\n TResolvedStep,\n ValidStepKey<TExtractedStepNumber>\n > = GetFieldsForStep<TResolvedStep, ValidStepKey<TExtractedStepNumber>>\n> = { [field in keyof TFields]: TFields[field]['defaultValue'] };\nexport interface StepSpecificCreateComponentFn<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>\n> {\n /**\n * A utility function to easily create a component for the current step.\n * @param fn The callback function where the component is defined.\n */\n <props = undefined>(\n fn: CreateStepSpecificComponentCallback<\n TResolvedStep,\n TSteps,\n TTargetStep,\n props,\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n >\n ): CreatedMultiStepFormComponent<props>;\n /**\n * A utility function to easily create a component for the current step.\n * @param options Specific config options for creating a component for the current step.\n * @param fn The callback function where the component is defined.\n * @returns The created component.\n */\n <\n formInstance,\n additionalCtx extends Record<string, unknown> = {},\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined\n >(\n options: StepSpecificComponent.options<\n TResolvedStep,\n TSteps,\n TTargetStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >,\n fn: CreateStepSpecificComponentCallback<\n TResolvedStep,\n TSteps,\n TTargetStep,\n props,\n TFormAlias,\n TFormProps,\n TFormEnabledFor,\n { [_ in formInstanceAlias]: formInstance },\n additionalCtx\n >\n ): CreatedMultiStepFormComponent<props>;\n}\n\nexport type ResolvedStep<\n TStep extends Step<TDefaultCasing>,\n TDefaultCasing extends CasingType = DefaultCasing,\n TResolvedStep extends ResolvedCoreStep<\n TStep,\n TDefaultCasing\n > = ResolvedCoreStep<TStep, TDefaultCasing>,\n TFormAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor,\n TFormProps extends object = ComponentPropsWithRef<'form'>\n> = {\n [stepKey in keyof TResolvedStep]: TResolvedStep[stepKey] &\n (stepKey extends ValidStepKey<StepNumbers<TResolvedStep>>\n ? {\n createComponent: StepSpecificCreateComponentFn<\n TResolvedStep,\n StepNumbers<TResolvedStep>,\n [stepKey],\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n >;\n }\n : {});\n};\n\nexport type AnyResolvedStep = ResolvedStep<any, any, any>;\nexport interface HelperFunctions<\n TResolvedStep extends AnyResolvedStep,\n TStepNumbers extends StepNumbers<TResolvedStep>\n> {\n createComponent: CreateComponentFn<TResolvedStep, TStepNumbers>;\n}\nnamespace CreateComponentImplConfig {\n export type stepSpecificConfig<\n TResolvedStep extends AnyResolvedStep,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object\n > = {\n isStepSpecific: true;\n defaultId: string;\n form?: MultiStepFormSchemaConfig.FormConfig<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n >;\n };\n\n export type nonStepSpecific = {\n isStepSpecific: false;\n };\n\n export type config<\n TResolvedStep extends AnyResolvedStep,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object\n > =\n | nonStepSpecific\n | stepSpecificConfig<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n >;\n}\n\n/**\n * Creates a default values object for the target step.\n * @param steps The steps schema.\n * @param targetStep The step to create the default values for.\n * @returns An object containing the field names and their default values.\n */\nexport function createDefaultValues<\n resolvedStep extends AnyResolvedStep,\n stepNumbers extends StepNumbers<resolvedStep>,\n targetStep extends ValidStepKey<stepNumbers>\n>(steps: resolvedStep, targetStep: targetStep) {\n invariant(\n targetStep in steps,\n `The target step ${targetStep} is not a valid step key`\n );\n\n const current = steps[targetStep as unknown as keyof resolvedStep];\n\n invariant('fields' in current, `No \"fields\" were found for ${targetStep}`);\n\n let defaultValues = {};\n\n for (const [fieldName, fieldValues] of Object.entries(\n current.fields as Record<string, Record<string, unknown>>\n )) {\n defaultValues = {\n ...defaultValues,\n [fieldName]: fieldValues.defaultValue,\n };\n }\n\n return defaultValues as Expand<\n ExtractedDefaultValues<resolvedStep, stepNumbers, [targetStep]>\n >;\n}\nexport class MultiStepFormStepSchema<\n step extends Step<casing>,\n casing extends CasingType = DefaultCasing,\n storageKey extends string = DefaultStorageKey,\n formAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias,\n formEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<resolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor,\n formProps extends object = ComponentPropsWithRef<'form'>,\n core extends ResolvedCoreStep<step, casing> = ResolvedCoreStep<\n step,\n casing\n >,\n resolvedStep extends ResolvedStep<\n step,\n casing,\n core,\n formAlias,\n formEnabledFor,\n formProps\n > = ResolvedStep<step, casing, core, formAlias, formEnabledFor, formProps>,\n stepNumbers extends StepNumbers<resolvedStep> = StepNumbers<resolvedStep>\n >\n extends MultiStepFormStepSchemaBase<step, casing>\n implements HelperFunctions<resolvedStep, stepNumbers>\n{\n // @ts-ignore type doesn't match `MultiStepFormSchemaBase.value`\n value: resolvedStep;\n readonly #internal: MultiStepFormStepSchemaInternal<\n step,\n casing,\n resolvedStep,\n stepNumbers\n >;\n\n constructor(\n config: MultiStepFormSchemaStepConfig<\n step,\n Constrain<casing, CasingType>,\n storageKey,\n formAlias,\n formEnabledFor,\n formProps\n >\n ) {\n const { form, ...rest } = config;\n\n super(rest as never);\n\n this.value = createStep(this.original);\n\n this.#internal = new MultiStepFormStepSchemaInternal<\n step,\n casing,\n resolvedStep,\n stepNumbers\n >({\n originalValue: this.original,\n getValue: () => this.value,\n setValue: (next) => this.handlePostUpdate(next as never),\n });\n\n // this.value = this.#internal.enrichValues(resolvedValues);\n this.sync();\n this.value = this.#internal.enrichValues(this.value, (step) => {\n const key = `step${step as stepNumbers}`;\n const stepData = [key] as HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >;\n const id = form?.id ?? key;\n const ctx = createCtx<\n resolvedStep,\n stepNumbers,\n HelperFnChosenSteps.tupleNotation<ValidStepKey<stepNumbers>>\n >(this.value, stepData);\n\n return {\n createComponent: this.createStepSpecificComponentFactory(\n stepData,\n {\n isStepSpecific: true,\n defaultId: id,\n form: form as never,\n },\n ctx\n ),\n };\n });\n }\n\n private createFormComponent(\n form: Omit<\n MultiStepFormSchemaConfig.FormConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >,\n 'alias'\n >,\n defaultId: string\n ) {\n const { render, enabledForSteps = 'all', id = defaultId } = form;\n\n const ctx = {\n id,\n steps: createCtx(this.value, enabledForSteps as never),\n };\n\n return (props: formProps) => render(ctx, props);\n }\n\n private createStepSpecificComponentImpl<\n chosenStep extends HelperFnChosenSteps<resolvedStep, stepNumbers>\n >(\n stepData: chosenStep,\n config: CreateComponentImplConfig.stepSpecificConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >,\n input: HelperFnInputBase<resolvedStep, stepNumbers, chosenStep>,\n extraInput = {}\n ) {\n return <props>(fn: Function) =>\n ((props: props) => {\n // Call hook functions from extraInput at the top level of the component\n // This ensures hooks are called in a valid React context (before any conditionals)\n const hookResults: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(extraInput)) {\n if (typeof value === 'function') {\n try {\n const result = value();\n // Verify the hook was actually called and returned a value\n // (hooks should always return something, even if it's undefined)\n hookResults[key] = result;\n\n // In development, we can add additional verification here\n // Log hook calls for debugging (can be disabled in production by removing console.debug)\n // if (typeof console !== 'undefined' && console.debug) {\n // console.debug(\n // `[multi-step-form] Hook \"${key}\" called successfully`,\n // { result: result === undefined ? 'defined' : 'undefined' }\n // );\n // }\n } catch (error) {\n // Provide helpful error message if hook throws\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n\n throw new Error(\n `[multi-step-form] Error calling hook \"${key}\" in useFormInstance.render: ${errorMessage}\\n\\n` +\n `This usually means:\\n` +\n `1. The hook is being called outside of a React component\\n` +\n `2. The hook has invalid dependencies or configuration\\n` +\n `3. There's an error in your hook implementation\\n\\n` +\n `Original error: ${errorMessage}`,\n { cause: error }\n );\n }\n } else {\n hookResults[key] = value;\n }\n }\n\n const { defaultId, form } = config;\n const { ctx } = input;\n\n // Safe cast here since the step specific `createComponent` will always have\n // `stepData` as a tuple\n const [step] =\n stepData as HelperFnChosenSteps.tupleNotation<`step${stepNumbers}`>;\n\n invariant(\n this.steps.isValidStepKey(step),\n `[createComponent]: the target step ${step} is invalid. Note, this error shouldn't appear as the target step should always be valid. If you see this error, please open an issue.`\n );\n\n const stepNumber = Number.parseInt(step.replace('step', ''));\n\n invariant(\n !Number.isNaN(stepNumber),\n `[${step}:\"createComponent\"]: an error occurred while extracting the number`\n );\n const current = this.value[step as keyof resolvedStep];\n\n // These checks are mostly for type safety. `current` should _always_ be in the proper format.\n // On the off chance that it's not, we have the checks here to help, but these checks are basically\n // just for type safety.\n invariant(\n 'fields' in current,\n `[${step}:createComponent]: unable to find the \"fields\" for the current step`\n );\n invariant(\n typeof current.fields === 'object',\n `[${step}:createComponent]: the \"fields\" property must be an object, was ${typeof current.fields}`\n );\n const stepUpdater = this.#internal.createStepUpdaterFn(step);\n\n const Field = field.create<\n resolvedStep,\n HelperFnChosenSteps.resolve<resolvedStep, stepNumbers, chosenStep>\n >((name) => {\n const currentFields = Object.keys(\n current.fields as Record<string, unknown>\n );\n\n invariant(\n typeof name === 'string',\n (formatter) =>\n `[${step}:Field]: the \"name\" prop must be a string and a valid field for ${step}. Available fields include: \"${formatter.format(\n currentFields\n )}\"`\n );\n // TODO add support for deep keys (`name`)\n\n invariant(\n name in (current.fields as object),\n (formatter) =>\n `[${step}:Field]: the field \"${name}\" doesn't exist for the current step. Available fields include: \"${formatter.format(\n currentFields\n )}\".`\n );\n\n invariant(\n 'update' in current,\n `[${step}:Field]: No \"update\" function was found`\n );\n\n const defaultValue = this.getValue(step as never, name as never);\n\n const { label, nameTransformCasing, type } = (\n current.fields as AnyStepField\n )[name];\n const targetFields = `fields.${name}.defaultValue`;\n\n return {\n defaultValue,\n label,\n nameTransformCasing,\n type,\n name,\n onInputChange: (value: unknown) => {\n // Handle Updater pattern: if value is a function, call it with the current field value\n let resolvedValue;\n\n if (typeof value === 'function') {\n const defaultValue = this.getValue(\n step as never,\n name as never\n );\n\n resolvedValue = value(defaultValue);\n } else {\n resolvedValue = value;\n }\n\n this.update({\n targetStep: step,\n updater: resolvedValue as never,\n fields: [targetFields] as never,\n });\n },\n reset: () =>\n this.reset({\n fields: [targetFields] as never,\n targetStep: step,\n }),\n } as never;\n });\n\n let fnInput = {\n ctx,\n onInputChange: stepUpdater,\n reset: this.#internal.createStepResetterFn(step),\n Field,\n ...hookResults,\n };\n\n if (form) {\n const {\n alias = MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS,\n ...rest\n } = form;\n const enabledFor = rest.enabledForSteps ?? 'all';\n\n if (\n MultiStepFormSchemaConfig.isFormAvailable(\n stepData as never,\n enabledFor as never\n )\n ) {\n fnInput = {\n ...fnInput,\n [alias]: this.createFormComponent(rest, defaultId),\n };\n }\n\n return fn(fnInput, props);\n }\n\n return fn(\n {\n ...fnInput,\n [MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS]:\n MultiStepFormSchemaConfig.createDefaultForm(defaultId),\n },\n props\n );\n }) as CreatedMultiStepFormComponent<props>;\n }\n\n private createStepSpecificComponentFactory<\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >\n >(\n stepData: chosenStep,\n config: CreateComponentImplConfig.stepSpecificConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >,\n ctx: HelperFnCtx<resolvedStep, stepNumbers, chosenStep>\n ): StepSpecificCreateComponentFn<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formAlias,\n formProps,\n formEnabledFor\n > {\n const createStepSpecificComponentImpl =\n this.createStepSpecificComponentImpl.bind(this);\n const createDefaultValues = this.createDefaultValues.bind(this);\n\n // Not exactly sure why `this.value` could be undefined, but it can be so\n // we fallback to the internal value\n const resolvedValues = this.value;\n const [targetStep] = stepData;\n const update = this.#internal\n .createStepUpdaterFn(targetStep)\n .bind(this.#internal) as never;\n const reset = this.#internal\n .createStepResetterFn(targetStep)\n .bind(this.#internal) as never;\n\n function impl<props = undefined>(\n fn: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor\n >\n ): CreatedMultiStepFormComponent<props>;\n function impl<\n formInstance,\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined,\n additionalCtx = {}\n >(\n options: StepSpecificComponent.options<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >,\n fn: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor,\n { [_ in formInstanceAlias]: formInstance }\n >\n ): CreatedMultiStepFormComponent<props>;\n function impl<\n formInstance,\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined,\n additionalCtx = {}\n >(\n optionsOrFn:\n | StepSpecificComponent.options<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >\n | CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor\n >,\n fn?: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor,\n { [_ in formInstanceAlias]: formInstance }\n >\n ) {\n function createResolvedCtx(additionalCtx: unknown) {\n return (\n additionalCtx ? { ctx: { ...ctx, ...additionalCtx } } : { ctx }\n ) as HelperFnInputBase<resolvedStep, stepNumbers, chosenStep>;\n }\n\n function createStepSpecificComponent() {\n invariant(\n typeof optionsOrFn === 'function',\n 'The first argument must be a function'\n );\n\n return createStepSpecificComponentImpl(\n stepData,\n config,\n createResolvedCtx(undefined)\n )(optionsOrFn);\n }\n\n if (typeof optionsOrFn === 'object') {\n const { useFormInstance, ctxData, debug } = optionsOrFn;\n const logger = new MultiStepFormLogger({\n debug,\n prefix(prefix) {\n return `${prefix}-${targetStep}-createComponent`;\n },\n });\n\n logger.info('First argument is an object');\n\n const { [targetStep]: _, ...values } = resolvedValues;\n\n invariant(\n typeof fn === 'function',\n 'The second argument must be a function'\n );\n const createdCtx = resolvedCtxCreator(logger, values);\n\n if (useFormInstance) {\n const {\n render,\n alias = StepSpecificComponent.DEFAULT_FORM_INSTANCE_ALIAS,\n } = useFormInstance;\n\n // Safe cast here since the step specific `createComponent` will always have\n // `stepData` as a tuple\n const [step] =\n stepData as HelperFnChosenSteps.tupleNotation<`step${stepNumbers}`>;\n\n // Store the render function and inputs to call it at component level\n // This ensures hooks are called in a valid React context\n const defaultValues = createDefaultValues(step) as never;\n const resolvedCtx = ctxData ? createdCtx({ ctx, ctxData }) : ctx;\n const renderInput = { ctx: resolvedCtx, defaultValues };\n\n return createStepSpecificComponentImpl(\n stepData,\n config,\n {\n ctx: resolvedCtx as never,\n update,\n reset,\n },\n {\n [alias]: () => render(renderInput as never),\n }\n )(fn);\n }\n\n if (ctxData) {\n const resolvedCtx = createdCtx({ ctx, ctxData });\n\n return createStepSpecificComponentImpl(stepData, config, {\n ctx: resolvedCtx as never,\n update,\n reset,\n })(fn);\n }\n\n // Empty options object. Can throw here 🤷♂️\n // Maybe add \"global\" - top level config - option to tune fine grained errors.\n return createStepSpecificComponent();\n }\n\n return createStepSpecificComponent();\n }\n\n return impl;\n }\n\n /**\n * A helper function to create a component for a specific step.\n * @param options The options for creating the step specific component.\n * @param fn A callback that is used for accessing the target step's data and defining\n * any props that the component should have. This function must return a valid `JSX` element.\n * @returns The created component for the step.\n */\n createComponent<\n chosenSteps extends HelperFnChosenSteps<resolvedStep, stepNumbers>,\n props = undefined\n >(\n options: CreateHelperFunctionOptionsBase<\n resolvedStep,\n stepNumbers,\n chosenSteps\n >,\n fn: CreateComponentCallback<resolvedStep, stepNumbers, chosenSteps, props>\n ) {\n const { stepData } = options;\n const ctx = createCtx<resolvedStep, stepNumbers, chosenSteps>(\n this.value,\n stepData\n ) as never;\n\n return ((props?: props) =>\n fn(ctx, props as any)) as CreatedMultiStepFormComponent<props>;\n }\n\n createDefaultValues<targetStep extends ValidStepKey<stepNumbers>>(\n targetStep: targetStep\n ) {\n return createDefaultValues(this.value, targetStep);\n }\n}\n"],"mappings":";;;;;;;;;sDAwO6C;;;;;;;;AA6P7C,SAAgB,oBAId,OAAqB,YAAwB;AAC7C,kDACE,cAAc,OACd,mBAAmB,WAAW,0BAC/B;CAED,MAAM,UAAU,MAAM;AAEtB,kDAAU,YAAY,SAAS,8BAA8B,aAAa;CAE1E,IAAI,gBAAgB,EAAE;AAEtB,MAAK,MAAM,CAAC,WAAW,gBAAgB,OAAO,QAC5C,QAAQ,OACT,CACC,iBAAgB;EACd,GAAG;GACF,YAAY,YAAY;EAC1B;AAGH,QAAO;;AAIT,IAAa,0BAAb,cAqBUA,0DAEV;CAEE;CACA,CAASC;CAOT,YACE,QAQA;EACA,MAAM,EAAE,MAAM,GAAG,SAAS;AAE1B,QAAM,KAAc;AAEpB,OAAK,0DAAmB,KAAK,SAAS;AAEtC,QAAKA,WAAY,IAAIC,6EAKnB;GACA,eAAe,KAAK;GACpB,gBAAgB,KAAK;GACrB,WAAW,SAAS,KAAK,iBAAiB,KAAc;GACzD,CAAC;AAGF,OAAK,MAAM;AACX,OAAK,QAAQ,MAAKD,SAAU,aAAa,KAAK,QAAQ,SAAS;GAC7D,MAAM,MAAM,OAAO;GACnB,MAAM,WAAW,CAAC,IAAI;GAGtB,MAAM,KAAK,MAAM,MAAM;GACvB,MAAM,uDAIJ,KAAK,OAAO,SAAS;AAEvB,UAAO,EACL,iBAAiB,KAAK,mCACpB,UACA;IACE,gBAAgB;IAChB,WAAW;IACL;IACP,EACD,IACD,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;;;;CAUT,gBAIE,SAKA,IACA;EACA,MAAM,EAAE,aAAa;EACrB,MAAM,uDACJ,KAAK,OACL,SACD;AAED,WAAS,UACP,GAAG,KAAK,MAAa;;CAGzB,oBACE,YACA;AACA,SAAO,oBAAoB,KAAK,OAAO,WAAW"}
|
|
1
|
+
{"version":3,"file":"step-schema.cjs","names":["MultiStepFormStepSchemaBase","#internal","MultiStepFormStepSchemaInternal","resolvedCtxCreator","MultiStepFormLogger","getValidatedCustomInputHooks","field","createUseSelector","selector","MultiStepFormSchemaConfig","createDefaultValues"],"sources":["../src/step-schema.ts"],"sourcesContent":["import {\n type AnyStepField,\n type CasingType,\n type Constrain,\n createCtx,\n type CreateHelperFunctionOptionsBase,\n createStep,\n type DefaultCasing,\n type DefaultStorageKey,\n type Expand,\n type GetCurrentStep,\n type GetFieldsForStep,\n type HelperFnChosenSteps,\n type HelperFnCtx,\n type HelperFnInputBase,\n invariant,\n MultiStepFormLogger,\n type MultiStepFormSchemaStepConfig as MultiStepFormSchemaStepBaseConfig,\n MultiStepFormStepSchema as MultiStepFormStepSchemaBase,\n type Relaxed,\n type ResetFn,\n type ResolvedStep as ResolvedCoreStep,\n type Step,\n type StepNumbers,\n type StrippedResolvedStep,\n type UpdateFn,\n type Updater,\n type ValidStepKey,\n} from '@jfdevelops/multi-step-form-core';\nimport { MultiStepFormStepSchemaInternal } from '@jfdevelops/multi-step-form-core/_internals';\nimport { type ComponentPropsWithRef, type ReactNode } from 'react';\nimport { field } from './field';\nimport { MultiStepFormSchemaConfig } from './form-config';\nimport { createUseSelector, type UseSelector } from './hooks/use-selector';\nimport { selector } from './selector';\nimport { getValidatedCustomInputHooks, resolvedCtxCreator } from './utils';\n\nexport interface MultiStepFormSchemaStepConfig<\n TStep extends Step<TCasing>,\n TCasing extends CasingType,\n TStorageKey extends string,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object,\n TResolvedStep extends ResolvedStep<TStep, TCasing> = ResolvedStep<\n TStep,\n TCasing\n >\n> extends MultiStepFormSchemaStepBaseConfig<TStep, TCasing, TStorageKey>,\n MultiStepFormSchemaConfig.Form<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n > {}\n\nexport type CreateFunction<TArgs extends any[], TReturn = void> = (\n ...args: TArgs\n) => TReturn;\nexport type CreateComponent<TInput, TProps> = CreateFunction<\n [input: TInput, props: TProps],\n ReactNode\n>;\n\nexport type CreateComponentCallback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps\n> = CreateComponent<\n HelperFnInputBase<TResolvedStep, TSteps, TChosenSteps>,\n TProps\n>;\nexport type CreatedMultiStepFormComponent<TProps> = TProps extends undefined\n ? () => ReactNode\n : (props: TProps) => ReactNode;\nexport type CreateComponentFn<\n TResolvedStep extends AnyResolvedStep,\n // This is needed to make TS happy with all types\n TStepNumbers extends StepNumbers<TResolvedStep>\n> = <\n chosenSteps extends HelperFnChosenSteps<TResolvedStep, TStepNumbers>,\n props = undefined\n>(\n options: CreateHelperFunctionOptionsBase<\n TResolvedStep,\n TStepNumbers,\n chosenSteps\n >,\n fn: CreateComponentCallback<TResolvedStep, TStepNumbers, chosenSteps, props>\n) => CreatedMultiStepFormComponent<props>;\n\nexport namespace StepSpecificComponent {\n // The logic for getting the formCtx only works for step specific `createComponent`\n // (i.e: step1.createComponent(...)) as of now. Reason is because I can't think of a good API for integrating the form\n // ctx into the main `createComponent` since multiple steps can be chosen. In that case\n // how would the logic work for when the form component should be defined in the callback?\n // Ideas:\n // - Make the main `createComponent` return a function that accepts the current step\n export type formComponent<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>\n > = TFormEnabledFor extends MultiStepFormSchemaConfig.defaultEnabledFor\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : TFormEnabledFor extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TFormEnabledFor[number] extends keyof TResolvedStep\n ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TChosenSteps[number] extends keyof TResolvedStep\n ? TChosenSteps[number] extends TFormEnabledFor[number]\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : {}\n : {}\n : {}\n : {}\n : keyof TFormEnabledFor extends keyof TResolvedStep\n ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<TSteps>\n >\n ? TChosenSteps[number] extends keyof TResolvedStep\n ? TChosenSteps[number] extends keyof TFormEnabledFor\n ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps>\n : {}\n : {}\n : {}\n : {};\n export type onInputChange<\n TResolvedStep extends AnyResolvedStep,\n TStepNumbers extends StepNumbers<TResolvedStep>,\n TTargetStep extends TStepNumbers\n > = <\n CurrentStepData extends GetCurrentStep<TResolvedStep, TTargetStep>,\n Field extends keyof CurrentStepData\n >(\n field: Field,\n updater: Updater<CurrentStepData[Field], Relaxed<CurrentStepData[Field]>>\n ) => void;\n export type updateWrappers<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TStepNumber extends HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n > = TStepNumber extends TSteps\n ? {\n /**\n * A useful wrapper around `update` to update the specific field.\n */\n onInputChange: UpdateFn.stepSpecific<\n TResolvedStep,\n TSteps,\n ValidStepKey<TStepNumber>\n >;\n /**\n * A useful wrapper for `update` to reset a specific field's value to its\n * original config value.\n * @resetFn\n */\n reset: ResetFn.stepSpecific<\n TResolvedStep,\n TSteps,\n ValidStepKey<TStepNumber>\n >;\n }\n : {};\n export type input<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TAdditionalCtx extends Record<string, unknown>,\n TStepNumber extends HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n > = HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n > = HelperFnInputBase<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n never,\n TAdditionalCtx\n > &\n updateWrappers<TResolvedStep, TSteps, TChosenSteps, TStepNumber> & {\n Field: field.component<\n TResolvedStep,\n ValidStepKey<\n HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TChosenSteps\n >\n >\n >;\n /**\n * A hook for reactively selecting a value from the form context.\n * The selector function receives the contextual data for the currently rendered step, and returns any derived value.\n * `useSelector` will automatically provide the latest context data on updates, and will subscribe the caller for automatic re-renders when the underlying data changes.\n *\n * @param selector - A function that receives the current step's context and returns the selected value\n * @returns The derived value, which will re-render the component on change\n *\n * @example\n * const someValue = useSelector(ctx => ctx.fields.username.value);\n */\n useSelector: UseSelector<TResolvedStep, TSteps, TChosenSteps>;\n /**\n * A component for reactively displaying a value from the form context.\n * Unlike `useSelector`, this component only re-renders itself, not the parent component.\n * Use this when you want to display a reactive value without causing parent re-renders.\n *\n * @param selector - A function that receives the current step's context and returns the selected value\n * @param children - Optional render prop that receives the selected value\n *\n * @example\n * <Selector selector={(ctx) => ctx.step1.fields.firstName.defaultValue}>\n * {(value) => <p>First name: {value}</p>}\n * </Selector>\n */\n Selector: selector.component<TResolvedStep, TSteps, TChosenSteps>;\n };\n\n export type callback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TAdditionalInput extends object,\n TAdditionalCtx extends Record<string, unknown>\n > = CreateComponent<\n Expand<\n input<TResolvedStep, TSteps, TChosenSteps, TAdditionalCtx> &\n formComponent<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n > &\n TAdditionalInput\n >,\n TProps\n >;\n export const DEFAULT_FORM_INSTANCE_ALIAS = 'form';\n export type defaultFormInstanceAlias = typeof DEFAULT_FORM_INSTANCE_ALIAS;\n export type formInstanceOptions<\n TAlias extends string,\n TRenderInput,\n TReturn\n > = {\n /**\n * The name of the return value of the `render` method.\n */\n alias?: TAlias;\n /**\n * A function that renders/creates the form instance. This function will be called\n * at the top level of the component, ensuring hooks are called in a valid React context.\n *\n * @param input - The input object containing context and default values\n * @returns The form instance (typically from a hook like `useForm`)\n *\n * @example\n * ```tsx\n * useFormInstance: {\n * render({ defaultValues }) {\n * return useForm({\n * defaultValues,\n * });\n * },\n * }\n * ```\n *\n * **Verification**: The hook call is automatically verified:\n * - Errors are caught and reported with helpful messages\n * - In development, hook calls are logged to console.debug\n * - The hook must be called at the component top level (enforced by the framework)\n */\n render: CreateFunction<[input: TRenderInput], TReturn>;\n };\n\n export interface CtxSelector<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TCtx\n > {\n /**\n * A function to select the data that will be available in the `fn`'s ctx.\n * @param input The available input to create the context with.\n * @returns The created ctx.\n */\n ctxData?: (\n input: HelperFnInputBase<\n TResolvedStep,\n TSteps,\n 'all',\n HelperFnChosenSteps.resolve<TResolvedStep, TSteps, TTargetStep>\n >\n ) => TCtx;\n }\n\n export type options<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormInstanceAlias extends string,\n TFormInstance,\n TCtx\n > = CtxSelector<TResolvedStep, TSteps, TTargetStep, TCtx> & {\n /**\n * If set to `true`, you'll be able to open the {@linkcode console} to view logs.\n */\n debug?: boolean;\n useFormInstance?: formInstanceOptions<\n TFormInstanceAlias,\n Pick<HelperFnInputBase<TResolvedStep, TSteps, TTargetStep>, 'ctx'> & {\n /**\n * An object containing all the default values for the current step.\n */\n defaultValues: Expand<\n ExtractedDefaultValues<TResolvedStep, TSteps, TTargetStep>\n >;\n },\n TFormInstance\n >;\n };\n}\n\nexport type CreateStepSpecificComponentCallback<\n TResolvedStep extends StrippedResolvedStep<AnyResolvedStep>,\n TSteps extends StepNumbers<TResolvedStep>,\n TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TProps,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TAdditionalInput extends object = {},\n TAdditionalCtx extends Record<string, unknown> = {}\n> = StepSpecificComponent.callback<\n TResolvedStep,\n TSteps,\n TChosenSteps,\n TProps,\n TFormAlias,\n TFormProps,\n TFormEnabledFor,\n TAdditionalInput,\n TAdditionalCtx\n>;\n\nexport type ExtractedDefaultValues<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TExtractedStepNumber extends number = HelperFnChosenSteps.extractStepNumber<\n TResolvedStep,\n TSteps,\n TTargetStep\n >,\n TFields extends GetFieldsForStep<\n TResolvedStep,\n ValidStepKey<TExtractedStepNumber>\n > = GetFieldsForStep<TResolvedStep, ValidStepKey<TExtractedStepNumber>>\n> = { [field in keyof TFields]: TFields[field]['defaultValue'] };\nexport interface StepSpecificCreateComponentFn<\n TResolvedStep extends AnyResolvedStep,\n TSteps extends StepNumbers<TResolvedStep>,\n TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>,\n TFormAlias extends string,\n TFormProps extends object,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>\n> {\n /**\n * A utility function to easily create a component for the current step.\n * @param fn The callback function where the component is defined.\n */\n <props = undefined>(\n fn: CreateStepSpecificComponentCallback<\n TResolvedStep,\n TSteps,\n TTargetStep,\n props,\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n >\n ): CreatedMultiStepFormComponent<props>;\n /**\n * A utility function to easily create a component for the current step.\n * @param options Specific config options for creating a component for the current step.\n * @param fn The callback function where the component is defined.\n * @returns The created component.\n */\n <\n formInstance,\n additionalCtx extends Record<string, unknown> = {},\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined\n >(\n options: StepSpecificComponent.options<\n TResolvedStep,\n TSteps,\n TTargetStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >,\n fn: CreateStepSpecificComponentCallback<\n TResolvedStep,\n TSteps,\n TTargetStep,\n props,\n TFormAlias,\n TFormProps,\n TFormEnabledFor,\n { [_ in formInstanceAlias]: formInstance },\n additionalCtx\n >\n ): CreatedMultiStepFormComponent<props>;\n}\n\nexport type ResolvedStep<\n TStep extends Step<TDefaultCasing>,\n TDefaultCasing extends CasingType = DefaultCasing,\n TResolvedStep extends ResolvedCoreStep<\n TStep,\n TDefaultCasing\n > = ResolvedCoreStep<TStep, TDefaultCasing>,\n TFormAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor,\n TFormProps extends object = ComponentPropsWithRef<'form'>\n> = {\n [stepKey in keyof TResolvedStep]: TResolvedStep[stepKey] &\n (stepKey extends ValidStepKey<StepNumbers<TResolvedStep>>\n ? {\n createComponent: StepSpecificCreateComponentFn<\n TResolvedStep,\n StepNumbers<TResolvedStep>,\n [stepKey],\n TFormAlias,\n TFormProps,\n TFormEnabledFor\n >;\n }\n : {});\n};\n\nexport type AnyResolvedStep = ResolvedStep<any, any, any>;\nexport interface HelperFunctions<\n TResolvedStep extends AnyResolvedStep,\n TStepNumbers extends StepNumbers<TResolvedStep>\n> {\n createComponent: CreateComponentFn<TResolvedStep, TStepNumbers>;\n}\nnamespace CreateComponentImplConfig {\n export type stepSpecificConfig<\n TResolvedStep extends AnyResolvedStep,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object\n > = {\n isStepSpecific: true;\n defaultId: string;\n form?: MultiStepFormSchemaConfig.FormConfig<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n >;\n };\n\n export type nonStepSpecific = {\n isStepSpecific: false;\n };\n\n export type config<\n TResolvedStep extends AnyResolvedStep,\n TFormAlias extends string,\n TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>,\n TFormProps extends object\n > =\n | nonStepSpecific\n | stepSpecificConfig<\n TResolvedStep,\n TFormAlias,\n TFormEnabledFor,\n TFormProps\n >;\n}\n\n/**\n * Creates a default values object for the target step.\n * @param steps The steps schema.\n * @param targetStep The step to create the default values for.\n * @returns An object containing the field names and their default values.\n */\nexport function createDefaultValues<\n resolvedStep extends AnyResolvedStep,\n stepNumbers extends StepNumbers<resolvedStep>,\n targetStep extends ValidStepKey<stepNumbers>\n>(steps: resolvedStep, targetStep: targetStep) {\n invariant(\n targetStep in steps,\n `The target step ${targetStep} is not a valid step key`\n );\n\n const current = steps[targetStep as unknown as keyof resolvedStep];\n\n invariant('fields' in current, `No \"fields\" were found for ${targetStep}`);\n\n let defaultValues = {};\n\n for (const [fieldName, fieldValues] of Object.entries(\n current.fields as Record<string, Record<string, unknown>>\n )) {\n defaultValues = {\n ...defaultValues,\n [fieldName]: fieldValues.defaultValue,\n };\n }\n\n return defaultValues as Expand<\n ExtractedDefaultValues<resolvedStep, stepNumbers, [targetStep]>\n >;\n}\nexport class MultiStepFormStepSchema<\n step extends Step<casing>,\n casing extends CasingType = DefaultCasing,\n storageKey extends string = DefaultStorageKey,\n formAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias,\n formEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<resolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor,\n formProps extends object = ComponentPropsWithRef<'form'>,\n core extends ResolvedCoreStep<step, casing> = ResolvedCoreStep<\n step,\n casing\n >,\n resolvedStep extends ResolvedStep<\n step,\n casing,\n core,\n formAlias,\n formEnabledFor,\n formProps\n > = ResolvedStep<step, casing, core, formAlias, formEnabledFor, formProps>,\n stepNumbers extends StepNumbers<resolvedStep> = StepNumbers<resolvedStep>\n >\n extends MultiStepFormStepSchemaBase<step, casing>\n implements HelperFunctions<resolvedStep, stepNumbers>\n{\n // @ts-ignore type doesn't match `MultiStepFormSchemaBase.value`\n value: resolvedStep;\n readonly #internal: MultiStepFormStepSchemaInternal<\n step,\n casing,\n resolvedStep,\n stepNumbers\n >;\n\n constructor(\n config: MultiStepFormSchemaStepConfig<\n step,\n Constrain<casing, CasingType>,\n storageKey,\n formAlias,\n formEnabledFor,\n formProps\n >\n ) {\n const { form, ...rest } = config;\n\n super(rest as never);\n\n this.value = createStep(this.original);\n\n this.#internal = new MultiStepFormStepSchemaInternal<\n step,\n casing,\n resolvedStep,\n stepNumbers\n >({\n originalValue: this.original,\n getValue: () => this.value,\n setValue: (next) => this.handlePostUpdate(next as never),\n });\n\n // this.value = this.#internal.enrichValues(resolvedValues);\n this.sync();\n this.value = this.#internal.enrichValues(this.value, (step) => {\n const key = `step${step as stepNumbers}`;\n const stepData = [key] as HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >;\n const id = form?.id ?? key;\n\n return {\n createComponent: this.createStepSpecificComponentFactory(stepData, {\n isStepSpecific: true,\n defaultId: id,\n form: form as never,\n }),\n };\n });\n }\n\n private createFormComponent(\n form: Omit<\n MultiStepFormSchemaConfig.FormConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >,\n 'alias'\n >,\n defaultId: string\n ) {\n const { render, enabledForSteps = 'all', id = defaultId } = form;\n\n const ctx = {\n id,\n steps: createCtx(this.value, enabledForSteps as never),\n };\n\n return (props: formProps) => render(ctx, props);\n }\n\n private createResolvedCtx<\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >,\n additionalCtx\n >(\n options: {\n stepData: chosenStep;\n logger: MultiStepFormLogger;\n } & StepSpecificComponent.CtxSelector<\n resolvedStep,\n stepNumbers,\n chosenStep,\n additionalCtx\n >\n ) {\n const { logger, stepData, ctxData } = options;\n // Create ctx fresh each time to ensure it has the latest this.value\n const ctx = createCtx<resolvedStep, stepNumbers, chosenStep>(\n this.value,\n stepData\n );\n let resolvedCtx = ctx as Expand<\n HelperFnCtx<resolvedStep, stepNumbers, chosenStep>\n >;\n\n if (ctxData) {\n const [targetStep] = stepData;\n const { [targetStep]: _, ...values } = this.value;\n const createResolvedCtx = resolvedCtxCreator(logger, values);\n\n resolvedCtx = createResolvedCtx({ ctx: resolvedCtx, ctxData });\n }\n\n return resolvedCtx;\n }\n\n private createStepSpecificComponentImpl<\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >,\n additionalCtx = {}\n >(\n stepData: chosenStep,\n config: CreateComponentImplConfig.stepSpecificConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >,\n extraConfig?: {\n logger?: MultiStepFormLogger;\n input?: (\n ctx: Expand<HelperFnCtx<resolvedStep, stepNumbers, chosenStep>>\n ) => Record<string, unknown>;\n } & StepSpecificComponent.CtxSelector<\n resolvedStep,\n stepNumbers,\n chosenStep,\n additionalCtx\n >\n ) {\n return <props>(fn: Function) =>\n ((props: props) => {\n const ctxData = extraConfig?.ctxData;\n const logger = extraConfig?.logger ?? new MultiStepFormLogger();\n const resolvedCtx = this.createResolvedCtx({\n stepData,\n ctxData,\n logger,\n });\n const extraInput = extraConfig?.input?.(resolvedCtx) ?? {};\n // Call hook functions from extraInput at the top level of the component\n // This ensures hooks are called in a valid React context (before any conditionals)\n const hookResults = getValidatedCustomInputHooks(extraInput);\n\n const { defaultId, form } = config;\n\n // Safe cast here since the step specific `createComponent` will always have\n // `stepData` as a tuple\n const [step] =\n stepData as HelperFnChosenSteps.tupleNotation<`step${stepNumbers}`>;\n\n invariant(\n this.steps.isValidStepKey(step),\n `[createComponent]: the target step ${step} is invalid. Note, this error shouldn't appear as the target step should always be valid. If you see this error, please open an issue.`\n );\n\n const stepNumber = Number.parseInt(step.replace('step', ''));\n\n invariant(\n !Number.isNaN(stepNumber),\n `[${step}:\"createComponent\"]: an error occurred while extracting the number`\n );\n const current = this.value[step as keyof resolvedStep];\n\n // These checks are mostly for type safety. `current` should _always_ be in the proper format.\n // On the off chance that it's not, we have the checks here to help, but these checks are basically\n // just for type safety.\n invariant(\n 'fields' in current,\n `[${step}:createComponent]: unable to find the \"fields\" for the current step`\n );\n invariant(\n typeof current.fields === 'object',\n `[${step}:createComponent]: the \"fields\" property must be an object, was ${typeof current.fields}`\n );\n\n // Memoize Field component to prevent remounting on every render\n // This ensures input focus is maintained when ctx changes\n const Field = field.create<\n resolvedStep,\n HelperFnChosenSteps.resolve<resolvedStep, stepNumbers, chosenStep>\n >(\n (name) => {\n // Access current step data directly to avoid stale closure\n const currentStep = this.value[\n step as keyof resolvedStep\n ] as typeof current;\n const currentFields = Object.keys(\n currentStep.fields as Record<string, unknown>\n );\n\n invariant(\n typeof name === 'string',\n (formatter) =>\n `[${step}:Field]: the \"name\" prop must be a string and a valid field for ${step}. Available fields include: \"${formatter.format(\n currentFields\n )}\"`\n );\n // TODO add support for deep keys (`name`)\n\n invariant(\n name in (currentStep.fields as object),\n (formatter) =>\n `[${step}:Field]: the field \"${name}\" doesn't exist for the current step. Available fields include: \"${formatter.format(\n currentFields\n )}\".`\n );\n\n invariant(\n 'update' in currentStep,\n `[${step}:Field]: No \"update\" function was found`\n );\n\n const defaultValue = this.getValue(step as never, name as never);\n\n const { label, nameTransformCasing, type } = (\n currentStep.fields as AnyStepField\n )[name];\n const targetFields = `fields.${name}.defaultValue`;\n\n return {\n defaultValue,\n label,\n nameTransformCasing,\n type,\n name,\n onInputChange: (value: unknown) => {\n // Handle Updater pattern: if value is a function, call it with the current field value\n let resolvedValue;\n\n if (typeof value === 'function') {\n const defaultValue = this.getValue(\n step as never,\n name as never\n );\n\n resolvedValue = value(defaultValue);\n } else {\n resolvedValue = value;\n }\n\n this.update({\n targetStep: step,\n updater: resolvedValue as never,\n fields: [targetFields] as never,\n });\n },\n reset: () =>\n this.reset({\n fields: [targetFields] as never,\n targetStep: step,\n }),\n } as never;\n },\n this.subscribe,\n (name) => this.getValue(step as never, name as never)\n );\n\n // Create useSelector hook for reactive value access via selector\n // This allows getting values from ctx reactively without causing re-renders\n // Pass a function that creates fresh ctx on each call to avoid stale closures\n const useSelector = createUseSelector(\n () => this.createResolvedCtx({ stepData, ctxData, logger }),\n this.subscribe\n );\n\n // Create Selector component that uses useSelector internally\n // This allows parts of the UI to subscribe to specific values without\n // causing the parent component to re-render\n const Selector = selector.create(\n () => this.createResolvedCtx({ stepData, ctxData, logger }),\n this.subscribe\n );\n\n let fnInput = {\n ctx: resolvedCtx,\n onInputChange: this.#internal.createStepUpdaterFn(step),\n reset: this.#internal.createStepResetterFn(step),\n Field,\n useSelector,\n Selector,\n ...hookResults,\n };\n\n if (form) {\n const {\n alias = MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS,\n ...rest\n } = form;\n const enabledFor = rest.enabledForSteps ?? 'all';\n\n if (\n MultiStepFormSchemaConfig.isFormAvailable(\n stepData as never,\n enabledFor as never\n )\n ) {\n fnInput = {\n ...fnInput,\n [alias]: this.createFormComponent(rest, defaultId),\n };\n }\n\n return fn(fnInput, props);\n }\n\n return fn(\n {\n ...fnInput,\n [MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS]:\n MultiStepFormSchemaConfig.createDefaultForm(defaultId),\n },\n props\n );\n }) as CreatedMultiStepFormComponent<props>;\n }\n\n private createStepSpecificComponentFactory<\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >\n >(\n stepData: chosenStep,\n config: CreateComponentImplConfig.stepSpecificConfig<\n resolvedStep,\n formAlias,\n formEnabledFor,\n formProps\n >\n // ctx: HelperFnCtx<resolvedStep, stepNumbers, chosenStep>\n ): StepSpecificCreateComponentFn<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formAlias,\n formProps,\n formEnabledFor\n > {\n const createStepSpecificComponentImpl =\n this.createStepSpecificComponentImpl.bind(this);\n const createDefaultValues = this.createDefaultValues.bind(this);\n const [targetStep] = stepData;\n\n function impl<props = undefined>(\n fn: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor\n >\n ): CreatedMultiStepFormComponent<props>;\n function impl<\n formInstance,\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined,\n additionalCtx = {}\n >(\n options: StepSpecificComponent.options<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >,\n fn: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor,\n { [_ in formInstanceAlias]: formInstance }\n >\n ): CreatedMultiStepFormComponent<props>;\n function impl<\n formInstance,\n formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias,\n props = undefined,\n additionalCtx = {}\n >(\n optionsOrFn:\n | StepSpecificComponent.options<\n resolvedStep,\n stepNumbers,\n chosenStep,\n formInstanceAlias,\n formInstance,\n additionalCtx\n >\n | CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor\n >,\n fn?: CreateStepSpecificComponentCallback<\n resolvedStep,\n stepNumbers,\n chosenStep,\n props,\n formAlias,\n formProps,\n formEnabledFor,\n { [_ in formInstanceAlias]: formInstance }\n >\n ) {\n function createStepSpecificComponent() {\n invariant(\n typeof optionsOrFn === 'function',\n 'The first argument must be a function'\n );\n\n return createStepSpecificComponentImpl(stepData, config)(optionsOrFn);\n }\n\n if (typeof optionsOrFn === 'object') {\n const { useFormInstance, ctxData, debug } = optionsOrFn;\n const logger = new MultiStepFormLogger({\n debug,\n prefix(prefix) {\n return `${prefix}-${targetStep}-createComponent`;\n },\n });\n\n logger.info('First argument is an object');\n\n invariant(\n typeof fn === 'function',\n 'The second argument must be a function'\n );\n\n if (useFormInstance) {\n const {\n render,\n alias = StepSpecificComponent.DEFAULT_FORM_INSTANCE_ALIAS,\n } = useFormInstance;\n\n // Safe cast here since the step specific `createComponent` will always have\n // `stepData` as a tuple\n const [step] =\n stepData as HelperFnChosenSteps.tupleNotation<`step${stepNumbers}`>;\n\n return createStepSpecificComponentImpl(stepData, config, {\n logger,\n ctxData,\n input: (ctx) => {\n const defaultValues = createDefaultValues(step) as never;\n\n return {\n [alias]: () =>\n render({\n ctx,\n defaultValues,\n }),\n };\n },\n })(fn);\n }\n\n if (ctxData) {\n return createStepSpecificComponentImpl(stepData, config, {\n logger,\n ctxData,\n })(fn);\n }\n\n // Empty options object. Can throw here 🤷♂️\n // Maybe add \"global\" - top level config - option to tune fine grained errors.\n return createStepSpecificComponent();\n }\n\n return createStepSpecificComponent();\n }\n\n return impl;\n }\n\n /**\n * A helper function to create a component for a specific step.\n * @param options The options for creating the step specific component.\n * @param fn A callback that is used for accessing the target step's data and defining\n * any props that the component should have. This function must return a valid `JSX` element.\n * @returns The created component for the step.\n */\n createComponent<\n chosenSteps extends HelperFnChosenSteps<resolvedStep, stepNumbers>,\n props = undefined\n >(\n options: CreateHelperFunctionOptionsBase<\n resolvedStep,\n stepNumbers,\n chosenSteps\n >,\n fn: CreateComponentCallback<resolvedStep, stepNumbers, chosenSteps, props>\n ) {\n const { stepData } = options;\n const ctx = createCtx<resolvedStep, stepNumbers, chosenSteps>(\n this.value,\n stepData\n ) as never;\n\n return ((props?: props) =>\n fn(ctx, props as any)) as CreatedMultiStepFormComponent<props>;\n }\n\n createDefaultValues<targetStep extends ValidStepKey<stepNumbers>>(\n targetStep: targetStep\n ) {\n return createDefaultValues(this.value, targetStep);\n }\n}\n"],"mappings":";;;;;;;;;;;sDAoQ6C;;;;;;;;AA6P7C,SAAgB,oBAId,OAAqB,YAAwB;AAC7C,kDACE,cAAc,OACd,mBAAmB,WAAW,0BAC/B;CAED,MAAM,UAAU,MAAM;AAEtB,kDAAU,YAAY,SAAS,8BAA8B,aAAa;CAE1E,IAAI,gBAAgB,EAAE;AAEtB,MAAK,MAAM,CAAC,WAAW,gBAAgB,OAAO,QAC5C,QAAQ,OACT,CACC,iBAAgB;EACd,GAAG;GACF,YAAY,YAAY;EAC1B;AAGH,QAAO;;AAIT,IAAa,0BAAb,cAqBUA,0DAEV;CAEE;CACA,CAASC;CAOT,YACE,QAQA;EACA,MAAM,EAAE,MAAM,GAAG,SAAS;AAE1B,QAAM,KAAc;AAEpB,OAAK,0DAAmB,KAAK,SAAS;AAEtC,QAAKA,WAAY,IAAIC,6EAKnB;GACA,eAAe,KAAK;GACpB,gBAAgB,KAAK;GACrB,WAAW,SAAS,KAAK,iBAAiB,KAAc;GACzD,CAAC;AAGF,OAAK,MAAM;AACX,OAAK,QAAQ,MAAKD,SAAU,aAAa,KAAK,QAAQ,SAAS;GAC7D,MAAM,MAAM,OAAO;GACnB,MAAM,WAAW,CAAC,IAAI;GAGtB,MAAM,KAAK,MAAM,MAAM;AAEvB,UAAO,EACL,iBAAiB,KAAK,mCAAmC,UAAU;IACjE,gBAAgB;IAChB,WAAW;IACL;IACP,CAAC,EACH;IACD;;CAGJ,AAAQ,oBACN,MASA,WACA;EACA,MAAM,EAAE,QAAQ,kB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|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { field } from "./field.cjs";
|
|
2
|
+
import { UseSelector } from "./hooks/use-selector.cjs";
|
|
3
|
+
import { selector } from "./selector.cjs";
|
|
2
4
|
import { MultiStepFormSchemaConfig } from "./form-config.cjs";
|
|
3
5
|
import * as _jfdevelops_multi_step_form_core0 from "@jfdevelops/multi-step-form-core";
|
|
4
6
|
import { CasingType, Constrain, CreateHelperFunctionOptionsBase, DefaultCasing, DefaultStorageKey, Expand, GetCurrentStep, GetFieldsForStep, HelperFnChosenSteps, HelperFnInputBase, MultiStepFormSchemaStepConfig, MultiStepFormStepSchema, Relaxed, ResetFn, ResolvedStep, Step, StepNumbers, StrippedResolvedStep, UpdateFn, Updater, ValidStepKey } from "@jfdevelops/multi-step-form-core";
|
|
@@ -28,6 +30,32 @@ declare namespace StepSpecificComponent {
|
|
|
28
30
|
} : {};
|
|
29
31
|
type input<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TAdditionalCtx extends Record<string, unknown>, TStepNumber extends HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps> = HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps>> = HelperFnInputBase<TResolvedStep, TSteps, TChosenSteps, never, TAdditionalCtx> & updateWrappers<TResolvedStep, TSteps, TChosenSteps, TStepNumber> & {
|
|
30
32
|
Field: field.component<TResolvedStep, ValidStepKey<HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps>>>;
|
|
33
|
+
/**
|
|
34
|
+
* A hook for reactively selecting a value from the form context.
|
|
35
|
+
* The selector function receives the contextual data for the currently rendered step, and returns any derived value.
|
|
36
|
+
* `useSelector` will automatically provide the latest context data on updates, and will subscribe the caller for automatic re-renders when the underlying data changes.
|
|
37
|
+
*
|
|
38
|
+
* @param selector - A function that receives the current step's context and returns the selected value
|
|
39
|
+
* @returns The derived value, which will re-render the component on change
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const someValue = useSelector(ctx => ctx.fields.username.value);
|
|
43
|
+
*/
|
|
44
|
+
useSelector: UseSelector<TResolvedStep, TSteps, TChosenSteps>;
|
|
45
|
+
/**
|
|
46
|
+
* A component for reactively displaying a value from the form context.
|
|
47
|
+
* Unlike `useSelector`, this component only re-renders itself, not the parent component.
|
|
48
|
+
* Use this when you want to display a reactive value without causing parent re-renders.
|
|
49
|
+
*
|
|
50
|
+
* @param selector - A function that receives the current step's context and returns the selected value
|
|
51
|
+
* @param children - Optional render prop that receives the selected value
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* <Selector selector={(ctx) => ctx.step1.fields.firstName.defaultValue}>
|
|
55
|
+
* {(value) => <p>First name: {value}</p>}
|
|
56
|
+
* </Selector>
|
|
57
|
+
*/
|
|
58
|
+
Selector: selector.component<TResolvedStep, TSteps, TChosenSteps>;
|
|
31
59
|
};
|
|
32
60
|
type callback<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TProps, TFormAlias extends string, TFormProps extends object, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>, TAdditionalInput extends object, TAdditionalCtx extends Record<string, unknown>> = CreateComponent<Expand<input<TResolvedStep, TSteps, TChosenSteps, TAdditionalCtx> & formComponent<TResolvedStep, TSteps, TChosenSteps, TFormAlias, TFormProps, TFormEnabledFor> & TAdditionalInput>, TProps>;
|
|
33
61
|
const DEFAULT_FORM_INSTANCE_ALIAS = "form";
|
|
@@ -75,7 +103,7 @@ declare namespace StepSpecificComponent {
|
|
|
75
103
|
* If set to `true`, you'll be able to open the {@linkcode console} to view logs.
|
|
76
104
|
*/
|
|
77
105
|
debug?: boolean;
|
|
78
|
-
useFormInstance?: formInstanceOptions<TFormInstanceAlias, HelperFnInputBase<TResolvedStep, TSteps, TTargetStep> & {
|
|
106
|
+
useFormInstance?: formInstanceOptions<TFormInstanceAlias, Pick<HelperFnInputBase<TResolvedStep, TSteps, TTargetStep>, 'ctx'> & {
|
|
79
107
|
/**
|
|
80
108
|
* An object containing all the default values for the current step.
|
|
81
109
|
*/
|
|
@@ -118,6 +146,7 @@ declare class MultiStepFormStepSchema$1<step extends Step<casing>, casing extend
|
|
|
118
146
|
value: resolvedStep;
|
|
119
147
|
constructor(config: MultiStepFormSchemaStepConfig$1<step, Constrain<casing, CasingType>, storageKey, formAlias, formEnabledFor, formProps>);
|
|
120
148
|
private createFormComponent;
|
|
149
|
+
private createResolvedCtx;
|
|
121
150
|
private createStepSpecificComponentImpl;
|
|
122
151
|
private createStepSpecificComponentFactory;
|
|
123
152
|
/**
|
package/dist/step-schema.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { field } from "./field.mjs";
|
|
2
|
+
import { UseSelector } from "./hooks/use-selector.mjs";
|
|
3
|
+
import { selector } from "./selector.mjs";
|
|
2
4
|
import { MultiStepFormSchemaConfig } from "./form-config.mjs";
|
|
3
5
|
import * as _jfdevelops_multi_step_form_core0 from "@jfdevelops/multi-step-form-core";
|
|
4
6
|
import { CasingType, Constrain, CreateHelperFunctionOptionsBase, DefaultCasing, DefaultStorageKey, Expand, GetCurrentStep, GetFieldsForStep, HelperFnChosenSteps, HelperFnInputBase, MultiStepFormSchemaStepConfig, MultiStepFormStepSchema, Relaxed, ResetFn, ResolvedStep, Step, StepNumbers, StrippedResolvedStep, UpdateFn, Updater, ValidStepKey } from "@jfdevelops/multi-step-form-core";
|
|
@@ -28,6 +30,32 @@ declare namespace StepSpecificComponent {
|
|
|
28
30
|
} : {};
|
|
29
31
|
type input<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TAdditionalCtx extends Record<string, unknown>, TStepNumber extends HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps> = HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps>> = HelperFnInputBase<TResolvedStep, TSteps, TChosenSteps, never, TAdditionalCtx> & updateWrappers<TResolvedStep, TSteps, TChosenSteps, TStepNumber> & {
|
|
30
32
|
Field: field.component<TResolvedStep, ValidStepKey<HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps>>>;
|
|
33
|
+
/**
|
|
34
|
+
* A hook for reactively selecting a value from the form context.
|
|
35
|
+
* The selector function receives the contextual data for the currently rendered step, and returns any derived value.
|
|
36
|
+
* `useSelector` will automatically provide the latest context data on updates, and will subscribe the caller for automatic re-renders when the underlying data changes.
|
|
37
|
+
*
|
|
38
|
+
* @param selector - A function that receives the current step's context and returns the selected value
|
|
39
|
+
* @returns The derived value, which will re-render the component on change
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const someValue = useSelector(ctx => ctx.fields.username.value);
|
|
43
|
+
*/
|
|
44
|
+
useSelector: UseSelector<TResolvedStep, TSteps, TChosenSteps>;
|
|
45
|
+
/**
|
|
46
|
+
* A component for reactively displaying a value from the form context.
|
|
47
|
+
* Unlike `useSelector`, this component only re-renders itself, not the parent component.
|
|
48
|
+
* Use this when you want to display a reactive value without causing parent re-renders.
|
|
49
|
+
*
|
|
50
|
+
* @param selector - A function that receives the current step's context and returns the selected value
|
|
51
|
+
* @param children - Optional render prop that receives the selected value
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* <Selector selector={(ctx) => ctx.step1.fields.firstName.defaultValue}>
|
|
55
|
+
* {(value) => <p>First name: {value}</p>}
|
|
56
|
+
* </Selector>
|
|
57
|
+
*/
|
|
58
|
+
Selector: selector.component<TResolvedStep, TSteps, TChosenSteps>;
|
|
31
59
|
};
|
|
32
60
|
type callback<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TProps, TFormAlias extends string, TFormProps extends object, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>, TAdditionalInput extends object, TAdditionalCtx extends Record<string, unknown>> = CreateComponent<Expand<input<TResolvedStep, TSteps, TChosenSteps, TAdditionalCtx> & formComponent<TResolvedStep, TSteps, TChosenSteps, TFormAlias, TFormProps, TFormEnabledFor> & TAdditionalInput>, TProps>;
|
|
33
61
|
const DEFAULT_FORM_INSTANCE_ALIAS = "form";
|
|
@@ -75,7 +103,7 @@ declare namespace StepSpecificComponent {
|
|
|
75
103
|
* If set to `true`, you'll be able to open the {@linkcode console} to view logs.
|
|
76
104
|
*/
|
|
77
105
|
debug?: boolean;
|
|
78
|
-
useFormInstance?: formInstanceOptions<TFormInstanceAlias, HelperFnInputBase<TResolvedStep, TSteps, TTargetStep> & {
|
|
106
|
+
useFormInstance?: formInstanceOptions<TFormInstanceAlias, Pick<HelperFnInputBase<TResolvedStep, TSteps, TTargetStep>, 'ctx'> & {
|
|
79
107
|
/**
|
|
80
108
|
* An object containing all the default values for the current step.
|
|
81
109
|
*/
|
|
@@ -118,6 +146,7 @@ declare class MultiStepFormStepSchema$1<step extends Step<casing>, casing extend
|
|
|
118
146
|
value: resolvedStep;
|
|
119
147
|
constructor(config: MultiStepFormSchemaStepConfig$1<step, Constrain<casing, CasingType>, storageKey, formAlias, formEnabledFor, formProps>);
|
|
120
148
|
private createFormComponent;
|
|
149
|
+
private createResolvedCtx;
|
|
121
150
|
private createStepSpecificComponentImpl;
|
|
122
151
|
private createStepSpecificComponentFactory;
|
|
123
152
|
/**
|
package/dist/step-schema.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { field } from "./field.mjs";
|
|
2
2
|
import { MultiStepFormSchemaConfig } from "./form-config.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { createUseSelector } from "./hooks/use-selector.mjs";
|
|
4
|
+
import { selector } from "./selector.mjs";
|
|
5
|
+
import { getValidatedCustomInputHooks, resolvedCtxCreator } from "./utils.mjs";
|
|
4
6
|
import { MultiStepFormLogger, MultiStepFormStepSchema, createCtx, createStep, invariant } from "@jfdevelops/multi-step-form-core";
|
|
5
7
|
import { MultiStepFormStepSchemaInternal } from "@jfdevelops/multi-step-form-core/_internals";
|
|
6
8
|
|
|
@@ -43,12 +45,11 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
43
45
|
const key = `step${step}`;
|
|
44
46
|
const stepData = [key];
|
|
45
47
|
const id = form?.id ?? key;
|
|
46
|
-
const ctx = createCtx(this.value, stepData);
|
|
47
48
|
return { createComponent: this.createStepSpecificComponentFactory(stepData, {
|
|
48
49
|
isStepSpecific: true,
|
|
49
50
|
defaultId: id,
|
|
50
51
|
form
|
|
51
|
-
}
|
|
52
|
+
}) };
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
createFormComponent(form, defaultId) {
|
|
@@ -59,18 +60,30 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
59
60
|
};
|
|
60
61
|
return (props) => render(ctx, props);
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
+
createResolvedCtx(options) {
|
|
64
|
+
const { logger, stepData, ctxData } = options;
|
|
65
|
+
let resolvedCtx = createCtx(this.value, stepData);
|
|
66
|
+
if (ctxData) {
|
|
67
|
+
const [targetStep] = stepData;
|
|
68
|
+
const { [targetStep]: _, ...values } = this.value;
|
|
69
|
+
resolvedCtx = resolvedCtxCreator(logger, values)({
|
|
70
|
+
ctx: resolvedCtx,
|
|
71
|
+
ctxData
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return resolvedCtx;
|
|
75
|
+
}
|
|
76
|
+
createStepSpecificComponentImpl(stepData, config, extraConfig) {
|
|
63
77
|
return (fn) => ((props) => {
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
78
|
+
const ctxData = extraConfig?.ctxData;
|
|
79
|
+
const logger = extraConfig?.logger ?? new MultiStepFormLogger();
|
|
80
|
+
const resolvedCtx = this.createResolvedCtx({
|
|
81
|
+
stepData,
|
|
82
|
+
ctxData,
|
|
83
|
+
logger
|
|
84
|
+
});
|
|
85
|
+
const hookResults = getValidatedCustomInputHooks(extraConfig?.input?.(resolvedCtx) ?? {});
|
|
72
86
|
const { defaultId, form } = config;
|
|
73
|
-
const { ctx } = input;
|
|
74
87
|
const [step] = stepData;
|
|
75
88
|
invariant(this.steps.isValidStepKey(step), `[createComponent]: the target step ${step} is invalid. Note, this error shouldn't appear as the target step should always be valid. If you see this error, please open an issue.`);
|
|
76
89
|
const stepNumber = Number.parseInt(step.replace("step", ""));
|
|
@@ -78,14 +91,14 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
78
91
|
const current = this.value[step];
|
|
79
92
|
invariant("fields" in current, `[${step}:createComponent]: unable to find the "fields" for the current step`);
|
|
80
93
|
invariant(typeof current.fields === "object", `[${step}:createComponent]: the "fields" property must be an object, was ${typeof current.fields}`);
|
|
81
|
-
const stepUpdater = this.#internal.createStepUpdaterFn(step);
|
|
82
94
|
const Field = field.create((name) => {
|
|
83
|
-
const
|
|
95
|
+
const currentStep = this.value[step];
|
|
96
|
+
const currentFields = Object.keys(currentStep.fields);
|
|
84
97
|
invariant(typeof name === "string", (formatter) => `[${step}:Field]: the "name" prop must be a string and a valid field for ${step}. Available fields include: "${formatter.format(currentFields)}"`);
|
|
85
|
-
invariant(name in
|
|
86
|
-
invariant("update" in
|
|
98
|
+
invariant(name in currentStep.fields, (formatter) => `[${step}:Field]: the field "${name}" doesn't exist for the current step. Available fields include: "${formatter.format(currentFields)}".`);
|
|
99
|
+
invariant("update" in currentStep, `[${step}:Field]: No "update" function was found`);
|
|
87
100
|
const defaultValue = this.getValue(step, name);
|
|
88
|
-
const { label, nameTransformCasing, type } =
|
|
101
|
+
const { label, nameTransformCasing, type } = currentStep.fields[name];
|
|
89
102
|
const targetFields = `fields.${name}.defaultValue`;
|
|
90
103
|
return {
|
|
91
104
|
defaultValue,
|
|
@@ -108,12 +121,24 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
108
121
|
targetStep: step
|
|
109
122
|
})
|
|
110
123
|
};
|
|
111
|
-
});
|
|
124
|
+
}, this.subscribe, (name) => this.getValue(step, name));
|
|
125
|
+
const useSelector = createUseSelector(() => this.createResolvedCtx({
|
|
126
|
+
stepData,
|
|
127
|
+
ctxData,
|
|
128
|
+
logger
|
|
129
|
+
}), this.subscribe);
|
|
130
|
+
const Selector = selector.create(() => this.createResolvedCtx({
|
|
131
|
+
stepData,
|
|
132
|
+
ctxData,
|
|
133
|
+
logger
|
|
134
|
+
}), this.subscribe);
|
|
112
135
|
let fnInput = {
|
|
113
|
-
ctx,
|
|
114
|
-
onInputChange:
|
|
136
|
+
ctx: resolvedCtx,
|
|
137
|
+
onInputChange: this.#internal.createStepUpdaterFn(step),
|
|
115
138
|
reset: this.#internal.createStepResetterFn(step),
|
|
116
139
|
Field,
|
|
140
|
+
useSelector,
|
|
141
|
+
Selector,
|
|
117
142
|
...hookResults
|
|
118
143
|
};
|
|
119
144
|
if (form) {
|
|
@@ -131,23 +156,14 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
131
156
|
}, props);
|
|
132
157
|
});
|
|
133
158
|
}
|
|
134
|
-
createStepSpecificComponentFactory(stepData, config
|
|
159
|
+
createStepSpecificComponentFactory(stepData, config) {
|
|
135
160
|
const createStepSpecificComponentImpl = this.createStepSpecificComponentImpl.bind(this);
|
|
136
161
|
const createDefaultValues$1 = this.createDefaultValues.bind(this);
|
|
137
|
-
const resolvedValues = this.value;
|
|
138
162
|
const [targetStep] = stepData;
|
|
139
|
-
const update = this.#internal.createStepUpdaterFn(targetStep).bind(this.#internal);
|
|
140
|
-
const reset = this.#internal.createStepResetterFn(targetStep).bind(this.#internal);
|
|
141
163
|
function impl(optionsOrFn, fn) {
|
|
142
|
-
function createResolvedCtx(additionalCtx) {
|
|
143
|
-
return additionalCtx ? { ctx: {
|
|
144
|
-
...ctx,
|
|
145
|
-
...additionalCtx
|
|
146
|
-
} } : { ctx };
|
|
147
|
-
}
|
|
148
164
|
function createStepSpecificComponent() {
|
|
149
165
|
invariant(typeof optionsOrFn === "function", "The first argument must be a function");
|
|
150
|
-
return createStepSpecificComponentImpl(stepData, config
|
|
166
|
+
return createStepSpecificComponentImpl(stepData, config)(optionsOrFn);
|
|
151
167
|
}
|
|
152
168
|
if (typeof optionsOrFn === "object") {
|
|
153
169
|
const { useFormInstance, ctxData, debug } = optionsOrFn;
|
|
@@ -158,34 +174,25 @@ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
|
|
|
158
174
|
}
|
|
159
175
|
});
|
|
160
176
|
logger.info("First argument is an object");
|
|
161
|
-
const { [targetStep]: _, ...values } = resolvedValues;
|
|
162
177
|
invariant(typeof fn === "function", "The second argument must be a function");
|
|
163
|
-
const createdCtx = resolvedCtxCreator(logger, values);
|
|
164
178
|
if (useFormInstance) {
|
|
165
179
|
const { render, alias = StepSpecificComponent.DEFAULT_FORM_INSTANCE_ALIAS } = useFormInstance;
|
|
166
180
|
const [step] = stepData;
|
|
167
|
-
const defaultValues = createDefaultValues$1(step);
|
|
168
|
-
const resolvedCtx = ctxData ? createdCtx({
|
|
169
|
-
ctx,
|
|
170
|
-
ctxData
|
|
171
|
-
}) : ctx;
|
|
172
|
-
const renderInput = {
|
|
173
|
-
ctx: resolvedCtx,
|
|
174
|
-
defaultValues
|
|
175
|
-
};
|
|
176
181
|
return createStepSpecificComponentImpl(stepData, config, {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
182
|
+
logger,
|
|
183
|
+
ctxData,
|
|
184
|
+
input: (ctx) => {
|
|
185
|
+
const defaultValues = createDefaultValues$1(step);
|
|
186
|
+
return { [alias]: () => render({
|
|
187
|
+
ctx,
|
|
188
|
+
defaultValues
|
|
189
|
+
}) };
|
|
190
|
+
}
|
|
191
|
+
})(fn);
|
|
181
192
|
}
|
|
182
193
|
if (ctxData) return createStepSpecificComponentImpl(stepData, config, {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
ctxData
|
|
186
|
-
}),
|
|
187
|
-
update,
|
|
188
|
-
reset
|
|
194
|
+
logger,
|
|
195
|
+
ctxData
|
|
189
196
|
})(fn);
|
|
190
197
|
return createStepSpecificComponent();
|
|
191
198
|
}
|