@trackunit/react-form-wizard 0.1.96 → 0.1.98
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/index.cjs.js +14 -3
- package/index.esm.js +14 -3
- package/package.json +1 -1
- package/src/FormWizard/FormWizard.d.ts +1 -1
- package/src/FormWizard/useFormWizard.d.ts +3 -2
package/index.cjs.js
CHANGED
|
@@ -348,7 +348,7 @@ const cvaStepContainer = cssClassVarianceUtilities.cvaMerge(["h-full", "grid", "
|
|
|
348
348
|
* The FormWizard component is used for forms in multiple steps.
|
|
349
349
|
* Use the useFormWizard hook to generate the props for this component.
|
|
350
350
|
*/
|
|
351
|
-
const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }) => {
|
|
351
|
+
const FormWizard = ({ lastStepPrimaryActionLabel, isEdit, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }) => {
|
|
352
352
|
var _a;
|
|
353
353
|
const { stepPath, subStepPath } = reactRouter.useParams({ from: basePath });
|
|
354
354
|
const [stepStates, setStepStates] = react.useState({});
|
|
@@ -362,13 +362,16 @@ const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCance
|
|
|
362
362
|
.filter(([_, { defaultValues }]) => defaultValues)
|
|
363
363
|
.map(([key, { defaultValues }]) => [key, defaultValues])));
|
|
364
364
|
const currentVisibleStepKey = (_a = sharedUtils.objectEntries(steps).find(([, { path }]) => subStepPath ? path === `${stepPath}/${subStepPath}` : path === stepPath)) === null || _a === void 0 ? void 0 : _a[0];
|
|
365
|
-
const currentVisibleStepIndex = sharedUtils.objectKeys(steps)
|
|
365
|
+
const currentVisibleStepIndex = sharedUtils.objectKeys(steps)
|
|
366
|
+
.filter(key => { var _a, _b, _c; return (_c = (_b = (_a = steps[key]).shouldRender) === null || _b === void 0 ? void 0 : _b.call(_a, fullFormState)) !== null && _c !== void 0 ? _c : true; })
|
|
367
|
+
.findIndex(key => key === currentVisibleStepKey);
|
|
366
368
|
return (jsxRuntime.jsx("div", { className: cvaFormWizardContainer({ className }), "data-testid": dataTestId, children: jsxRuntime.jsxs("div", { className: cvaFormWizardLayout(), children: [jsxRuntime.jsx(FormWizardSidebar, { children: sharedUtils.objectEntries(steps)
|
|
367
369
|
.filter(([_, step]) => { var _a, _b; return (_b = (_a = step.shouldRender) === null || _a === void 0 ? void 0 : _a.call(step, fullFormState)) !== null && _b !== void 0 ? _b : true; })
|
|
368
|
-
.map(([key, { path, title, sidebarDescription, parent }], index) => {
|
|
370
|
+
.map(([key, { path, title, sidebarDescription, parent, defaultValues }], index) => {
|
|
369
371
|
const isPastStep = index < currentVisibleStepIndex;
|
|
370
372
|
const isFutureStep = index > currentVisibleStepIndex;
|
|
371
373
|
const isCurrentStep = key === currentVisibleStepKey;
|
|
374
|
+
const hasDefaultValue = defaultValues !== undefined;
|
|
372
375
|
const isSubStep = parent !== undefined;
|
|
373
376
|
const onlyParentSteps = sharedUtils.objectEntries(steps)
|
|
374
377
|
.filter(([_, step]) => step.parent === undefined)
|
|
@@ -378,6 +381,14 @@ const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCance
|
|
|
378
381
|
if (isCurrentStep) {
|
|
379
382
|
return "active";
|
|
380
383
|
}
|
|
384
|
+
//If the step has a default value, it is considered valid, so we set it to valid in order to show the green checkmark
|
|
385
|
+
if (hasDefaultValue && stepStates[key] === undefined && isEdit) {
|
|
386
|
+
setStepStates(prev => (Object.assign(Object.assign({}, prev), { [key]: "valid" })));
|
|
387
|
+
}
|
|
388
|
+
//If the step has a default value, we return done, so we can navigate to that step
|
|
389
|
+
if (hasDefaultValue && isEdit) {
|
|
390
|
+
return "done";
|
|
391
|
+
}
|
|
381
392
|
if (isPastStep) {
|
|
382
393
|
return stepStates[key] === "valid" ? "done" : "invalid";
|
|
383
394
|
}
|
package/index.esm.js
CHANGED
|
@@ -344,7 +344,7 @@ const cvaStepContainer = cvaMerge(["h-full", "grid", "bg-white", "overflow-hidde
|
|
|
344
344
|
* The FormWizard component is used for forms in multiple steps.
|
|
345
345
|
* Use the useFormWizard hook to generate the props for this component.
|
|
346
346
|
*/
|
|
347
|
-
const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }) => {
|
|
347
|
+
const FormWizard = ({ lastStepPrimaryActionLabel, isEdit, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }) => {
|
|
348
348
|
var _a;
|
|
349
349
|
const { stepPath, subStepPath } = useParams({ from: basePath });
|
|
350
350
|
const [stepStates, setStepStates] = useState({});
|
|
@@ -358,13 +358,16 @@ const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCance
|
|
|
358
358
|
.filter(([_, { defaultValues }]) => defaultValues)
|
|
359
359
|
.map(([key, { defaultValues }]) => [key, defaultValues])));
|
|
360
360
|
const currentVisibleStepKey = (_a = objectEntries(steps).find(([, { path }]) => subStepPath ? path === `${stepPath}/${subStepPath}` : path === stepPath)) === null || _a === void 0 ? void 0 : _a[0];
|
|
361
|
-
const currentVisibleStepIndex = objectKeys(steps)
|
|
361
|
+
const currentVisibleStepIndex = objectKeys(steps)
|
|
362
|
+
.filter(key => { var _a, _b, _c; return (_c = (_b = (_a = steps[key]).shouldRender) === null || _b === void 0 ? void 0 : _b.call(_a, fullFormState)) !== null && _c !== void 0 ? _c : true; })
|
|
363
|
+
.findIndex(key => key === currentVisibleStepKey);
|
|
362
364
|
return (jsx("div", { className: cvaFormWizardContainer({ className }), "data-testid": dataTestId, children: jsxs("div", { className: cvaFormWizardLayout(), children: [jsx(FormWizardSidebar, { children: objectEntries(steps)
|
|
363
365
|
.filter(([_, step]) => { var _a, _b; return (_b = (_a = step.shouldRender) === null || _a === void 0 ? void 0 : _a.call(step, fullFormState)) !== null && _b !== void 0 ? _b : true; })
|
|
364
|
-
.map(([key, { path, title, sidebarDescription, parent }], index) => {
|
|
366
|
+
.map(([key, { path, title, sidebarDescription, parent, defaultValues }], index) => {
|
|
365
367
|
const isPastStep = index < currentVisibleStepIndex;
|
|
366
368
|
const isFutureStep = index > currentVisibleStepIndex;
|
|
367
369
|
const isCurrentStep = key === currentVisibleStepKey;
|
|
370
|
+
const hasDefaultValue = defaultValues !== undefined;
|
|
368
371
|
const isSubStep = parent !== undefined;
|
|
369
372
|
const onlyParentSteps = objectEntries(steps)
|
|
370
373
|
.filter(([_, step]) => step.parent === undefined)
|
|
@@ -374,6 +377,14 @@ const FormWizard = ({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCance
|
|
|
374
377
|
if (isCurrentStep) {
|
|
375
378
|
return "active";
|
|
376
379
|
}
|
|
380
|
+
//If the step has a default value, it is considered valid, so we set it to valid in order to show the green checkmark
|
|
381
|
+
if (hasDefaultValue && stepStates[key] === undefined && isEdit) {
|
|
382
|
+
setStepStates(prev => (Object.assign(Object.assign({}, prev), { [key]: "valid" })));
|
|
383
|
+
}
|
|
384
|
+
//If the step has a default value, we return done, so we can navigate to that step
|
|
385
|
+
if (hasDefaultValue && isEdit) {
|
|
386
|
+
return "done";
|
|
387
|
+
}
|
|
377
388
|
if (isPastStep) {
|
|
378
389
|
return stepStates[key] === "valid" ? "done" : "invalid";
|
|
379
390
|
}
|
package/package.json
CHANGED
|
@@ -9,4 +9,4 @@ export type FormWizardStepFormReturnMap<TFullSchema extends AnyZodObject> = Part
|
|
|
9
9
|
* The FormWizard component is used for forms in multiple steps.
|
|
10
10
|
* Use the useFormWizard hook to generate the props for this component.
|
|
11
11
|
*/
|
|
12
|
-
export declare const FormWizard: <TFullSchema extends AnyZodObject, TComponentStepDefinitions extends ComponentStepDefinitions<TFullSchema>>({ lastStepPrimaryActionLabel, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }: FromWizardComponentProps<TFullSchema, TComponentStepDefinitions>) => JSX.Element;
|
|
12
|
+
export declare const FormWizard: <TFullSchema extends AnyZodObject, TComponentStepDefinitions extends ComponentStepDefinitions<TFullSchema>>({ lastStepPrimaryActionLabel, isEdit, steps, fullFormSchema, onCancel, className, dataTestId, basePath, }: FromWizardComponentProps<TFullSchema, TComponentStepDefinitions>) => JSX.Element;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RegisteredRouter, RouteIds } from "@tanstack/react-router";
|
|
2
2
|
import { CommonProps } from "@trackunit/react-components";
|
|
3
3
|
import { ReactNode } from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { UseFormReturn } from "react-hook-form";
|
|
5
5
|
import { AnyZodObject, SomeZodObject, z } from "zod";
|
|
6
6
|
export type StepState = "active" | "done" | "invalid" | "disabled" | "ready";
|
|
7
7
|
export type StepDefinitions<TFullSchema extends AnyZodObject> = {
|
|
@@ -23,7 +23,7 @@ interface DefinedStep<TStepSchema extends TFullSchemaValue[keyof TFullSchemaValu
|
|
|
23
23
|
component: (props: StepComponentPropsInner<TStepSchema, TFullSchemaValue>) => React.ReactNode;
|
|
24
24
|
parent?: keyof TFullSchemaValue;
|
|
25
25
|
shouldRender?: (fullFormState: Partial<TFullSchemaValue>) => boolean;
|
|
26
|
-
defaultValues?:
|
|
26
|
+
defaultValues?: TStepSchema;
|
|
27
27
|
}
|
|
28
28
|
export type StepComponentProps<TFullSchemaHook extends () => AnyZodObject, TStepKey extends keyof z.TypeOf<ReturnType<TFullSchemaHook>>> = StepComponentPropsInner<z.TypeOf<ReturnType<TFullSchemaHook>>[TStepKey], z.TypeOf<ReturnType<TFullSchemaHook>>>;
|
|
29
29
|
export type ComponentStepDefinitions<TFullSchema extends AnyZodObject> = {
|
|
@@ -42,6 +42,7 @@ export interface UseFormWizardProps<TFullSchema extends SomeZodObject, TStepDefi
|
|
|
42
42
|
onCancel?: () => void;
|
|
43
43
|
basePath: StringWithStepKey<`${RouteIds<RegisteredRouter["routeTree"]>}`>;
|
|
44
44
|
steps: TStepDefinitions;
|
|
45
|
+
isEdit?: boolean;
|
|
45
46
|
}
|
|
46
47
|
/**
|
|
47
48
|
* A hook to generate the props for the FormWizard component.
|