@jfdevelops/react-multi-step-form 1.0.0-alpha.15 → 1.0.0-alpha.16
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/create-context.cjs +92 -0
- package/dist/create-context.cjs.map +1 -0
- package/dist/create-context.d.cts +154 -0
- package/dist/create-context.d.mts +154 -0
- package/dist/create-context.mjs +92 -0
- package/dist/create-context.mjs.map +1 -0
- package/dist/field.cjs +22 -0
- package/dist/field.cjs.map +1 -0
- package/dist/field.d.cts +38 -0
- package/dist/field.d.mts +38 -0
- package/dist/field.mjs +16 -0
- package/dist/field.mjs.map +1 -0
- package/dist/form-config.cjs +32 -0
- package/dist/form-config.cjs.map +1 -0
- package/dist/form-config.d.cts +160 -0
- package/dist/form-config.d.mts +160 -0
- package/dist/form-config.mjs +27 -0
- package/dist/form-config.mjs.map +1 -0
- package/dist/hooks/use-multi-step-form-data.cjs +41 -0
- package/dist/hooks/use-multi-step-form-data.cjs.map +1 -0
- package/dist/hooks/use-multi-step-form-data.d.cts +28 -0
- package/dist/hooks/use-multi-step-form-data.d.mts +28 -0
- package/dist/hooks/use-multi-step-form-data.mjs +39 -0
- package/dist/hooks/use-multi-step-form-data.mjs.map +1 -0
- package/dist/index.cjs +16 -47
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +5 -1911
- package/dist/schema.cjs +44 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.cts +34 -0
- package/dist/schema.d.mts +34 -0
- package/dist/schema.mjs +43 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/step-schema.cjs +222 -0
- package/dist/step-schema.cjs.map +1 -0
- package/dist/step-schema.d.cts +132 -0
- package/dist/step-schema.d.mts +132 -0
- package/dist/step-schema.mjs +215 -0
- package/dist/step-schema.mjs.map +1 -0
- package/package.json +9 -9
- package/dist/create-context.d.ts +0 -156
- package/dist/field.d.ts +0 -23
- package/dist/form-config.d.ts +0 -161
- package/dist/hooks/use-multi-step-form-data.d.ts +0 -26
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.mjs.map +0 -1
- package/dist/schema.d.ts +0 -30
- package/dist/step-schema.d.ts +0 -131
- package/dist/utils.d.ts +0 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const require_use_multi_step_form_data = require('./hooks/use-multi-step-form-data.cjs');
|
|
2
|
+
let __jfdevelops_multi_step_form_core = require("@jfdevelops/multi-step-form-core");
|
|
3
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
4
|
+
|
|
5
|
+
//#region src/create-context.tsx
|
|
6
|
+
function createComponent(ctx) {
|
|
7
|
+
return function(fn) {
|
|
8
|
+
return ((props) => fn(ctx, props));
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create multi step form context with a {@linkcode MultiStepFormSchema} instance.
|
|
13
|
+
* @param schema The {@linkcode MultiStepFormSchema} instance.
|
|
14
|
+
*/
|
|
15
|
+
function createMultiStepFormContext(schema) {
|
|
16
|
+
const useMultiStepFormData = require_use_multi_step_form_data.createMultiStepFormDataHook(schema);
|
|
17
|
+
function useCurrentStepData(options) {
|
|
18
|
+
const { targetStep, notFoundMessage, isDataGuaranteed } = options;
|
|
19
|
+
const data = useMultiStepFormData({ targetStep });
|
|
20
|
+
const NoDataFoundComponent = notFoundMessage ? withNoStepDataFound({ targetStep }, notFoundMessage) : (props) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
21
|
+
...props,
|
|
22
|
+
children: ["No data found for step ", String(targetStep)]
|
|
23
|
+
});
|
|
24
|
+
if (isDataGuaranteed) return {
|
|
25
|
+
data,
|
|
26
|
+
NoCurrentData: NoDataFoundComponent
|
|
27
|
+
};
|
|
28
|
+
if (__jfdevelops_multi_step_form_core.MultiStepFormStepSchema.hasData(data)) return {
|
|
29
|
+
data,
|
|
30
|
+
hasData: true,
|
|
31
|
+
NoCurrentData: NoDataFoundComponent
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
data: void 0,
|
|
35
|
+
hasData: false,
|
|
36
|
+
NoCurrentData: NoDataFoundComponent
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function useProgress(options) {
|
|
40
|
+
const steps = useMultiStepFormData((data) => data.stepSchema.steps.value.length);
|
|
41
|
+
const { targetStep, maxProgressValue = 100, totalSteps = steps, progressTextTransformer } = options;
|
|
42
|
+
const currentStep = require_use_multi_step_form_data.throwIfInvalidStepNumber(schema, targetStep);
|
|
43
|
+
return {
|
|
44
|
+
value: currentStep / totalSteps * maxProgressValue,
|
|
45
|
+
maxProgressValue,
|
|
46
|
+
ProgressText: progressTextTransformer ? withProgressText({
|
|
47
|
+
targetStep,
|
|
48
|
+
maxProgressValue,
|
|
49
|
+
totalSteps
|
|
50
|
+
}, progressTextTransformer) : (props) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
51
|
+
...props,
|
|
52
|
+
children: [
|
|
53
|
+
"Step ",
|
|
54
|
+
currentStep,
|
|
55
|
+
"/",
|
|
56
|
+
totalSteps
|
|
57
|
+
]
|
|
58
|
+
})
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function useCanRestartForm(cb) {
|
|
62
|
+
const value = useMultiStepFormData((data) => data.storage).get();
|
|
63
|
+
const canRestart = Boolean(value && typeof value === "object" && Object.keys(value).length > 0);
|
|
64
|
+
if (cb) return cb(canRestart);
|
|
65
|
+
return canRestart;
|
|
66
|
+
}
|
|
67
|
+
function withProgressText(options, cb) {
|
|
68
|
+
const steps = schema.getSnapshot().stepSchema.steps.value.length;
|
|
69
|
+
const { targetStep, maxProgressValue = 100, totalSteps = steps } = options;
|
|
70
|
+
return createComponent({
|
|
71
|
+
targetStep,
|
|
72
|
+
maxProgressValue,
|
|
73
|
+
totalSteps
|
|
74
|
+
})(cb);
|
|
75
|
+
}
|
|
76
|
+
function withNoStepDataFound(options, cb) {
|
|
77
|
+
require_use_multi_step_form_data.throwIfInvalidStepNumber(schema, options.targetStep);
|
|
78
|
+
return createComponent(options)(cb);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
useMultiStepFormData,
|
|
82
|
+
useCurrentStepData,
|
|
83
|
+
useProgress,
|
|
84
|
+
useCanRestartForm,
|
|
85
|
+
withProgressText,
|
|
86
|
+
withNoStepDataFound
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
exports.createMultiStepFormContext = createMultiStepFormContext;
|
|
92
|
+
//# sourceMappingURL=create-context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-context.cjs","names":["createMultiStepFormDataHook","MultiStepFormStepSchema","throwIfInvalidStepNumber"],"sources":["../src/create-context.tsx"],"sourcesContent":["import { MultiStepFormStepSchema } from '@jfdevelops/multi-step-form-core';\nimport { type ComponentProps, type ReactNode } from 'react';\nimport {\n createMultiStepFormDataHook,\n throwIfInvalidStepNumber,\n UseMultiStepFormData,\n} from './hooks/use-multi-step-form-data';\nimport { MultiStepFormSchema, type AnyMultiStepFormSchema } from './schema';\nimport type {\n CreatedMultiStepFormComponent,\n CreateFunction,\n} from './step-schema';\n\ntype BaseOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>\n> = {\n /**\n * The step to return data from.\n */\n targetStep: TTargetStep;\n};\nexport type UseCurrentStepOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props,\n isDataGuaranteed extends boolean = false\n> = BaseOptions<TSchema, TTargetStep> & {\n /**\n * Determines if the result should follow \"strictness\".\n * The result will change based on the value for this option.\n *\n * - `true`: `data` is **defined** and `hasData` isn't available.\n * - `false`: `data` _can be_ `undefined`, but the `hasData` property is available\n * to help with type narrowing.\n *\n * @default false\n * @example\n * ### `true`\n * ```tsx\n * function MyComponent() {\n * const { data, NoCurrentData } = useCurrentStep({\n * stepNumber: 1,\n * isDataGuaranteed: true,\n * })\n *\n * // Notice how `NoCurrentData` is still available\n * // Do things with `data` here\n * }\n * ```\n *\n * ### `false` - The default\n * ```tsx\n * function MyComponent() {\n * const { data, NoCurrentData, hasData } = useCurrentStep({\n * stepNumber: 1,\n * })\n *\n * if (!hasData) {\n * return <NoCurrentData />\n * }\n *\n * // Do things with `data` here\n * }\n * ```\n */\n isDataGuaranteed?: isDataGuaranteed;\n /**\n * An optional transformation function to provide a custom not found message.\n */\n notFoundMessage?: CreateFunction<\n [ctx: BaseOptions<TSchema, TTargetStep>, props: props],\n ReactNode\n >;\n};\nexport interface UseCurrentStepBaseResult<TData = unknown, TProps = undefined> {\n /**\n * The current step's data.\n */\n data: TData | undefined;\n /**\n * Boolean indicating if the current step has data.\n */\n hasData: boolean;\n /**\n * Component to render some sort of error if `data` isn't defined.\n */\n NoCurrentData: CreatedMultiStepFormComponent<\n TProps extends undefined ? Omit<ComponentProps<'div'>, 'children'> : TProps\n >;\n}\nexport interface UseCurrentStepErrorResult<TData = unknown, TProps = undefined>\n extends UseCurrentStepBaseResult<TData, TProps> {\n data: undefined;\n hasData: false;\n}\nexport interface UseCurrentStepSuccessResult<\n TData = unknown,\n TProps = undefined\n> extends UseCurrentStepBaseResult<TData, TProps> {\n data: TData;\n hasData: true;\n}\nexport type UseCurrentStepResult<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props,\n isDataGuaranteed extends boolean = false\n> = isDataGuaranteed extends true\n ? Omit<\n UseCurrentStepSuccessResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >,\n 'hasData'\n >\n :\n | UseCurrentStepErrorResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >\n | UseCurrentStepSuccessResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >;\nexport type UseProgressBaseOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>\n> = BaseOptions<TSchema, TTargetStep> & {\n /**\n * The total amount of steps that are in the form.\n *\n * @default schema.stepData.steps.value.length\n */\n totalSteps?: number;\n /**\n * The highest value the progress indicator should go.\n *\n * @default 100\n */\n maxProgressValue?: number;\n};\nexport type UseProgressOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props\n> = UseProgressBaseOptions<TSchema, TTargetStep> & {\n /**\n * An optional transformation function to provide a custom progress text.\n */\n progressTextTransformer?: CreateFunction<\n [ctx: Required<UseProgressBaseOptions<TSchema, TTargetStep>>, props],\n ReactNode\n >;\n};\nexport type UseProgressResult<props> = {\n /**\n * The value of the progress indicator.\n */\n value: number;\n /**\n * The highest value the progress indicator can be.\n *\n * @default 100\n */\n maxProgressValue: number;\n ProgressText: CreatedMultiStepFormComponent<\n props extends undefined ? Omit<ComponentProps<'div'>, 'children'> : props\n >;\n};\nexport type CreateHOC<TContext, TProps> = (\n ctx: TContext,\n props: TProps\n) => CreatedMultiStepFormComponent<TProps>;\n\nexport type MultiStepFormContextResult<\n TSchema extends AnyMultiStepFormSchema,\n TResolvedStep extends MultiStepFormSchema.resolvedStep<TSchema> = MultiStepFormSchema.resolvedStep<TSchema>\n> = {\n // MultiStepFormContext: schema;\n // MultiStepFormProvider: (props: { children: ReactNode }) => JSX.Element;\n // useMultiStepFormSchema: schema;\n useMultiStepFormData: UseMultiStepFormData<TSchema>;\n /**\n * Gets the data for the specified step.\n *\n * @returns The data for the given step number.\n */\n useCurrentStepData: <\n targetStep extends keyof TResolvedStep,\n props = undefined,\n isDataGuaranteed extends boolean = false\n >(\n options: UseCurrentStepOptions<TSchema, targetStep, props, isDataGuaranteed>\n ) => UseCurrentStepResult<TSchema, targetStep, props, isDataGuaranteed>;\n useProgress: <targetStep extends keyof TResolvedStep, props = undefined>(\n options: UseProgressOptions<TSchema, targetStep, props>\n ) => UseProgressResult<props>;\n /**\n * A hook that can be used to check if the form can be restarted. If no {@linkcode cb}\n * is provided, the return value will be dictated by if there is an object stored in\n * {@link MultiStepFormSchema#storage}.\n * @param cb A callback function to provide custom logic for if the form can restart.\n * @returns A boolean indicating if the form can restart.\n */\n useCanRestartForm: (cb?: (canRestart: boolean) => boolean) => boolean;\n /**\n * A HOC for creating a custom progress text for `useProgress`.\n * @param options Options for creating the HOC.\n * @param cb The callback function for creating the HOC.\n * @returns A HOC for the `progressTextTransformer` option of the `useProgress` hook.\n */\n withProgressText: <targetStep extends keyof TResolvedStep, props = undefined>(\n options: UseProgressBaseOptions<TSchema, targetStep>,\n cb: (\n ctx: Required<UseProgressBaseOptions<TSchema, targetStep>>,\n props: props\n ) => ReactNode\n ) => CreatedMultiStepFormComponent<props>;\n /**\n * A HOC for creating a custom not found component for when a step's data is `undefined`.\n * @param options Options for creating the HOC.\n * @param cb The callback function for creating the HOC.\n * @returns A HOC for the `notFoundMessage` option of the `useCurrentStep` hook.\n */\n withNoStepDataFound: <\n targetStep extends keyof TResolvedStep,\n props = undefined\n >(\n options: BaseOptions<TSchema, targetStep>,\n cb: (ctx: BaseOptions<TSchema, targetStep>, props: props) => ReactNode\n ) => CreatedMultiStepFormComponent<props>;\n};\n\nfunction createComponent<ctx>(ctx: ctx) {\n return function <props>(fn: CreateFunction<[ctx, props], ReactNode>) {\n return ((props: props) =>\n fn(ctx, props)) as CreatedMultiStepFormComponent<props>;\n };\n}\n\n/**\n * Create multi step form context with a {@linkcode MultiStepFormSchema} instance.\n * @param schema The {@linkcode MultiStepFormSchema} instance.\n */\n// export function createMultiStepFormContext<\n// schema extends AnyMultiStepFormSchema\n// >(schema: schema): MultiStepFormContextResult<schema>;\n// /**\n// * Create multi step form context without a {@linkcode MultiStepFormSchema} instance.\n// *\n// * The {@linkcode MultiStepFormSchema} instance is returned.\n// * @param options Options to create a new instance of {@linkcode MultiStepFormSchema}.\n// */\n// export function createMultiStepFormContext<\n// step extends Step<casing>,\n// casing extends CasingType = DefaultCasing,\n// storageKey extends string = DefaultStorageKey,\n// resolvedStep extends ResolvedStep<step, casing> = ResolvedStep<step, casing>,\n// stepNumbers extends StepNumbers<resolvedStep> = StepNumbers<resolvedStep>,\n// schema extends MultiStepFormSchema<\n// step,\n// casing,\n// storageKey,\n// resolvedStep,\n// stepNumbers\n// > = MultiStepFormSchema<step, casing, storageKey, resolvedStep, stepNumbers>\n// >(\n// options: MultiStepFormSchemaConfig<\n// step,\n// Constrain<casing, CasingType>,\n// storageKey\n// >\n// ): MultiStepFormContextResult<schema> & {\n// schema: MultiStepFormSchema<step, casing, storageKey>;\n// };\nexport function createMultiStepFormContext<\n schema extends AnyMultiStepFormSchema,\n resolvedStep extends MultiStepFormSchema.resolvedStep<schema> = MultiStepFormSchema.resolvedStep<schema>\n>(schema: schema): MultiStepFormContextResult<schema> {\n // const isInstance = schemaOrOptions instanceof MultiStepFormSchema;\n // const schema: schema = isInstance\n // ? schemaOrOptions\n // : createMultiStepFormSchema(schemaOrOptions);\n // const Context = createContext(schema);\n\n // function Provider({ children }: { children: ReactNode }) {\n // const [observer] = useState(() => new MultiStepFormObserver({ schema }));\n\n // useEffect(() => {\n // const unmount = schema.mount();\n\n // return () => {\n // unmount();\n // observer.destroy();\n // };\n // }, [observer]);\n\n // return <Context.Provider value={observer}>{children}</Context.Provider>;\n // }\n\n // function throwIfInvalidStepNumber(\n // schema: MultiStepFormSchema<step, casing, storageKey>,\n // stepNumber: number\n // ) {\n // const formatter = new Intl.ListFormat('en', {\n // type: 'disjunction',\n // style: 'long',\n // });\n // const { as, isValidStepNumber } = schema.stepSchema.steps;\n\n // invariant(\n // isValidStepNumber(stepNumber),\n // `The step number \"${stepNumber}\" is not a valid step number. Valid step numbers include ${formatter.format(\n // as('array.string.untyped')\n // )}`,\n // TypeError\n // );\n // }\n\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n const useMultiStepFormData = createMultiStepFormDataHook(schema);\n\n function useCurrentStepData<\n targetStep extends keyof resolvedStep,\n props = undefined,\n isDataGuaranteed extends boolean = false\n >(\n options: UseCurrentStepOptions<schema, targetStep, props, isDataGuaranteed>\n ): UseCurrentStepResult<schema, targetStep, props, isDataGuaranteed> {\n const { targetStep, notFoundMessage, isDataGuaranteed } = options;\n const data = useMultiStepFormData({\n targetStep,\n });\n const NoDataFoundComponent = notFoundMessage\n ? withNoStepDataFound({ targetStep }, notFoundMessage)\n : (props: Omit<ComponentProps<'div'>, 'children'>) => (\n <div {...props}>No data found for step {String(targetStep)}</div>\n );\n\n if (isDataGuaranteed) {\n return {\n data,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n if (MultiStepFormStepSchema.hasData(data)) {\n return {\n data,\n hasData: true,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n return {\n data: undefined,\n hasData: false,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n function useProgress<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: UseProgressOptions<schema, targetStep, props>\n ): UseProgressResult<props> {\n const steps = useMultiStepFormData(\n (data) => data.stepSchema.steps.value.length\n );\n const {\n targetStep,\n maxProgressValue = 100,\n totalSteps = steps,\n progressTextTransformer,\n } = options;\n const currentStep = throwIfInvalidStepNumber(schema, targetStep);\n const value = (currentStep / totalSteps) * maxProgressValue;\n const ProgressText = progressTextTransformer\n ? withProgressText(\n { targetStep, maxProgressValue, totalSteps },\n progressTextTransformer\n )\n : (props: Omit<ComponentProps<'div'>, 'children'>) => (\n <div {...props}>\n Step {currentStep}/{totalSteps}\n </div>\n );\n\n return {\n value,\n maxProgressValue,\n ProgressText: ProgressText as never,\n };\n }\n\n function useCanRestartForm(cb?: CreateFunction<[boolean], boolean>) {\n const storage = useMultiStepFormData((data) => data.storage);\n const value = storage.get();\n const canRestart = Boolean(\n value && typeof value === 'object' && Object.keys(value).length > 0\n );\n\n if (cb) {\n return cb(canRestart);\n }\n\n return canRestart;\n }\n\n function withProgressText<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: UseProgressBaseOptions<schema, targetStep>,\n cb: (\n ctx: Required<UseProgressBaseOptions<schema, targetStep>>,\n props: props\n ) => ReactNode\n ) {\n const steps = schema.getSnapshot().stepSchema.steps.value.length;\n const { targetStep, maxProgressValue = 100, totalSteps = steps } = options;\n\n return createComponent({ targetStep, maxProgressValue, totalSteps })(cb);\n }\n\n function withNoStepDataFound<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: BaseOptions<schema, targetStep>,\n cb: (ctx: BaseOptions<schema, targetStep>, props: props) => ReactNode\n ) {\n throwIfInvalidStepNumber(schema, options.targetStep);\n\n return createComponent(options)(cb);\n }\n\n return {\n // MultiStepFormContext: Context,\n useMultiStepFormData,\n useCurrentStepData,\n useProgress,\n useCanRestartForm,\n withProgressText,\n withNoStepDataFound,\n };\n}\n"],"mappings":";;;;;AA0OA,SAAS,gBAAqB,KAAU;AACtC,QAAO,SAAiB,IAA6C;AACnE,WAAS,UACP,GAAG,KAAK,MAAM;;;;;;;AAuCpB,SAAgB,2BAGd,QAAoD;CA0CpD,MAAM,uBAAuBA,6DAA4B,OAAO;CAEhE,SAAS,mBAKP,SACmE;EACnE,MAAM,EAAE,YAAY,iBAAiB,qBAAqB;EAC1D,MAAM,OAAO,qBAAqB,EAChC,YACD,CAAC;EACF,MAAM,uBAAuB,kBACzB,oBAAoB,EAAE,YAAY,EAAE,gBAAgB,IACnD,UACC,4CAAC;GAAI,GAAI;cAAO,2BAAwB,OAAO,WAAW;IAAO;AAGvE,MAAI,iBACF,QAAO;GACL;GACA,eAAe;GAChB;AAGH,MAAIC,0DAAwB,QAAQ,KAAK,CACvC,QAAO;GACL;GACA,SAAS;GACT,eAAe;GAChB;AAGH,SAAO;GACL,MAAM;GACN,SAAS;GACT,eAAe;GAChB;;CAGH,SAAS,YAIP,SAC0B;EAC1B,MAAM,QAAQ,sBACX,SAAS,KAAK,WAAW,MAAM,MAAM,OACvC;EACD,MAAM,EACJ,YACA,mBAAmB,KACnB,aAAa,OACb,4BACE;EACJ,MAAM,cAAcC,0DAAyB,QAAQ,WAAW;AAahE,SAAO;GACL,OAba,cAAc,aAAc;GAczC;GACA,cAdmB,0BACjB,iBACE;IAAE;IAAY;IAAkB;IAAY,EAC5C,wBACD,IACA,UACC,4CAAC;IAAI,GAAI;;KAAO;KACR;KAAY;KAAE;;KAChB;GAOX;;CAGH,SAAS,kBAAkB,IAAyC;EAElE,MAAM,QADU,sBAAsB,SAAS,KAAK,QAAQ,CACtC,KAAK;EAC3B,MAAM,aAAa,QACjB,SAAS,OAAO,UAAU,YAAY,OAAO,KAAK,MAAM,CAAC,SAAS,EACnE;AAED,MAAI,GACF,QAAO,GAAG,WAAW;AAGvB,SAAO;;CAGT,SAAS,iBAIP,SACA,IAIA;EACA,MAAM,QAAQ,OAAO,aAAa,CAAC,WAAW,MAAM,MAAM;EAC1D,MAAM,EAAE,YAAY,mBAAmB,KAAK,aAAa,UAAU;AAEnE,SAAO,gBAAgB;GAAE;GAAY;GAAkB;GAAY,CAAC,CAAC,GAAG;;CAG1E,SAAS,oBAIP,SACA,IACA;AACA,4DAAyB,QAAQ,QAAQ,WAAW;AAEpD,SAAO,gBAAgB,QAAQ,CAAC,GAAG;;AAGrC,QAAO;EAEL;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { CreateFunction, CreatedMultiStepFormComponent } from "./step-schema.cjs";
|
|
2
|
+
import { AnyMultiStepFormSchema, MultiStepFormSchema } from "./schema.cjs";
|
|
3
|
+
import { UseMultiStepFormData } from "./hooks/use-multi-step-form-data.cjs";
|
|
4
|
+
import { ComponentProps, ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
//#region src/create-context.d.ts
|
|
7
|
+
type BaseOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>> = {
|
|
8
|
+
/**
|
|
9
|
+
* The step to return data from.
|
|
10
|
+
*/
|
|
11
|
+
targetStep: TTargetStep;
|
|
12
|
+
};
|
|
13
|
+
type UseCurrentStepOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props, isDataGuaranteed extends boolean = false> = BaseOptions<TSchema, TTargetStep> & {
|
|
14
|
+
/**
|
|
15
|
+
* Determines if the result should follow "strictness".
|
|
16
|
+
* The result will change based on the value for this option.
|
|
17
|
+
*
|
|
18
|
+
* - `true`: `data` is **defined** and `hasData` isn't available.
|
|
19
|
+
* - `false`: `data` _can be_ `undefined`, but the `hasData` property is available
|
|
20
|
+
* to help with type narrowing.
|
|
21
|
+
*
|
|
22
|
+
* @default false
|
|
23
|
+
* @example
|
|
24
|
+
* ### `true`
|
|
25
|
+
* ```tsx
|
|
26
|
+
* function MyComponent() {
|
|
27
|
+
* const { data, NoCurrentData } = useCurrentStep({
|
|
28
|
+
* stepNumber: 1,
|
|
29
|
+
* isDataGuaranteed: true,
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* // Notice how `NoCurrentData` is still available
|
|
33
|
+
* // Do things with `data` here
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### `false` - The default
|
|
38
|
+
* ```tsx
|
|
39
|
+
* function MyComponent() {
|
|
40
|
+
* const { data, NoCurrentData, hasData } = useCurrentStep({
|
|
41
|
+
* stepNumber: 1,
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* if (!hasData) {
|
|
45
|
+
* return <NoCurrentData />
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // Do things with `data` here
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
isDataGuaranteed?: isDataGuaranteed;
|
|
53
|
+
/**
|
|
54
|
+
* An optional transformation function to provide a custom not found message.
|
|
55
|
+
*/
|
|
56
|
+
notFoundMessage?: CreateFunction<[ctx: BaseOptions<TSchema, TTargetStep>, props: props], ReactNode>;
|
|
57
|
+
};
|
|
58
|
+
interface UseCurrentStepBaseResult<TData = unknown, TProps = undefined> {
|
|
59
|
+
/**
|
|
60
|
+
* The current step's data.
|
|
61
|
+
*/
|
|
62
|
+
data: TData | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Boolean indicating if the current step has data.
|
|
65
|
+
*/
|
|
66
|
+
hasData: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Component to render some sort of error if `data` isn't defined.
|
|
69
|
+
*/
|
|
70
|
+
NoCurrentData: CreatedMultiStepFormComponent<TProps extends undefined ? Omit<ComponentProps<'div'>, 'children'> : TProps>;
|
|
71
|
+
}
|
|
72
|
+
interface UseCurrentStepErrorResult<TData = unknown, TProps = undefined> extends UseCurrentStepBaseResult<TData, TProps> {
|
|
73
|
+
data: undefined;
|
|
74
|
+
hasData: false;
|
|
75
|
+
}
|
|
76
|
+
interface UseCurrentStepSuccessResult<TData = unknown, TProps = undefined> extends UseCurrentStepBaseResult<TData, TProps> {
|
|
77
|
+
data: TData;
|
|
78
|
+
hasData: true;
|
|
79
|
+
}
|
|
80
|
+
type UseCurrentStepResult<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props, isDataGuaranteed extends boolean = false> = isDataGuaranteed extends true ? Omit<UseCurrentStepSuccessResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props>, 'hasData'> : UseCurrentStepErrorResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props> | UseCurrentStepSuccessResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props>;
|
|
81
|
+
type UseProgressBaseOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>> = BaseOptions<TSchema, TTargetStep> & {
|
|
82
|
+
/**
|
|
83
|
+
* The total amount of steps that are in the form.
|
|
84
|
+
*
|
|
85
|
+
* @default schema.stepData.steps.value.length
|
|
86
|
+
*/
|
|
87
|
+
totalSteps?: number;
|
|
88
|
+
/**
|
|
89
|
+
* The highest value the progress indicator should go.
|
|
90
|
+
*
|
|
91
|
+
* @default 100
|
|
92
|
+
*/
|
|
93
|
+
maxProgressValue?: number;
|
|
94
|
+
};
|
|
95
|
+
type UseProgressOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props> = UseProgressBaseOptions<TSchema, TTargetStep> & {
|
|
96
|
+
/**
|
|
97
|
+
* An optional transformation function to provide a custom progress text.
|
|
98
|
+
*/
|
|
99
|
+
progressTextTransformer?: CreateFunction<[ctx: Required<UseProgressBaseOptions<TSchema, TTargetStep>>, props], ReactNode>;
|
|
100
|
+
};
|
|
101
|
+
type UseProgressResult<props> = {
|
|
102
|
+
/**
|
|
103
|
+
* The value of the progress indicator.
|
|
104
|
+
*/
|
|
105
|
+
value: number;
|
|
106
|
+
/**
|
|
107
|
+
* The highest value the progress indicator can be.
|
|
108
|
+
*
|
|
109
|
+
* @default 100
|
|
110
|
+
*/
|
|
111
|
+
maxProgressValue: number;
|
|
112
|
+
ProgressText: CreatedMultiStepFormComponent<props extends undefined ? Omit<ComponentProps<'div'>, 'children'> : props>;
|
|
113
|
+
};
|
|
114
|
+
type CreateHOC<TContext, TProps> = (ctx: TContext, props: TProps) => CreatedMultiStepFormComponent<TProps>;
|
|
115
|
+
type MultiStepFormContextResult<TSchema extends AnyMultiStepFormSchema, TResolvedStep extends MultiStepFormSchema.resolvedStep<TSchema> = MultiStepFormSchema.resolvedStep<TSchema>> = {
|
|
116
|
+
useMultiStepFormData: UseMultiStepFormData<TSchema>;
|
|
117
|
+
/**
|
|
118
|
+
* Gets the data for the specified step.
|
|
119
|
+
*
|
|
120
|
+
* @returns The data for the given step number.
|
|
121
|
+
*/
|
|
122
|
+
useCurrentStepData: <targetStep extends keyof TResolvedStep, props = undefined, isDataGuaranteed extends boolean = false>(options: UseCurrentStepOptions<TSchema, targetStep, props, isDataGuaranteed>) => UseCurrentStepResult<TSchema, targetStep, props, isDataGuaranteed>;
|
|
123
|
+
useProgress: <targetStep extends keyof TResolvedStep, props = undefined>(options: UseProgressOptions<TSchema, targetStep, props>) => UseProgressResult<props>;
|
|
124
|
+
/**
|
|
125
|
+
* A hook that can be used to check if the form can be restarted. If no {@linkcode cb}
|
|
126
|
+
* is provided, the return value will be dictated by if there is an object stored in
|
|
127
|
+
* {@link MultiStepFormSchema#storage}.
|
|
128
|
+
* @param cb A callback function to provide custom logic for if the form can restart.
|
|
129
|
+
* @returns A boolean indicating if the form can restart.
|
|
130
|
+
*/
|
|
131
|
+
useCanRestartForm: (cb?: (canRestart: boolean) => boolean) => boolean;
|
|
132
|
+
/**
|
|
133
|
+
* A HOC for creating a custom progress text for `useProgress`.
|
|
134
|
+
* @param options Options for creating the HOC.
|
|
135
|
+
* @param cb The callback function for creating the HOC.
|
|
136
|
+
* @returns A HOC for the `progressTextTransformer` option of the `useProgress` hook.
|
|
137
|
+
*/
|
|
138
|
+
withProgressText: <targetStep extends keyof TResolvedStep, props = undefined>(options: UseProgressBaseOptions<TSchema, targetStep>, cb: (ctx: Required<UseProgressBaseOptions<TSchema, targetStep>>, props: props) => ReactNode) => CreatedMultiStepFormComponent<props>;
|
|
139
|
+
/**
|
|
140
|
+
* A HOC for creating a custom not found component for when a step's data is `undefined`.
|
|
141
|
+
* @param options Options for creating the HOC.
|
|
142
|
+
* @param cb The callback function for creating the HOC.
|
|
143
|
+
* @returns A HOC for the `notFoundMessage` option of the `useCurrentStep` hook.
|
|
144
|
+
*/
|
|
145
|
+
withNoStepDataFound: <targetStep extends keyof TResolvedStep, props = undefined>(options: BaseOptions<TSchema, targetStep>, cb: (ctx: BaseOptions<TSchema, targetStep>, props: props) => ReactNode) => CreatedMultiStepFormComponent<props>;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Create multi step form context with a {@linkcode MultiStepFormSchema} instance.
|
|
149
|
+
* @param schema The {@linkcode MultiStepFormSchema} instance.
|
|
150
|
+
*/
|
|
151
|
+
declare function createMultiStepFormContext<schema extends AnyMultiStepFormSchema, resolvedStep extends MultiStepFormSchema.resolvedStep<schema> = MultiStepFormSchema.resolvedStep<schema>>(schema: schema): MultiStepFormContextResult<schema>;
|
|
152
|
+
//#endregion
|
|
153
|
+
export { CreateHOC, MultiStepFormContextResult, UseCurrentStepBaseResult, UseCurrentStepErrorResult, UseCurrentStepOptions, UseCurrentStepResult, UseCurrentStepSuccessResult, UseProgressBaseOptions, UseProgressOptions, UseProgressResult, createMultiStepFormContext };
|
|
154
|
+
//# sourceMappingURL=create-context.d.cts.map
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { CreateFunction, CreatedMultiStepFormComponent } from "./step-schema.mjs";
|
|
2
|
+
import { AnyMultiStepFormSchema, MultiStepFormSchema } from "./schema.mjs";
|
|
3
|
+
import { UseMultiStepFormData } from "./hooks/use-multi-step-form-data.mjs";
|
|
4
|
+
import { ComponentProps, ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
//#region src/create-context.d.ts
|
|
7
|
+
type BaseOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>> = {
|
|
8
|
+
/**
|
|
9
|
+
* The step to return data from.
|
|
10
|
+
*/
|
|
11
|
+
targetStep: TTargetStep;
|
|
12
|
+
};
|
|
13
|
+
type UseCurrentStepOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props, isDataGuaranteed extends boolean = false> = BaseOptions<TSchema, TTargetStep> & {
|
|
14
|
+
/**
|
|
15
|
+
* Determines if the result should follow "strictness".
|
|
16
|
+
* The result will change based on the value for this option.
|
|
17
|
+
*
|
|
18
|
+
* - `true`: `data` is **defined** and `hasData` isn't available.
|
|
19
|
+
* - `false`: `data` _can be_ `undefined`, but the `hasData` property is available
|
|
20
|
+
* to help with type narrowing.
|
|
21
|
+
*
|
|
22
|
+
* @default false
|
|
23
|
+
* @example
|
|
24
|
+
* ### `true`
|
|
25
|
+
* ```tsx
|
|
26
|
+
* function MyComponent() {
|
|
27
|
+
* const { data, NoCurrentData } = useCurrentStep({
|
|
28
|
+
* stepNumber: 1,
|
|
29
|
+
* isDataGuaranteed: true,
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* // Notice how `NoCurrentData` is still available
|
|
33
|
+
* // Do things with `data` here
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### `false` - The default
|
|
38
|
+
* ```tsx
|
|
39
|
+
* function MyComponent() {
|
|
40
|
+
* const { data, NoCurrentData, hasData } = useCurrentStep({
|
|
41
|
+
* stepNumber: 1,
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* if (!hasData) {
|
|
45
|
+
* return <NoCurrentData />
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // Do things with `data` here
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
isDataGuaranteed?: isDataGuaranteed;
|
|
53
|
+
/**
|
|
54
|
+
* An optional transformation function to provide a custom not found message.
|
|
55
|
+
*/
|
|
56
|
+
notFoundMessage?: CreateFunction<[ctx: BaseOptions<TSchema, TTargetStep>, props: props], ReactNode>;
|
|
57
|
+
};
|
|
58
|
+
interface UseCurrentStepBaseResult<TData = unknown, TProps = undefined> {
|
|
59
|
+
/**
|
|
60
|
+
* The current step's data.
|
|
61
|
+
*/
|
|
62
|
+
data: TData | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Boolean indicating if the current step has data.
|
|
65
|
+
*/
|
|
66
|
+
hasData: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Component to render some sort of error if `data` isn't defined.
|
|
69
|
+
*/
|
|
70
|
+
NoCurrentData: CreatedMultiStepFormComponent<TProps extends undefined ? Omit<ComponentProps<'div'>, 'children'> : TProps>;
|
|
71
|
+
}
|
|
72
|
+
interface UseCurrentStepErrorResult<TData = unknown, TProps = undefined> extends UseCurrentStepBaseResult<TData, TProps> {
|
|
73
|
+
data: undefined;
|
|
74
|
+
hasData: false;
|
|
75
|
+
}
|
|
76
|
+
interface UseCurrentStepSuccessResult<TData = unknown, TProps = undefined> extends UseCurrentStepBaseResult<TData, TProps> {
|
|
77
|
+
data: TData;
|
|
78
|
+
hasData: true;
|
|
79
|
+
}
|
|
80
|
+
type UseCurrentStepResult<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props, isDataGuaranteed extends boolean = false> = isDataGuaranteed extends true ? Omit<UseCurrentStepSuccessResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props>, 'hasData'> : UseCurrentStepErrorResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props> | UseCurrentStepSuccessResult<MultiStepFormSchema.getData<TSchema, TTargetStep>, props>;
|
|
81
|
+
type UseProgressBaseOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>> = BaseOptions<TSchema, TTargetStep> & {
|
|
82
|
+
/**
|
|
83
|
+
* The total amount of steps that are in the form.
|
|
84
|
+
*
|
|
85
|
+
* @default schema.stepData.steps.value.length
|
|
86
|
+
*/
|
|
87
|
+
totalSteps?: number;
|
|
88
|
+
/**
|
|
89
|
+
* The highest value the progress indicator should go.
|
|
90
|
+
*
|
|
91
|
+
* @default 100
|
|
92
|
+
*/
|
|
93
|
+
maxProgressValue?: number;
|
|
94
|
+
};
|
|
95
|
+
type UseProgressOptions<TSchema extends AnyMultiStepFormSchema, TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>, props> = UseProgressBaseOptions<TSchema, TTargetStep> & {
|
|
96
|
+
/**
|
|
97
|
+
* An optional transformation function to provide a custom progress text.
|
|
98
|
+
*/
|
|
99
|
+
progressTextTransformer?: CreateFunction<[ctx: Required<UseProgressBaseOptions<TSchema, TTargetStep>>, props], ReactNode>;
|
|
100
|
+
};
|
|
101
|
+
type UseProgressResult<props> = {
|
|
102
|
+
/**
|
|
103
|
+
* The value of the progress indicator.
|
|
104
|
+
*/
|
|
105
|
+
value: number;
|
|
106
|
+
/**
|
|
107
|
+
* The highest value the progress indicator can be.
|
|
108
|
+
*
|
|
109
|
+
* @default 100
|
|
110
|
+
*/
|
|
111
|
+
maxProgressValue: number;
|
|
112
|
+
ProgressText: CreatedMultiStepFormComponent<props extends undefined ? Omit<ComponentProps<'div'>, 'children'> : props>;
|
|
113
|
+
};
|
|
114
|
+
type CreateHOC<TContext, TProps> = (ctx: TContext, props: TProps) => CreatedMultiStepFormComponent<TProps>;
|
|
115
|
+
type MultiStepFormContextResult<TSchema extends AnyMultiStepFormSchema, TResolvedStep extends MultiStepFormSchema.resolvedStep<TSchema> = MultiStepFormSchema.resolvedStep<TSchema>> = {
|
|
116
|
+
useMultiStepFormData: UseMultiStepFormData<TSchema>;
|
|
117
|
+
/**
|
|
118
|
+
* Gets the data for the specified step.
|
|
119
|
+
*
|
|
120
|
+
* @returns The data for the given step number.
|
|
121
|
+
*/
|
|
122
|
+
useCurrentStepData: <targetStep extends keyof TResolvedStep, props = undefined, isDataGuaranteed extends boolean = false>(options: UseCurrentStepOptions<TSchema, targetStep, props, isDataGuaranteed>) => UseCurrentStepResult<TSchema, targetStep, props, isDataGuaranteed>;
|
|
123
|
+
useProgress: <targetStep extends keyof TResolvedStep, props = undefined>(options: UseProgressOptions<TSchema, targetStep, props>) => UseProgressResult<props>;
|
|
124
|
+
/**
|
|
125
|
+
* A hook that can be used to check if the form can be restarted. If no {@linkcode cb}
|
|
126
|
+
* is provided, the return value will be dictated by if there is an object stored in
|
|
127
|
+
* {@link MultiStepFormSchema#storage}.
|
|
128
|
+
* @param cb A callback function to provide custom logic for if the form can restart.
|
|
129
|
+
* @returns A boolean indicating if the form can restart.
|
|
130
|
+
*/
|
|
131
|
+
useCanRestartForm: (cb?: (canRestart: boolean) => boolean) => boolean;
|
|
132
|
+
/**
|
|
133
|
+
* A HOC for creating a custom progress text for `useProgress`.
|
|
134
|
+
* @param options Options for creating the HOC.
|
|
135
|
+
* @param cb The callback function for creating the HOC.
|
|
136
|
+
* @returns A HOC for the `progressTextTransformer` option of the `useProgress` hook.
|
|
137
|
+
*/
|
|
138
|
+
withProgressText: <targetStep extends keyof TResolvedStep, props = undefined>(options: UseProgressBaseOptions<TSchema, targetStep>, cb: (ctx: Required<UseProgressBaseOptions<TSchema, targetStep>>, props: props) => ReactNode) => CreatedMultiStepFormComponent<props>;
|
|
139
|
+
/**
|
|
140
|
+
* A HOC for creating a custom not found component for when a step's data is `undefined`.
|
|
141
|
+
* @param options Options for creating the HOC.
|
|
142
|
+
* @param cb The callback function for creating the HOC.
|
|
143
|
+
* @returns A HOC for the `notFoundMessage` option of the `useCurrentStep` hook.
|
|
144
|
+
*/
|
|
145
|
+
withNoStepDataFound: <targetStep extends keyof TResolvedStep, props = undefined>(options: BaseOptions<TSchema, targetStep>, cb: (ctx: BaseOptions<TSchema, targetStep>, props: props) => ReactNode) => CreatedMultiStepFormComponent<props>;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Create multi step form context with a {@linkcode MultiStepFormSchema} instance.
|
|
149
|
+
* @param schema The {@linkcode MultiStepFormSchema} instance.
|
|
150
|
+
*/
|
|
151
|
+
declare function createMultiStepFormContext<schema extends AnyMultiStepFormSchema, resolvedStep extends MultiStepFormSchema.resolvedStep<schema> = MultiStepFormSchema.resolvedStep<schema>>(schema: schema): MultiStepFormContextResult<schema>;
|
|
152
|
+
//#endregion
|
|
153
|
+
export { CreateHOC, MultiStepFormContextResult, UseCurrentStepBaseResult, UseCurrentStepErrorResult, UseCurrentStepOptions, UseCurrentStepResult, UseCurrentStepSuccessResult, UseProgressBaseOptions, UseProgressOptions, UseProgressResult, createMultiStepFormContext };
|
|
154
|
+
//# sourceMappingURL=create-context.d.mts.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createMultiStepFormDataHook, throwIfInvalidStepNumber } from "./hooks/use-multi-step-form-data.mjs";
|
|
2
|
+
import { MultiStepFormStepSchema } from "@jfdevelops/multi-step-form-core";
|
|
3
|
+
import { jsxs } from "react/jsx-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/create-context.tsx
|
|
6
|
+
function createComponent(ctx) {
|
|
7
|
+
return function(fn) {
|
|
8
|
+
return ((props) => fn(ctx, props));
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create multi step form context with a {@linkcode MultiStepFormSchema} instance.
|
|
13
|
+
* @param schema The {@linkcode MultiStepFormSchema} instance.
|
|
14
|
+
*/
|
|
15
|
+
function createMultiStepFormContext(schema) {
|
|
16
|
+
const useMultiStepFormData = createMultiStepFormDataHook(schema);
|
|
17
|
+
function useCurrentStepData(options) {
|
|
18
|
+
const { targetStep, notFoundMessage, isDataGuaranteed } = options;
|
|
19
|
+
const data = useMultiStepFormData({ targetStep });
|
|
20
|
+
const NoDataFoundComponent = notFoundMessage ? withNoStepDataFound({ targetStep }, notFoundMessage) : (props) => /* @__PURE__ */ jsxs("div", {
|
|
21
|
+
...props,
|
|
22
|
+
children: ["No data found for step ", String(targetStep)]
|
|
23
|
+
});
|
|
24
|
+
if (isDataGuaranteed) return {
|
|
25
|
+
data,
|
|
26
|
+
NoCurrentData: NoDataFoundComponent
|
|
27
|
+
};
|
|
28
|
+
if (MultiStepFormStepSchema.hasData(data)) return {
|
|
29
|
+
data,
|
|
30
|
+
hasData: true,
|
|
31
|
+
NoCurrentData: NoDataFoundComponent
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
data: void 0,
|
|
35
|
+
hasData: false,
|
|
36
|
+
NoCurrentData: NoDataFoundComponent
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function useProgress(options) {
|
|
40
|
+
const steps = useMultiStepFormData((data) => data.stepSchema.steps.value.length);
|
|
41
|
+
const { targetStep, maxProgressValue = 100, totalSteps = steps, progressTextTransformer } = options;
|
|
42
|
+
const currentStep = throwIfInvalidStepNumber(schema, targetStep);
|
|
43
|
+
return {
|
|
44
|
+
value: currentStep / totalSteps * maxProgressValue,
|
|
45
|
+
maxProgressValue,
|
|
46
|
+
ProgressText: progressTextTransformer ? withProgressText({
|
|
47
|
+
targetStep,
|
|
48
|
+
maxProgressValue,
|
|
49
|
+
totalSteps
|
|
50
|
+
}, progressTextTransformer) : (props) => /* @__PURE__ */ jsxs("div", {
|
|
51
|
+
...props,
|
|
52
|
+
children: [
|
|
53
|
+
"Step ",
|
|
54
|
+
currentStep,
|
|
55
|
+
"/",
|
|
56
|
+
totalSteps
|
|
57
|
+
]
|
|
58
|
+
})
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function useCanRestartForm(cb) {
|
|
62
|
+
const value = useMultiStepFormData((data) => data.storage).get();
|
|
63
|
+
const canRestart = Boolean(value && typeof value === "object" && Object.keys(value).length > 0);
|
|
64
|
+
if (cb) return cb(canRestart);
|
|
65
|
+
return canRestart;
|
|
66
|
+
}
|
|
67
|
+
function withProgressText(options, cb) {
|
|
68
|
+
const steps = schema.getSnapshot().stepSchema.steps.value.length;
|
|
69
|
+
const { targetStep, maxProgressValue = 100, totalSteps = steps } = options;
|
|
70
|
+
return createComponent({
|
|
71
|
+
targetStep,
|
|
72
|
+
maxProgressValue,
|
|
73
|
+
totalSteps
|
|
74
|
+
})(cb);
|
|
75
|
+
}
|
|
76
|
+
function withNoStepDataFound(options, cb) {
|
|
77
|
+
throwIfInvalidStepNumber(schema, options.targetStep);
|
|
78
|
+
return createComponent(options)(cb);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
useMultiStepFormData,
|
|
82
|
+
useCurrentStepData,
|
|
83
|
+
useProgress,
|
|
84
|
+
useCanRestartForm,
|
|
85
|
+
withProgressText,
|
|
86
|
+
withNoStepDataFound
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { createMultiStepFormContext };
|
|
92
|
+
//# sourceMappingURL=create-context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-context.mjs","names":[],"sources":["../src/create-context.tsx"],"sourcesContent":["import { MultiStepFormStepSchema } from '@jfdevelops/multi-step-form-core';\nimport { type ComponentProps, type ReactNode } from 'react';\nimport {\n createMultiStepFormDataHook,\n throwIfInvalidStepNumber,\n UseMultiStepFormData,\n} from './hooks/use-multi-step-form-data';\nimport { MultiStepFormSchema, type AnyMultiStepFormSchema } from './schema';\nimport type {\n CreatedMultiStepFormComponent,\n CreateFunction,\n} from './step-schema';\n\ntype BaseOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>\n> = {\n /**\n * The step to return data from.\n */\n targetStep: TTargetStep;\n};\nexport type UseCurrentStepOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props,\n isDataGuaranteed extends boolean = false\n> = BaseOptions<TSchema, TTargetStep> & {\n /**\n * Determines if the result should follow \"strictness\".\n * The result will change based on the value for this option.\n *\n * - `true`: `data` is **defined** and `hasData` isn't available.\n * - `false`: `data` _can be_ `undefined`, but the `hasData` property is available\n * to help with type narrowing.\n *\n * @default false\n * @example\n * ### `true`\n * ```tsx\n * function MyComponent() {\n * const { data, NoCurrentData } = useCurrentStep({\n * stepNumber: 1,\n * isDataGuaranteed: true,\n * })\n *\n * // Notice how `NoCurrentData` is still available\n * // Do things with `data` here\n * }\n * ```\n *\n * ### `false` - The default\n * ```tsx\n * function MyComponent() {\n * const { data, NoCurrentData, hasData } = useCurrentStep({\n * stepNumber: 1,\n * })\n *\n * if (!hasData) {\n * return <NoCurrentData />\n * }\n *\n * // Do things with `data` here\n * }\n * ```\n */\n isDataGuaranteed?: isDataGuaranteed;\n /**\n * An optional transformation function to provide a custom not found message.\n */\n notFoundMessage?: CreateFunction<\n [ctx: BaseOptions<TSchema, TTargetStep>, props: props],\n ReactNode\n >;\n};\nexport interface UseCurrentStepBaseResult<TData = unknown, TProps = undefined> {\n /**\n * The current step's data.\n */\n data: TData | undefined;\n /**\n * Boolean indicating if the current step has data.\n */\n hasData: boolean;\n /**\n * Component to render some sort of error if `data` isn't defined.\n */\n NoCurrentData: CreatedMultiStepFormComponent<\n TProps extends undefined ? Omit<ComponentProps<'div'>, 'children'> : TProps\n >;\n}\nexport interface UseCurrentStepErrorResult<TData = unknown, TProps = undefined>\n extends UseCurrentStepBaseResult<TData, TProps> {\n data: undefined;\n hasData: false;\n}\nexport interface UseCurrentStepSuccessResult<\n TData = unknown,\n TProps = undefined\n> extends UseCurrentStepBaseResult<TData, TProps> {\n data: TData;\n hasData: true;\n}\nexport type UseCurrentStepResult<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props,\n isDataGuaranteed extends boolean = false\n> = isDataGuaranteed extends true\n ? Omit<\n UseCurrentStepSuccessResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >,\n 'hasData'\n >\n :\n | UseCurrentStepErrorResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >\n | UseCurrentStepSuccessResult<\n MultiStepFormSchema.getData<TSchema, TTargetStep>,\n props\n >;\nexport type UseProgressBaseOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>\n> = BaseOptions<TSchema, TTargetStep> & {\n /**\n * The total amount of steps that are in the form.\n *\n * @default schema.stepData.steps.value.length\n */\n totalSteps?: number;\n /**\n * The highest value the progress indicator should go.\n *\n * @default 100\n */\n maxProgressValue?: number;\n};\nexport type UseProgressOptions<\n TSchema extends AnyMultiStepFormSchema,\n TTargetStep extends keyof MultiStepFormSchema.resolvedStep<TSchema>,\n props\n> = UseProgressBaseOptions<TSchema, TTargetStep> & {\n /**\n * An optional transformation function to provide a custom progress text.\n */\n progressTextTransformer?: CreateFunction<\n [ctx: Required<UseProgressBaseOptions<TSchema, TTargetStep>>, props],\n ReactNode\n >;\n};\nexport type UseProgressResult<props> = {\n /**\n * The value of the progress indicator.\n */\n value: number;\n /**\n * The highest value the progress indicator can be.\n *\n * @default 100\n */\n maxProgressValue: number;\n ProgressText: CreatedMultiStepFormComponent<\n props extends undefined ? Omit<ComponentProps<'div'>, 'children'> : props\n >;\n};\nexport type CreateHOC<TContext, TProps> = (\n ctx: TContext,\n props: TProps\n) => CreatedMultiStepFormComponent<TProps>;\n\nexport type MultiStepFormContextResult<\n TSchema extends AnyMultiStepFormSchema,\n TResolvedStep extends MultiStepFormSchema.resolvedStep<TSchema> = MultiStepFormSchema.resolvedStep<TSchema>\n> = {\n // MultiStepFormContext: schema;\n // MultiStepFormProvider: (props: { children: ReactNode }) => JSX.Element;\n // useMultiStepFormSchema: schema;\n useMultiStepFormData: UseMultiStepFormData<TSchema>;\n /**\n * Gets the data for the specified step.\n *\n * @returns The data for the given step number.\n */\n useCurrentStepData: <\n targetStep extends keyof TResolvedStep,\n props = undefined,\n isDataGuaranteed extends boolean = false\n >(\n options: UseCurrentStepOptions<TSchema, targetStep, props, isDataGuaranteed>\n ) => UseCurrentStepResult<TSchema, targetStep, props, isDataGuaranteed>;\n useProgress: <targetStep extends keyof TResolvedStep, props = undefined>(\n options: UseProgressOptions<TSchema, targetStep, props>\n ) => UseProgressResult<props>;\n /**\n * A hook that can be used to check if the form can be restarted. If no {@linkcode cb}\n * is provided, the return value will be dictated by if there is an object stored in\n * {@link MultiStepFormSchema#storage}.\n * @param cb A callback function to provide custom logic for if the form can restart.\n * @returns A boolean indicating if the form can restart.\n */\n useCanRestartForm: (cb?: (canRestart: boolean) => boolean) => boolean;\n /**\n * A HOC for creating a custom progress text for `useProgress`.\n * @param options Options for creating the HOC.\n * @param cb The callback function for creating the HOC.\n * @returns A HOC for the `progressTextTransformer` option of the `useProgress` hook.\n */\n withProgressText: <targetStep extends keyof TResolvedStep, props = undefined>(\n options: UseProgressBaseOptions<TSchema, targetStep>,\n cb: (\n ctx: Required<UseProgressBaseOptions<TSchema, targetStep>>,\n props: props\n ) => ReactNode\n ) => CreatedMultiStepFormComponent<props>;\n /**\n * A HOC for creating a custom not found component for when a step's data is `undefined`.\n * @param options Options for creating the HOC.\n * @param cb The callback function for creating the HOC.\n * @returns A HOC for the `notFoundMessage` option of the `useCurrentStep` hook.\n */\n withNoStepDataFound: <\n targetStep extends keyof TResolvedStep,\n props = undefined\n >(\n options: BaseOptions<TSchema, targetStep>,\n cb: (ctx: BaseOptions<TSchema, targetStep>, props: props) => ReactNode\n ) => CreatedMultiStepFormComponent<props>;\n};\n\nfunction createComponent<ctx>(ctx: ctx) {\n return function <props>(fn: CreateFunction<[ctx, props], ReactNode>) {\n return ((props: props) =>\n fn(ctx, props)) as CreatedMultiStepFormComponent<props>;\n };\n}\n\n/**\n * Create multi step form context with a {@linkcode MultiStepFormSchema} instance.\n * @param schema The {@linkcode MultiStepFormSchema} instance.\n */\n// export function createMultiStepFormContext<\n// schema extends AnyMultiStepFormSchema\n// >(schema: schema): MultiStepFormContextResult<schema>;\n// /**\n// * Create multi step form context without a {@linkcode MultiStepFormSchema} instance.\n// *\n// * The {@linkcode MultiStepFormSchema} instance is returned.\n// * @param options Options to create a new instance of {@linkcode MultiStepFormSchema}.\n// */\n// export function createMultiStepFormContext<\n// step extends Step<casing>,\n// casing extends CasingType = DefaultCasing,\n// storageKey extends string = DefaultStorageKey,\n// resolvedStep extends ResolvedStep<step, casing> = ResolvedStep<step, casing>,\n// stepNumbers extends StepNumbers<resolvedStep> = StepNumbers<resolvedStep>,\n// schema extends MultiStepFormSchema<\n// step,\n// casing,\n// storageKey,\n// resolvedStep,\n// stepNumbers\n// > = MultiStepFormSchema<step, casing, storageKey, resolvedStep, stepNumbers>\n// >(\n// options: MultiStepFormSchemaConfig<\n// step,\n// Constrain<casing, CasingType>,\n// storageKey\n// >\n// ): MultiStepFormContextResult<schema> & {\n// schema: MultiStepFormSchema<step, casing, storageKey>;\n// };\nexport function createMultiStepFormContext<\n schema extends AnyMultiStepFormSchema,\n resolvedStep extends MultiStepFormSchema.resolvedStep<schema> = MultiStepFormSchema.resolvedStep<schema>\n>(schema: schema): MultiStepFormContextResult<schema> {\n // const isInstance = schemaOrOptions instanceof MultiStepFormSchema;\n // const schema: schema = isInstance\n // ? schemaOrOptions\n // : createMultiStepFormSchema(schemaOrOptions);\n // const Context = createContext(schema);\n\n // function Provider({ children }: { children: ReactNode }) {\n // const [observer] = useState(() => new MultiStepFormObserver({ schema }));\n\n // useEffect(() => {\n // const unmount = schema.mount();\n\n // return () => {\n // unmount();\n // observer.destroy();\n // };\n // }, [observer]);\n\n // return <Context.Provider value={observer}>{children}</Context.Provider>;\n // }\n\n // function throwIfInvalidStepNumber(\n // schema: MultiStepFormSchema<step, casing, storageKey>,\n // stepNumber: number\n // ) {\n // const formatter = new Intl.ListFormat('en', {\n // type: 'disjunction',\n // style: 'long',\n // });\n // const { as, isValidStepNumber } = schema.stepSchema.steps;\n\n // invariant(\n // isValidStepNumber(stepNumber),\n // `The step number \"${stepNumber}\" is not a valid step number. Valid step numbers include ${formatter.format(\n // as('array.string.untyped')\n // )}`,\n // TypeError\n // );\n // }\n\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n const useMultiStepFormData = createMultiStepFormDataHook(schema);\n\n function useCurrentStepData<\n targetStep extends keyof resolvedStep,\n props = undefined,\n isDataGuaranteed extends boolean = false\n >(\n options: UseCurrentStepOptions<schema, targetStep, props, isDataGuaranteed>\n ): UseCurrentStepResult<schema, targetStep, props, isDataGuaranteed> {\n const { targetStep, notFoundMessage, isDataGuaranteed } = options;\n const data = useMultiStepFormData({\n targetStep,\n });\n const NoDataFoundComponent = notFoundMessage\n ? withNoStepDataFound({ targetStep }, notFoundMessage)\n : (props: Omit<ComponentProps<'div'>, 'children'>) => (\n <div {...props}>No data found for step {String(targetStep)}</div>\n );\n\n if (isDataGuaranteed) {\n return {\n data,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n if (MultiStepFormStepSchema.hasData(data)) {\n return {\n data,\n hasData: true,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n return {\n data: undefined,\n hasData: false,\n NoCurrentData: NoDataFoundComponent as never,\n } as never;\n }\n\n function useProgress<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: UseProgressOptions<schema, targetStep, props>\n ): UseProgressResult<props> {\n const steps = useMultiStepFormData(\n (data) => data.stepSchema.steps.value.length\n );\n const {\n targetStep,\n maxProgressValue = 100,\n totalSteps = steps,\n progressTextTransformer,\n } = options;\n const currentStep = throwIfInvalidStepNumber(schema, targetStep);\n const value = (currentStep / totalSteps) * maxProgressValue;\n const ProgressText = progressTextTransformer\n ? withProgressText(\n { targetStep, maxProgressValue, totalSteps },\n progressTextTransformer\n )\n : (props: Omit<ComponentProps<'div'>, 'children'>) => (\n <div {...props}>\n Step {currentStep}/{totalSteps}\n </div>\n );\n\n return {\n value,\n maxProgressValue,\n ProgressText: ProgressText as never,\n };\n }\n\n function useCanRestartForm(cb?: CreateFunction<[boolean], boolean>) {\n const storage = useMultiStepFormData((data) => data.storage);\n const value = storage.get();\n const canRestart = Boolean(\n value && typeof value === 'object' && Object.keys(value).length > 0\n );\n\n if (cb) {\n return cb(canRestart);\n }\n\n return canRestart;\n }\n\n function withProgressText<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: UseProgressBaseOptions<schema, targetStep>,\n cb: (\n ctx: Required<UseProgressBaseOptions<schema, targetStep>>,\n props: props\n ) => ReactNode\n ) {\n const steps = schema.getSnapshot().stepSchema.steps.value.length;\n const { targetStep, maxProgressValue = 100, totalSteps = steps } = options;\n\n return createComponent({ targetStep, maxProgressValue, totalSteps })(cb);\n }\n\n function withNoStepDataFound<\n targetStep extends keyof resolvedStep,\n props = undefined\n >(\n options: BaseOptions<schema, targetStep>,\n cb: (ctx: BaseOptions<schema, targetStep>, props: props) => ReactNode\n ) {\n throwIfInvalidStepNumber(schema, options.targetStep);\n\n return createComponent(options)(cb);\n }\n\n return {\n // MultiStepFormContext: Context,\n useMultiStepFormData,\n useCurrentStepData,\n useProgress,\n useCanRestartForm,\n withProgressText,\n withNoStepDataFound,\n };\n}\n"],"mappings":";;;;;AA0OA,SAAS,gBAAqB,KAAU;AACtC,QAAO,SAAiB,IAA6C;AACnE,WAAS,UACP,GAAG,KAAK,MAAM;;;;;;;AAuCpB,SAAgB,2BAGd,QAAoD;CA0CpD,MAAM,uBAAuB,4BAA4B,OAAO;CAEhE,SAAS,mBAKP,SACmE;EACnE,MAAM,EAAE,YAAY,iBAAiB,qBAAqB;EAC1D,MAAM,OAAO,qBAAqB,EAChC,YACD,CAAC;EACF,MAAM,uBAAuB,kBACzB,oBAAoB,EAAE,YAAY,EAAE,gBAAgB,IACnD,UACC,qBAAC;GAAI,GAAI;cAAO,2BAAwB,OAAO,WAAW;IAAO;AAGvE,MAAI,iBACF,QAAO;GACL;GACA,eAAe;GAChB;AAGH,MAAI,wBAAwB,QAAQ,KAAK,CACvC,QAAO;GACL;GACA,SAAS;GACT,eAAe;GAChB;AAGH,SAAO;GACL,MAAM;GACN,SAAS;GACT,eAAe;GAChB;;CAGH,SAAS,YAIP,SAC0B;EAC1B,MAAM,QAAQ,sBACX,SAAS,KAAK,WAAW,MAAM,MAAM,OACvC;EACD,MAAM,EACJ,YACA,mBAAmB,KACnB,aAAa,OACb,4BACE;EACJ,MAAM,cAAc,yBAAyB,QAAQ,WAAW;AAahE,SAAO;GACL,OAba,cAAc,aAAc;GAczC;GACA,cAdmB,0BACjB,iBACE;IAAE;IAAY;IAAkB;IAAY,EAC5C,wBACD,IACA,UACC,qBAAC;IAAI,GAAI;;KAAO;KACR;KAAY;KAAE;;KAChB;GAOX;;CAGH,SAAS,kBAAkB,IAAyC;EAElE,MAAM,QADU,sBAAsB,SAAS,KAAK,QAAQ,CACtC,KAAK;EAC3B,MAAM,aAAa,QACjB,SAAS,OAAO,UAAU,YAAY,OAAO,KAAK,MAAM,CAAC,SAAS,EACnE;AAED,MAAI,GACF,QAAO,GAAG,WAAW;AAGvB,SAAO;;CAGT,SAAS,iBAIP,SACA,IAIA;EACA,MAAM,QAAQ,OAAO,aAAa,CAAC,WAAW,MAAM,MAAM;EAC1D,MAAM,EAAE,YAAY,mBAAmB,KAAK,aAAa,UAAU;AAEnE,SAAO,gBAAgB;GAAE;GAAY;GAAkB;GAAY,CAAC,CAAC,GAAG;;CAG1E,SAAS,oBAIP,SACA,IACA;AACA,2BAAyB,QAAQ,QAAQ,WAAW;AAEpD,SAAO,gBAAgB,QAAQ,CAAC,GAAG;;AAGrC,QAAO;EAEL;EACA;EACA;EACA;EACA;EACA;EACD"}
|
package/dist/field.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/field.tsx
|
|
3
|
+
let field;
|
|
4
|
+
(function(_field) {
|
|
5
|
+
function create(propsCreator) {
|
|
6
|
+
const Field = (props) => {
|
|
7
|
+
const { children, name } = props;
|
|
8
|
+
return children(propsCreator(name));
|
|
9
|
+
};
|
|
10
|
+
return Field;
|
|
11
|
+
}
|
|
12
|
+
_field.create = create;
|
|
13
|
+
})(field || (field = {}));
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
Object.defineProperty(exports, 'field', {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return field;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
//# sourceMappingURL=field.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field.cjs","names":["Field: field.component<TResolvedStep, TTargetStep>"],"sources":["../src/field.tsx"],"sourcesContent":["import type {\n AnyResolvedStep,\n fields,\n Override,\n Updater,\n} from '@jfdevelops/multi-step-form-core';\nimport type { ReactNode } from 'react';\n\nexport namespace field {\n type sharedProps<TField extends string> = {\n /**\n * The name of the field.\n */\n name: TField;\n };\n\n export type childrenProps<\n TResolvedStep extends AnyResolvedStep,\n TTargetStep extends keyof TResolvedStep,\n TField extends fields.getDeep<TResolvedStep, TTargetStep>,\n TValue extends fields.resolveDeepPath<\n TResolvedStep,\n TTargetStep,\n TField\n > = fields.resolveDeepPath<TResolvedStep, TTargetStep, TField>\n > = sharedProps<TField> &\n Override<\n fields.get<TResolvedStep, TTargetStep>[fields.parentOf<TField>],\n 'defaultValue',\n TValue\n > & {\n /**\n * A useful wrapper around `update` to update the specific field.\n * @param value The new value for the field.\n */\n onInputChange: (value: Updater<TValue>) => void;\n /**\n * Resets the field's value to the original value that was\n * defined in the config.\n */\n reset: () => void;\n };\n export type props<\n TResolvedStep extends AnyResolvedStep,\n TTargetStep extends keyof TResolvedStep,\n TField extends fields.getDeep<TResolvedStep, TTargetStep>\n > = sharedProps<TField> & {\n children: (\n props: childrenProps<TResolvedStep, TTargetStep, TField>\n ) => ReactNode;\n };\n export type component<\n TResolvedStep extends AnyResolvedStep,\n TTargetStep extends keyof TResolvedStep\n > = <TField extends fields.getDeep<TResolvedStep, TTargetStep>>(\n props: props<TResolvedStep, TTargetStep, TField>\n ) => ReactNode;\n\n /**\n * Create a field.\n * @param propsCreator\n * @returns\n */\n export function create<\n TResolvedStep extends AnyResolvedStep,\n TTargetStep extends keyof TResolvedStep\n >(\n propsCreator: <TField extends fields.getDeep<TResolvedStep, TTargetStep>>(\n name: TField\n ) => field.childrenProps<TResolvedStep, TTargetStep, TField>\n ) {\n const Field: field.component<TResolvedStep, TTargetStep> = (props) => {\n const { children, name } = props;\n const createdProps = propsCreator(name);\n\n return children(createdProps);\n };\n\n return Field;\n }\n}\n"],"mappings":";;;;CA+DS,SAAS,OAId,cAGA;EACA,MAAMA,SAAsD,UAAU;GACpE,MAAM,EAAE,UAAU,SAAS;AAG3B,UAAO,SAFc,aAAa,KAAK,CAEV;;AAG/B,SAAO"}
|
package/dist/field.d.cts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AnyResolvedStep, Override, Updater, fields } from "@jfdevelops/multi-step-form-core";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/field.d.ts
|
|
5
|
+
declare namespace field {
|
|
6
|
+
type sharedProps<TField extends string> = {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the field.
|
|
9
|
+
*/
|
|
10
|
+
name: TField;
|
|
11
|
+
};
|
|
12
|
+
export type childrenProps<TResolvedStep extends AnyResolvedStep, TTargetStep extends keyof TResolvedStep, TField extends fields.getDeep<TResolvedStep, TTargetStep>, TValue extends fields.resolveDeepPath<TResolvedStep, TTargetStep, TField> = fields.resolveDeepPath<TResolvedStep, TTargetStep, TField>> = sharedProps<TField> & Override<fields.get<TResolvedStep, TTargetStep>[fields.parentOf<TField>], 'defaultValue', TValue> & {
|
|
13
|
+
/**
|
|
14
|
+
* A useful wrapper around `update` to update the specific field.
|
|
15
|
+
* @param value The new value for the field.
|
|
16
|
+
*/
|
|
17
|
+
onInputChange: (value: Updater<TValue>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Resets the field's value to the original value that was
|
|
20
|
+
* defined in the config.
|
|
21
|
+
*/
|
|
22
|
+
reset: () => void;
|
|
23
|
+
};
|
|
24
|
+
export type props<TResolvedStep extends AnyResolvedStep, TTargetStep extends keyof TResolvedStep, TField extends fields.getDeep<TResolvedStep, TTargetStep>> = sharedProps<TField> & {
|
|
25
|
+
children: (props: childrenProps<TResolvedStep, TTargetStep, TField>) => ReactNode;
|
|
26
|
+
};
|
|
27
|
+
export type component<TResolvedStep extends AnyResolvedStep, TTargetStep extends keyof TResolvedStep> = <TField extends fields.getDeep<TResolvedStep, TTargetStep>>(props: props<TResolvedStep, TTargetStep, TField>) => ReactNode;
|
|
28
|
+
/**
|
|
29
|
+
* Create a field.
|
|
30
|
+
* @param propsCreator
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
export function create<TResolvedStep extends AnyResolvedStep, TTargetStep extends keyof TResolvedStep>(propsCreator: <TField extends fields.getDeep<TResolvedStep, TTargetStep>>(name: TField) => field.childrenProps<TResolvedStep, TTargetStep, TField>): component<TResolvedStep, TTargetStep>;
|
|
34
|
+
export {};
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { field };
|
|
38
|
+
//# sourceMappingURL=field.d.cts.map
|