@jfdevelops/react-multi-step-form 1.0.0-alpha.22 → 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.
Files changed (57) hide show
  1. package/dist/create-context.cjs +92 -0
  2. package/dist/create-context.cjs.map +1 -0
  3. package/dist/create-context.d.cts +154 -0
  4. package/dist/create-context.d.mts +154 -0
  5. package/dist/create-context.mjs +92 -0
  6. package/dist/create-context.mjs.map +1 -0
  7. package/dist/field.cjs +30 -0
  8. package/dist/field.cjs.map +1 -0
  9. package/dist/field.d.cts +40 -0
  10. package/dist/field.d.mts +40 -0
  11. package/dist/field.mjs +25 -0
  12. package/dist/field.mjs.map +1 -0
  13. package/dist/form-config.cjs +32 -0
  14. package/dist/form-config.cjs.map +1 -0
  15. package/dist/form-config.d.cts +160 -0
  16. package/dist/form-config.d.mts +160 -0
  17. package/dist/form-config.mjs +27 -0
  18. package/dist/form-config.mjs.map +1 -0
  19. package/dist/hooks/use-multi-step-form-data.cjs +41 -0
  20. package/dist/hooks/use-multi-step-form-data.cjs.map +1 -0
  21. package/dist/hooks/use-multi-step-form-data.d.cts +28 -0
  22. package/dist/hooks/use-multi-step-form-data.d.mts +28 -0
  23. package/dist/hooks/use-multi-step-form-data.mjs +39 -0
  24. package/dist/hooks/use-multi-step-form-data.mjs.map +1 -0
  25. package/dist/hooks/use-selector.cjs +16 -0
  26. package/dist/hooks/use-selector.cjs.map +1 -0
  27. package/dist/hooks/use-selector.d.cts +9 -0
  28. package/dist/hooks/use-selector.d.mts +9 -0
  29. package/dist/hooks/use-selector.mjs +16 -0
  30. package/dist/hooks/use-selector.mjs.map +1 -0
  31. package/dist/index.cjs +17 -0
  32. package/dist/index.d.cts +5 -0
  33. package/dist/index.d.mts +5 -0
  34. package/dist/index.mjs +6 -0
  35. package/dist/schema.cjs +45 -0
  36. package/dist/schema.cjs.map +1 -0
  37. package/dist/schema.d.cts +34 -0
  38. package/dist/schema.d.mts +34 -0
  39. package/dist/schema.mjs +44 -0
  40. package/dist/schema.mjs.map +1 -0
  41. package/dist/selector.cjs +27 -0
  42. package/dist/selector.cjs.map +1 -0
  43. package/dist/selector.d.cts +30 -0
  44. package/dist/selector.d.mts +30 -0
  45. package/dist/selector.mjs +22 -0
  46. package/dist/selector.mjs.map +1 -0
  47. package/dist/step-schema.cjs +229 -0
  48. package/dist/step-schema.cjs.map +1 -0
  49. package/dist/step-schema.d.cts +164 -0
  50. package/dist/step-schema.d.mts +164 -0
  51. package/dist/step-schema.mjs +222 -0
  52. package/dist/step-schema.mjs.map +1 -0
  53. package/dist/utils.cjs +35 -0
  54. package/dist/utils.cjs.map +1 -0
  55. package/dist/utils.mjs +34 -0
  56. package/dist/utils.mjs.map +1 -0
  57. package/package.json +2 -2
@@ -0,0 +1,164 @@
1
+ import { field } from "./field.mjs";
2
+ import { UseSelector } from "./hooks/use-selector.mjs";
3
+ import { selector } from "./selector.mjs";
4
+ import { MultiStepFormSchemaConfig } from "./form-config.mjs";
5
+ import * as _jfdevelops_multi_step_form_core0 from "@jfdevelops/multi-step-form-core";
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";
7
+ import { ComponentPropsWithRef, ReactNode } from "react";
8
+
9
+ //#region src/step-schema.d.ts
10
+ interface MultiStepFormSchemaStepConfig$1<TStep extends Step<TCasing>, TCasing extends CasingType, TStorageKey extends string, TFormAlias extends string, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>, TFormProps extends object, TResolvedStep extends ResolvedStep$1<TStep, TCasing> = ResolvedStep$1<TStep, TCasing>> extends MultiStepFormSchemaStepConfig<TStep, TCasing, TStorageKey>, MultiStepFormSchemaConfig.Form<TResolvedStep, TFormAlias, TFormEnabledFor, TFormProps> {}
11
+ type CreateFunction<TArgs extends any[], TReturn = void> = (...args: TArgs) => TReturn;
12
+ type CreateComponent<TInput, TProps> = CreateFunction<[input: TInput, props: TProps], ReactNode>;
13
+ type CreateComponentCallback<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TProps> = CreateComponent<HelperFnInputBase<TResolvedStep, TSteps, TChosenSteps>, TProps>;
14
+ type CreatedMultiStepFormComponent<TProps> = TProps extends undefined ? () => ReactNode : (props: TProps) => ReactNode;
15
+ type CreateComponentFn<TResolvedStep extends AnyResolvedStep$1, TStepNumbers extends StepNumbers<TResolvedStep>> = <chosenSteps extends HelperFnChosenSteps<TResolvedStep, TStepNumbers>, props = undefined>(options: CreateHelperFunctionOptionsBase<TResolvedStep, TStepNumbers, chosenSteps>, fn: CreateComponentCallback<TResolvedStep, TStepNumbers, chosenSteps, props>) => CreatedMultiStepFormComponent<props>;
16
+ declare namespace StepSpecificComponent {
17
+ type formComponent<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TFormAlias extends string, TFormProps extends object, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>> = TFormEnabledFor extends MultiStepFormSchemaConfig.defaultEnabledFor ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps> : TFormEnabledFor extends HelperFnChosenSteps.tupleNotation<ValidStepKey<TSteps>> ? TFormEnabledFor[number] extends keyof TResolvedStep ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<ValidStepKey<TSteps>> ? TChosenSteps[number] extends keyof TResolvedStep ? TChosenSteps[number] extends TFormEnabledFor[number] ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps> : {} : {} : {} : {} : keyof TFormEnabledFor extends keyof TResolvedStep ? TChosenSteps extends HelperFnChosenSteps.tupleNotation<ValidStepKey<TSteps>> ? TChosenSteps[number] extends keyof TResolvedStep ? TChosenSteps[number] extends keyof TFormEnabledFor ? MultiStepFormSchemaConfig.formCtx<TFormAlias, TFormProps> : {} : {} : {} : {};
18
+ type onInputChange<TResolvedStep extends AnyResolvedStep$1, TStepNumbers extends StepNumbers<TResolvedStep>, TTargetStep extends TStepNumbers> = <CurrentStepData extends GetCurrentStep<TResolvedStep, TTargetStep>, Field extends keyof CurrentStepData>(field: Field, updater: Updater<CurrentStepData[Field], Relaxed<CurrentStepData[Field]>>) => void;
19
+ type updateWrappers<TResolvedStep extends StrippedResolvedStep<AnyResolvedStep$1>, TSteps extends StepNumbers<TResolvedStep>, TChosenSteps extends HelperFnChosenSteps<TResolvedStep, TSteps>, TStepNumber extends HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TChosenSteps>> = TStepNumber extends TSteps ? {
20
+ /**
21
+ * A useful wrapper around `update` to update the specific field.
22
+ */
23
+ onInputChange: UpdateFn.stepSpecific<TResolvedStep, TSteps, ValidStepKey<TStepNumber>>;
24
+ /**
25
+ * A useful wrapper for `update` to reset a specific field's value to its
26
+ * original config value.
27
+ * @resetFn
28
+ */
29
+ reset: ResetFn.stepSpecific<TResolvedStep, TSteps, ValidStepKey<TStepNumber>>;
30
+ } : {};
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> & {
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>;
59
+ };
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>;
61
+ const DEFAULT_FORM_INSTANCE_ALIAS = "form";
62
+ type defaultFormInstanceAlias = typeof DEFAULT_FORM_INSTANCE_ALIAS;
63
+ type formInstanceOptions<TAlias extends string, TRenderInput, TReturn> = {
64
+ /**
65
+ * The name of the return value of the `render` method.
66
+ */
67
+ alias?: TAlias;
68
+ /**
69
+ * A function that renders/creates the form instance. This function will be called
70
+ * at the top level of the component, ensuring hooks are called in a valid React context.
71
+ *
72
+ * @param input - The input object containing context and default values
73
+ * @returns The form instance (typically from a hook like `useForm`)
74
+ *
75
+ * @example
76
+ * ```tsx
77
+ * useFormInstance: {
78
+ * render({ defaultValues }) {
79
+ * return useForm({
80
+ * defaultValues,
81
+ * });
82
+ * },
83
+ * }
84
+ * ```
85
+ *
86
+ * **Verification**: The hook call is automatically verified:
87
+ * - Errors are caught and reported with helpful messages
88
+ * - In development, hook calls are logged to console.debug
89
+ * - The hook must be called at the component top level (enforced by the framework)
90
+ */
91
+ render: CreateFunction<[input: TRenderInput], TReturn>;
92
+ };
93
+ interface CtxSelector<TResolvedStep extends AnyResolvedStep$1, TSteps extends StepNumbers<TResolvedStep>, TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>, TCtx> {
94
+ /**
95
+ * A function to select the data that will be available in the `fn`'s ctx.
96
+ * @param input The available input to create the context with.
97
+ * @returns The created ctx.
98
+ */
99
+ ctxData?: (input: HelperFnInputBase<TResolvedStep, TSteps, 'all', HelperFnChosenSteps.resolve<TResolvedStep, TSteps, TTargetStep>>) => TCtx;
100
+ }
101
+ type options<TResolvedStep extends AnyResolvedStep$1, TSteps extends StepNumbers<TResolvedStep>, TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>, TFormInstanceAlias extends string, TFormInstance, TCtx> = CtxSelector<TResolvedStep, TSteps, TTargetStep, TCtx> & {
102
+ /**
103
+ * If set to `true`, you'll be able to open the {@linkcode console} to view logs.
104
+ */
105
+ debug?: boolean;
106
+ useFormInstance?: formInstanceOptions<TFormInstanceAlias, Pick<HelperFnInputBase<TResolvedStep, TSteps, TTargetStep>, 'ctx'> & {
107
+ /**
108
+ * An object containing all the default values for the current step.
109
+ */
110
+ defaultValues: Expand<ExtractedDefaultValues<TResolvedStep, TSteps, TTargetStep>>;
111
+ }, TFormInstance>;
112
+ };
113
+ }
114
+ type CreateStepSpecificComponentCallback<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> = {}> = StepSpecificComponent.callback<TResolvedStep, TSteps, TChosenSteps, TProps, TFormAlias, TFormProps, TFormEnabledFor, TAdditionalInput, TAdditionalCtx>;
115
+ type ExtractedDefaultValues<TResolvedStep extends AnyResolvedStep$1, TSteps extends StepNumbers<TResolvedStep>, TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>, TExtractedStepNumber extends number = HelperFnChosenSteps.extractStepNumber<TResolvedStep, TSteps, TTargetStep>, TFields extends GetFieldsForStep<TResolvedStep, ValidStepKey<TExtractedStepNumber>> = GetFieldsForStep<TResolvedStep, ValidStepKey<TExtractedStepNumber>>> = { [field in keyof TFields]: TFields[field]['defaultValue'] };
116
+ interface StepSpecificCreateComponentFn<TResolvedStep extends AnyResolvedStep$1, TSteps extends StepNumbers<TResolvedStep>, TTargetStep extends HelperFnChosenSteps<TResolvedStep, TSteps>, TFormAlias extends string, TFormProps extends object, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep>> {
117
+ /**
118
+ * A utility function to easily create a component for the current step.
119
+ * @param fn The callback function where the component is defined.
120
+ */
121
+ <props = undefined>(fn: CreateStepSpecificComponentCallback<TResolvedStep, TSteps, TTargetStep, props, TFormAlias, TFormProps, TFormEnabledFor>): CreatedMultiStepFormComponent<props>;
122
+ /**
123
+ * A utility function to easily create a component for the current step.
124
+ * @param options Specific config options for creating a component for the current step.
125
+ * @param fn The callback function where the component is defined.
126
+ * @returns The created component.
127
+ */
128
+ <formInstance, additionalCtx extends Record<string, unknown> = {}, formInstanceAlias extends string = StepSpecificComponent.defaultFormInstanceAlias, props = undefined>(options: StepSpecificComponent.options<TResolvedStep, TSteps, TTargetStep, formInstanceAlias, formInstance, additionalCtx>, fn: CreateStepSpecificComponentCallback<TResolvedStep, TSteps, TTargetStep, props, TFormAlias, TFormProps, TFormEnabledFor, { [_ in formInstanceAlias]: formInstance }, additionalCtx>): CreatedMultiStepFormComponent<props>;
129
+ }
130
+ type ResolvedStep$1<TStep extends Step<TDefaultCasing>, TDefaultCasing extends CasingType = DefaultCasing, TResolvedStep extends ResolvedStep<TStep, TDefaultCasing> = ResolvedStep<TStep, TDefaultCasing>, TFormAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias, TFormEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<TResolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor, TFormProps extends object = ComponentPropsWithRef<'form'>> = { [stepKey in keyof TResolvedStep]: TResolvedStep[stepKey] & (stepKey extends ValidStepKey<StepNumbers<TResolvedStep>> ? {
131
+ createComponent: StepSpecificCreateComponentFn<TResolvedStep, StepNumbers<TResolvedStep>, [stepKey], TFormAlias, TFormProps, TFormEnabledFor>;
132
+ } : {}) };
133
+ type AnyResolvedStep$1 = ResolvedStep$1<any, any, any>;
134
+ interface HelperFunctions<TResolvedStep extends AnyResolvedStep$1, TStepNumbers extends StepNumbers<TResolvedStep>> {
135
+ createComponent: CreateComponentFn<TResolvedStep, TStepNumbers>;
136
+ }
137
+ /**
138
+ * Creates a default values object for the target step.
139
+ * @param steps The steps schema.
140
+ * @param targetStep The step to create the default values for.
141
+ * @returns An object containing the field names and their default values.
142
+ */
143
+ declare function createDefaultValues<resolvedStep extends AnyResolvedStep$1, stepNumbers extends StepNumbers<resolvedStep>, targetStep extends ValidStepKey<stepNumbers>>(steps: resolvedStep, targetStep: targetStep): Expand<ExtractedDefaultValues<resolvedStep, stepNumbers, [targetStep]>>;
144
+ declare class MultiStepFormStepSchema$1<step extends Step<casing>, casing extends CasingType = DefaultCasing, storageKey extends string = DefaultStorageKey, formAlias extends string = MultiStepFormSchemaConfig.defaultFormAlias, formEnabledFor extends MultiStepFormSchemaConfig.formEnabledFor<resolvedStep> = MultiStepFormSchemaConfig.defaultEnabledFor, formProps extends object = ComponentPropsWithRef<'form'>, core extends ResolvedStep<step, casing> = ResolvedStep<step, casing>, resolvedStep extends ResolvedStep$1<step, casing, core, formAlias, formEnabledFor, formProps> = ResolvedStep$1<step, casing, core, formAlias, formEnabledFor, formProps>, stepNumbers extends StepNumbers<resolvedStep> = StepNumbers<resolvedStep>> extends MultiStepFormStepSchema<step, casing> implements HelperFunctions<resolvedStep, stepNumbers> {
145
+ #private;
146
+ value: resolvedStep;
147
+ constructor(config: MultiStepFormSchemaStepConfig$1<step, Constrain<casing, CasingType>, storageKey, formAlias, formEnabledFor, formProps>);
148
+ private createFormComponent;
149
+ private createResolvedCtx;
150
+ private createStepSpecificComponentImpl;
151
+ private createStepSpecificComponentFactory;
152
+ /**
153
+ * A helper function to create a component for a specific step.
154
+ * @param options The options for creating the step specific component.
155
+ * @param fn A callback that is used for accessing the target step's data and defining
156
+ * any props that the component should have. This function must return a valid `JSX` element.
157
+ * @returns The created component for the step.
158
+ */
159
+ createComponent<chosenSteps extends HelperFnChosenSteps<resolvedStep, stepNumbers>, props = undefined>(options: CreateHelperFunctionOptionsBase<resolvedStep, stepNumbers, chosenSteps>, fn: CreateComponentCallback<resolvedStep, stepNumbers, chosenSteps, props>): CreatedMultiStepFormComponent<props>;
160
+ createDefaultValues<targetStep extends ValidStepKey<stepNumbers>>(targetStep: targetStep): Expand<ExtractedDefaultValues<resolvedStep, _jfdevelops_multi_step_form_core0.ExtractStepFromKey<keyof resolvedStep extends infer T ? T extends keyof resolvedStep ? T extends string ? T : never : never : never>, [targetStep], HelperFnChosenSteps.extractStepNumber<resolvedStep, _jfdevelops_multi_step_form_core0.ExtractStepFromKey<keyof resolvedStep extends infer T_1 ? T_1 extends keyof resolvedStep ? T_1 extends string ? T_1 : never : never : never>, [targetStep]>, GetFieldsForStep<resolvedStep, `step${HelperFnChosenSteps.extractStepNumber<resolvedStep, _jfdevelops_multi_step_form_core0.ExtractStepFromKey<keyof resolvedStep extends infer T_2 ? T_2 extends keyof resolvedStep ? T_2 extends string ? T_2 : never : never : never>, [targetStep]>}`>>>;
161
+ }
162
+ //#endregion
163
+ export { AnyResolvedStep$1 as AnyResolvedStep, CreateComponent, CreateComponentCallback, CreateComponentFn, CreateFunction, CreateStepSpecificComponentCallback, CreatedMultiStepFormComponent, ExtractedDefaultValues, HelperFunctions, MultiStepFormSchemaStepConfig$1 as MultiStepFormSchemaStepConfig, MultiStepFormStepSchema$1 as MultiStepFormStepSchema, ResolvedStep$1 as ResolvedStep, StepSpecificComponent, StepSpecificCreateComponentFn, createDefaultValues };
164
+ //# sourceMappingURL=step-schema.d.mts.map
@@ -0,0 +1,222 @@
1
+ import { field } from "./field.mjs";
2
+ import { MultiStepFormSchemaConfig } from "./form-config.mjs";
3
+ import { createUseSelector } from "./hooks/use-selector.mjs";
4
+ import { selector } from "./selector.mjs";
5
+ import { getValidatedCustomInputHooks, resolvedCtxCreator } from "./utils.mjs";
6
+ import { MultiStepFormLogger, MultiStepFormStepSchema, createCtx, createStep, invariant } from "@jfdevelops/multi-step-form-core";
7
+ import { MultiStepFormStepSchemaInternal } from "@jfdevelops/multi-step-form-core/_internals";
8
+
9
+ //#region src/step-schema.ts
10
+ let StepSpecificComponent;
11
+ (function(_StepSpecificComponent) {
12
+ _StepSpecificComponent.DEFAULT_FORM_INSTANCE_ALIAS = "form";
13
+ })(StepSpecificComponent || (StepSpecificComponent = {}));
14
+ /**
15
+ * Creates a default values object for the target step.
16
+ * @param steps The steps schema.
17
+ * @param targetStep The step to create the default values for.
18
+ * @returns An object containing the field names and their default values.
19
+ */
20
+ function createDefaultValues(steps, targetStep) {
21
+ invariant(targetStep in steps, `The target step ${targetStep} is not a valid step key`);
22
+ const current = steps[targetStep];
23
+ invariant("fields" in current, `No "fields" were found for ${targetStep}`);
24
+ let defaultValues = {};
25
+ for (const [fieldName, fieldValues] of Object.entries(current.fields)) defaultValues = {
26
+ ...defaultValues,
27
+ [fieldName]: fieldValues.defaultValue
28
+ };
29
+ return defaultValues;
30
+ }
31
+ var MultiStepFormStepSchema$1 = class extends MultiStepFormStepSchema {
32
+ value;
33
+ #internal;
34
+ constructor(config) {
35
+ const { form, ...rest } = config;
36
+ super(rest);
37
+ this.value = createStep(this.original);
38
+ this.#internal = new MultiStepFormStepSchemaInternal({
39
+ originalValue: this.original,
40
+ getValue: () => this.value,
41
+ setValue: (next) => this.handlePostUpdate(next)
42
+ });
43
+ this.sync();
44
+ this.value = this.#internal.enrichValues(this.value, (step) => {
45
+ const key = `step${step}`;
46
+ const stepData = [key];
47
+ const id = form?.id ?? key;
48
+ return { createComponent: this.createStepSpecificComponentFactory(stepData, {
49
+ isStepSpecific: true,
50
+ defaultId: id,
51
+ form
52
+ }) };
53
+ });
54
+ }
55
+ createFormComponent(form, defaultId) {
56
+ const { render, enabledForSteps = "all", id = defaultId } = form;
57
+ const ctx = {
58
+ id,
59
+ steps: createCtx(this.value, enabledForSteps)
60
+ };
61
+ return (props) => render(ctx, props);
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) {
77
+ return (fn) => ((props) => {
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) ?? {});
86
+ const { defaultId, form } = config;
87
+ const [step] = stepData;
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.`);
89
+ const stepNumber = Number.parseInt(step.replace("step", ""));
90
+ invariant(!Number.isNaN(stepNumber), `[${step}:"createComponent"]: an error occurred while extracting the number`);
91
+ const current = this.value[step];
92
+ invariant("fields" in current, `[${step}:createComponent]: unable to find the "fields" for the current step`);
93
+ invariant(typeof current.fields === "object", `[${step}:createComponent]: the "fields" property must be an object, was ${typeof current.fields}`);
94
+ const Field = field.create((name) => {
95
+ const currentStep = this.value[step];
96
+ const currentFields = Object.keys(currentStep.fields);
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)}"`);
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`);
100
+ const defaultValue = this.getValue(step, name);
101
+ const { label, nameTransformCasing, type } = currentStep.fields[name];
102
+ const targetFields = `fields.${name}.defaultValue`;
103
+ return {
104
+ defaultValue,
105
+ label,
106
+ nameTransformCasing,
107
+ type,
108
+ name,
109
+ onInputChange: (value) => {
110
+ let resolvedValue;
111
+ if (typeof value === "function") resolvedValue = value(this.getValue(step, name));
112
+ else resolvedValue = value;
113
+ this.update({
114
+ targetStep: step,
115
+ updater: resolvedValue,
116
+ fields: [targetFields]
117
+ });
118
+ },
119
+ reset: () => this.reset({
120
+ fields: [targetFields],
121
+ targetStep: step
122
+ })
123
+ };
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);
135
+ let fnInput = {
136
+ ctx: resolvedCtx,
137
+ onInputChange: this.#internal.createStepUpdaterFn(step),
138
+ reset: this.#internal.createStepResetterFn(step),
139
+ Field,
140
+ useSelector,
141
+ Selector,
142
+ ...hookResults
143
+ };
144
+ if (form) {
145
+ const { alias = MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS, ...rest } = form;
146
+ const enabledFor = rest.enabledForSteps ?? "all";
147
+ if (MultiStepFormSchemaConfig.isFormAvailable(stepData, enabledFor)) fnInput = {
148
+ ...fnInput,
149
+ [alias]: this.createFormComponent(rest, defaultId)
150
+ };
151
+ return fn(fnInput, props);
152
+ }
153
+ return fn({
154
+ ...fnInput,
155
+ [MultiStepFormSchemaConfig.DEFAULT_FORM_ALIAS]: MultiStepFormSchemaConfig.createDefaultForm(defaultId)
156
+ }, props);
157
+ });
158
+ }
159
+ createStepSpecificComponentFactory(stepData, config) {
160
+ const createStepSpecificComponentImpl = this.createStepSpecificComponentImpl.bind(this);
161
+ const createDefaultValues$1 = this.createDefaultValues.bind(this);
162
+ const [targetStep] = stepData;
163
+ function impl(optionsOrFn, fn) {
164
+ function createStepSpecificComponent() {
165
+ invariant(typeof optionsOrFn === "function", "The first argument must be a function");
166
+ return createStepSpecificComponentImpl(stepData, config)(optionsOrFn);
167
+ }
168
+ if (typeof optionsOrFn === "object") {
169
+ const { useFormInstance, ctxData, debug } = optionsOrFn;
170
+ const logger = new MultiStepFormLogger({
171
+ debug,
172
+ prefix(prefix) {
173
+ return `${prefix}-${targetStep}-createComponent`;
174
+ }
175
+ });
176
+ logger.info("First argument is an object");
177
+ invariant(typeof fn === "function", "The second argument must be a function");
178
+ if (useFormInstance) {
179
+ const { render, alias = StepSpecificComponent.DEFAULT_FORM_INSTANCE_ALIAS } = useFormInstance;
180
+ const [step] = stepData;
181
+ return createStepSpecificComponentImpl(stepData, config, {
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);
192
+ }
193
+ if (ctxData) return createStepSpecificComponentImpl(stepData, config, {
194
+ logger,
195
+ ctxData
196
+ })(fn);
197
+ return createStepSpecificComponent();
198
+ }
199
+ return createStepSpecificComponent();
200
+ }
201
+ return impl;
202
+ }
203
+ /**
204
+ * A helper function to create a component for a specific step.
205
+ * @param options The options for creating the step specific component.
206
+ * @param fn A callback that is used for accessing the target step's data and defining
207
+ * any props that the component should have. This function must return a valid `JSX` element.
208
+ * @returns The created component for the step.
209
+ */
210
+ createComponent(options, fn) {
211
+ const { stepData } = options;
212
+ const ctx = createCtx(this.value, stepData);
213
+ return ((props) => fn(ctx, props));
214
+ }
215
+ createDefaultValues(targetStep) {
216
+ return createDefaultValues(this.value, targetStep);
217
+ }
218
+ };
219
+
220
+ //#endregion
221
+ export { MultiStepFormStepSchema$1 as MultiStepFormStepSchema, StepSpecificComponent, createDefaultValues };
222
+ //# sourceMappingURL=step-schema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"step-schema.mjs","names":["MultiStepFormStepSchema","MultiStepFormStepSchemaBase","#internal","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":"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"}
package/dist/utils.cjs ADDED
@@ -0,0 +1,35 @@
1
+ let __jfdevelops_multi_step_form_core = require("@jfdevelops/multi-step-form-core");
2
+
3
+ //#region src/utils.ts
4
+ function resolvedCtxCreator(logger, values) {
5
+ return function(options) {
6
+ const { ctx, ctxData } = options;
7
+ logger.info("Option \"ctxData\" is defined");
8
+ (0, __jfdevelops_multi_step_form_core.invariant)(typeof ctxData === "function", "Option \"ctxData\" must be a function");
9
+ const additionalCtx = ctxData({ ctx: values });
10
+ logger.info(`Addition context is: ${JSON.stringify(additionalCtx, null, 2)}`);
11
+ const resolvedCtx = {
12
+ ...ctx,
13
+ ...additionalCtx
14
+ };
15
+ logger.info(`Resolved context is: ${JSON.stringify(resolvedCtx, null, 2)}`);
16
+ return resolvedCtx;
17
+ };
18
+ }
19
+ function getValidatedCustomInputHooks(input) {
20
+ const result = {};
21
+ for (const [key, value] of Object.entries(input)) if (typeof value === "function") try {
22
+ const result$1 = value();
23
+ result$1[key] = result$1;
24
+ } catch (error) {
25
+ const errorMessage = error instanceof Error ? error.message : String(error);
26
+ throw new Error(`[multi-step-form] Error calling hook "${key}" in useFormInstance.render: ${errorMessage}\n\nThis usually means:\n1. The hook is being called outside of a React component\n2. The hook has invalid dependencies or configuration\n3. There's an error in your hook implementation\n\nOriginal error: ${errorMessage}`, { cause: error });
27
+ }
28
+ else result[key] = value;
29
+ return result;
30
+ }
31
+
32
+ //#endregion
33
+ exports.getValidatedCustomInputHooks = getValidatedCustomInputHooks;
34
+ exports.resolvedCtxCreator = resolvedCtxCreator;
35
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","names":["result: Record<string, unknown>","result"],"sources":["../src/utils.ts"],"sourcesContent":["import {\n invariant,\n type Expand,\n type HelperFnChosenSteps,\n type HelperFnCtx,\n type MultiStepFormLogger,\n type StepNumbers,\n type ValidStepKey,\n} from '@jfdevelops/multi-step-form-core';\nimport type { AnyResolvedStep, StepSpecificComponent } from './step-schema';\n\nexport function resolvedCtxCreator<\n resolvedStep extends AnyResolvedStep,\n stepNumbers extends StepNumbers<resolvedStep>\n>(\n logger: MultiStepFormLogger,\n values: Omit<resolvedStep, `step${stepNumbers}`>\n) {\n return function <\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >,\n additionalCtx\n >(\n options: Required<\n StepSpecificComponent.CtxSelector<\n resolvedStep,\n stepNumbers,\n chosenStep,\n additionalCtx\n >\n > & { ctx: Expand<HelperFnCtx<resolvedStep, stepNumbers, chosenStep>> }\n ) {\n const { ctx, ctxData } = options;\n\n logger.info('Option \"ctxData\" is defined');\n invariant(\n typeof ctxData === 'function',\n 'Option \"ctxData\" must be a function'\n );\n\n const additionalCtx = ctxData({ ctx: values } as never);\n\n logger.info(\n `Addition context is: ${JSON.stringify(additionalCtx, null, 2)}`\n );\n\n const resolvedCtx = {\n ...ctx,\n ...additionalCtx,\n };\n\n logger.info(`Resolved context is: ${JSON.stringify(resolvedCtx, null, 2)}`);\n\n return resolvedCtx;\n };\n}\n\nexport function getValidatedCustomInputHooks(input: Record<string, unknown>) {\n const result: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(input)) {\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 result[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 result[key] = value;\n }\n }\n\n return result;\n}\n"],"mappings":";;;AAWA,SAAgB,mBAId,QACA,QACA;AACA,QAAO,SAML,SAQA;EACA,MAAM,EAAE,KAAK,YAAY;AAEzB,SAAO,KAAK,gCAA8B;AAC1C,mDACE,OAAO,YAAY,YACnB,wCACD;EAED,MAAM,gBAAgB,QAAQ,EAAE,KAAK,QAAQ,CAAU;AAEvD,SAAO,KACL,wBAAwB,KAAK,UAAU,eAAe,MAAM,EAAE,GAC/D;EAED,MAAM,cAAc;GAClB,GAAG;GACH,GAAG;GACJ;AAED,SAAO,KAAK,wBAAwB,KAAK,UAAU,aAAa,MAAM,EAAE,GAAG;AAE3E,SAAO;;;AAIX,SAAgB,6BAA6B,OAAgC;CAC3E,MAAMA,SAAkC,EAAE;AAE1C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,CAC9C,KAAI,OAAO,UAAU,WACnB,KAAI;EACF,MAAMC,WAAS,OAAO;AAGtB,WAAO,OAAOA;UAUP,OAAO;EAEd,MAAM,eACJ,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;AAExD,QAAM,IAAI,MACR,yCAAyC,IAAI,+BAA+B,aAAa,+MAKpE,gBACrB,EAAE,OAAO,OAAO,CACjB;;KAGH,QAAO,OAAO;AAIlB,QAAO"}
package/dist/utils.mjs ADDED
@@ -0,0 +1,34 @@
1
+ import { invariant } from "@jfdevelops/multi-step-form-core";
2
+
3
+ //#region src/utils.ts
4
+ function resolvedCtxCreator(logger, values) {
5
+ return function(options) {
6
+ const { ctx, ctxData } = options;
7
+ logger.info("Option \"ctxData\" is defined");
8
+ invariant(typeof ctxData === "function", "Option \"ctxData\" must be a function");
9
+ const additionalCtx = ctxData({ ctx: values });
10
+ logger.info(`Addition context is: ${JSON.stringify(additionalCtx, null, 2)}`);
11
+ const resolvedCtx = {
12
+ ...ctx,
13
+ ...additionalCtx
14
+ };
15
+ logger.info(`Resolved context is: ${JSON.stringify(resolvedCtx, null, 2)}`);
16
+ return resolvedCtx;
17
+ };
18
+ }
19
+ function getValidatedCustomInputHooks(input) {
20
+ const result = {};
21
+ for (const [key, value] of Object.entries(input)) if (typeof value === "function") try {
22
+ const result$1 = value();
23
+ result$1[key] = result$1;
24
+ } catch (error) {
25
+ const errorMessage = error instanceof Error ? error.message : String(error);
26
+ throw new Error(`[multi-step-form] Error calling hook "${key}" in useFormInstance.render: ${errorMessage}\n\nThis usually means:\n1. The hook is being called outside of a React component\n2. The hook has invalid dependencies or configuration\n3. There's an error in your hook implementation\n\nOriginal error: ${errorMessage}`, { cause: error });
27
+ }
28
+ else result[key] = value;
29
+ return result;
30
+ }
31
+
32
+ //#endregion
33
+ export { getValidatedCustomInputHooks, resolvedCtxCreator };
34
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","names":["result: Record<string, unknown>","result"],"sources":["../src/utils.ts"],"sourcesContent":["import {\n invariant,\n type Expand,\n type HelperFnChosenSteps,\n type HelperFnCtx,\n type MultiStepFormLogger,\n type StepNumbers,\n type ValidStepKey,\n} from '@jfdevelops/multi-step-form-core';\nimport type { AnyResolvedStep, StepSpecificComponent } from './step-schema';\n\nexport function resolvedCtxCreator<\n resolvedStep extends AnyResolvedStep,\n stepNumbers extends StepNumbers<resolvedStep>\n>(\n logger: MultiStepFormLogger,\n values: Omit<resolvedStep, `step${stepNumbers}`>\n) {\n return function <\n chosenStep extends HelperFnChosenSteps.tupleNotation<\n ValidStepKey<stepNumbers>\n >,\n additionalCtx\n >(\n options: Required<\n StepSpecificComponent.CtxSelector<\n resolvedStep,\n stepNumbers,\n chosenStep,\n additionalCtx\n >\n > & { ctx: Expand<HelperFnCtx<resolvedStep, stepNumbers, chosenStep>> }\n ) {\n const { ctx, ctxData } = options;\n\n logger.info('Option \"ctxData\" is defined');\n invariant(\n typeof ctxData === 'function',\n 'Option \"ctxData\" must be a function'\n );\n\n const additionalCtx = ctxData({ ctx: values } as never);\n\n logger.info(\n `Addition context is: ${JSON.stringify(additionalCtx, null, 2)}`\n );\n\n const resolvedCtx = {\n ...ctx,\n ...additionalCtx,\n };\n\n logger.info(`Resolved context is: ${JSON.stringify(resolvedCtx, null, 2)}`);\n\n return resolvedCtx;\n };\n}\n\nexport function getValidatedCustomInputHooks(input: Record<string, unknown>) {\n const result: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(input)) {\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 result[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 result[key] = value;\n }\n }\n\n return result;\n}\n"],"mappings":";;;AAWA,SAAgB,mBAId,QACA,QACA;AACA,QAAO,SAML,SAQA;EACA,MAAM,EAAE,KAAK,YAAY;AAEzB,SAAO,KAAK,gCAA8B;AAC1C,YACE,OAAO,YAAY,YACnB,wCACD;EAED,MAAM,gBAAgB,QAAQ,EAAE,KAAK,QAAQ,CAAU;AAEvD,SAAO,KACL,wBAAwB,KAAK,UAAU,eAAe,MAAM,EAAE,GAC/D;EAED,MAAM,cAAc;GAClB,GAAG;GACH,GAAG;GACJ;AAED,SAAO,KAAK,wBAAwB,KAAK,UAAU,aAAa,MAAM,EAAE,GAAG;AAE3E,SAAO;;;AAIX,SAAgB,6BAA6B,OAAgC;CAC3E,MAAMA,SAAkC,EAAE;AAE1C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,CAC9C,KAAI,OAAO,UAAU,WACnB,KAAI;EACF,MAAMC,WAAS,OAAO;AAGtB,WAAO,OAAOA;UAUP,OAAO;EAEd,MAAM,eACJ,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;AAExD,QAAM,IAAI,MACR,yCAAyC,IAAI,+BAA+B,aAAa,+MAKpE,gBACrB,EAAE,OAAO,OAAO,CACjB;;KAGH,QAAO,OAAO;AAIlB,QAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jfdevelops/react-multi-step-form",
3
- "version": "1.0.0-alpha.22",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "",
5
5
  "repository": "https://github.com/jfdevelops/multi-step-form",
6
6
  "type": "module",
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "use-sync-external-store": "^1.6.0",
39
- "@jfdevelops/multi-step-form-core": "1.0.0-alpha.17"
39
+ "@jfdevelops/multi-step-form-core": "1.0.0-alpha.18"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public",