@btst/components 0.0.0-stage → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/LICENSE.md +17 -0
  2. package/dist/_private/ui/styles/markdown-editor.css +31 -0
  3. package/dist/auto-form/index.cjs +8 -0
  4. package/dist/auto-form/index.d.cts +26 -0
  5. package/dist/auto-form/index.d.mts +26 -0
  6. package/dist/auto-form/index.d.ts +26 -0
  7. package/dist/auto-form/index.mjs +1 -0
  8. package/dist/empty/index.cjs +12 -0
  9. package/dist/empty/index.d.cts +15 -0
  10. package/dist/empty/index.d.mts +15 -0
  11. package/dist/empty/index.d.ts +15 -0
  12. package/dist/empty/index.mjs +1 -0
  13. package/dist/form-builder/index.cjs +16 -0
  14. package/dist/form-builder/index.d.cts +395 -0
  15. package/dist/form-builder/index.d.mts +395 -0
  16. package/dist/form-builder/index.d.ts +395 -0
  17. package/dist/form-builder/index.mjs +4 -0
  18. package/dist/markdown/index.cjs +9 -0
  19. package/dist/markdown/index.d.cts +46 -0
  20. package/dist/markdown/index.d.mts +46 -0
  21. package/dist/markdown/index.d.ts +46 -0
  22. package/dist/markdown/index.mjs +2 -0
  23. package/dist/multi-select/index.cjs +7 -0
  24. package/dist/multi-select/index.d.cts +78 -0
  25. package/dist/multi-select/index.d.mts +78 -0
  26. package/dist/multi-select/index.d.ts +78 -0
  27. package/dist/multi-select/index.mjs +1 -0
  28. package/dist/search-select/index.cjs +7 -0
  29. package/dist/search-select/index.d.cts +16 -0
  30. package/dist/search-select/index.d.mts +16 -0
  31. package/dist/search-select/index.d.ts +16 -0
  32. package/dist/search-select/index.mjs +1 -0
  33. package/dist/shared/components.BdvBiegH.d.cts +291 -0
  34. package/dist/shared/components.BdvBiegH.d.mts +291 -0
  35. package/dist/shared/components.BdvBiegH.d.ts +291 -0
  36. package/dist/shared/components.CZMWR72v.d.cts +10 -0
  37. package/dist/shared/components.CZMWR72v.d.mts +10 -0
  38. package/dist/shared/components.CZMWR72v.d.ts +10 -0
  39. package/dist/stepped-auto-form/index.cjs +7 -0
  40. package/dist/stepped-auto-form/index.d.cts +37 -0
  41. package/dist/stepped-auto-form/index.d.mts +37 -0
  42. package/dist/stepped-auto-form/index.d.ts +37 -0
  43. package/dist/stepped-auto-form/index.mjs +1 -0
  44. package/dist/style.css +440 -0
  45. package/dist/ui/src/components/accordion.cjs +81 -0
  46. package/dist/ui/src/components/accordion.mjs +62 -0
  47. package/dist/ui/src/components/auto-form/common/label.cjs +18 -0
  48. package/dist/ui/src/components/auto-form/common/label.mjs +16 -0
  49. package/dist/ui/src/components/auto-form/common/tooltip.cjs +9 -0
  50. package/dist/ui/src/components/auto-form/common/tooltip.mjs +7 -0
  51. package/dist/ui/src/components/auto-form/config.cjs +40 -0
  52. package/dist/ui/src/components/auto-form/config.mjs +37 -0
  53. package/dist/ui/src/components/auto-form/dependencies.cjs +48 -0
  54. package/dist/ui/src/components/auto-form/dependencies.mjs +46 -0
  55. package/dist/ui/src/components/auto-form/fields/array.cjs +266 -0
  56. package/dist/ui/src/components/auto-form/fields/array.mjs +264 -0
  57. package/dist/ui/src/components/auto-form/fields/checkbox.cjs +38 -0
  58. package/dist/ui/src/components/auto-form/fields/checkbox.mjs +36 -0
  59. package/dist/ui/src/components/auto-form/fields/date.cjs +67 -0
  60. package/dist/ui/src/components/auto-form/fields/date.mjs +65 -0
  61. package/dist/ui/src/components/auto-form/fields/enum.cjs +73 -0
  62. package/dist/ui/src/components/auto-form/fields/enum.mjs +71 -0
  63. package/dist/ui/src/components/auto-form/fields/input.cjs +32 -0
  64. package/dist/ui/src/components/auto-form/fields/input.mjs +30 -0
  65. package/dist/ui/src/components/auto-form/fields/number.cjs +31 -0
  66. package/dist/ui/src/components/auto-form/fields/number.mjs +29 -0
  67. package/dist/ui/src/components/auto-form/fields/object.cjs +233 -0
  68. package/dist/ui/src/components/auto-form/fields/object.mjs +217 -0
  69. package/dist/ui/src/components/auto-form/fields/radio-group.cjs +67 -0
  70. package/dist/ui/src/components/auto-form/fields/radio-group.mjs +65 -0
  71. package/dist/ui/src/components/auto-form/fields/switch.cjs +38 -0
  72. package/dist/ui/src/components/auto-form/fields/switch.mjs +36 -0
  73. package/dist/ui/src/components/auto-form/fields/textarea.cjs +31 -0
  74. package/dist/ui/src/components/auto-form/fields/textarea.mjs +29 -0
  75. package/dist/ui/src/components/auto-form/helpers.cjs +270 -0
  76. package/dist/ui/src/components/auto-form/helpers.mjs +259 -0
  77. package/dist/ui/src/components/auto-form/index.cjs +102 -0
  78. package/dist/ui/src/components/auto-form/index.mjs +93 -0
  79. package/dist/ui/src/components/auto-form/stepped-auto-form.cjs +374 -0
  80. package/dist/ui/src/components/auto-form/stepped-auto-form.mjs +368 -0
  81. package/dist/ui/src/components/auto-form/types.cjs +11 -0
  82. package/dist/ui/src/components/auto-form/types.mjs +9 -0
  83. package/dist/ui/src/components/badge.cjs +42 -0
  84. package/dist/ui/src/components/badge.mjs +39 -0
  85. package/dist/ui/src/components/button.cjs +56 -0
  86. package/dist/ui/src/components/button.mjs +53 -0
  87. package/dist/ui/src/components/calendar.cjs +207 -0
  88. package/dist/ui/src/components/calendar.mjs +190 -0
  89. package/dist/ui/src/components/checkbox.cjs +48 -0
  90. package/dist/ui/src/components/checkbox.mjs +32 -0
  91. package/dist/ui/src/components/command.cjs +118 -0
  92. package/dist/ui/src/components/command.mjs +111 -0
  93. package/dist/ui/src/components/date-picker.cjs +41 -0
  94. package/dist/ui/src/components/date-picker.mjs +39 -0
  95. package/dist/ui/src/components/dialog.cjs +141 -0
  96. package/dist/ui/src/components/dialog.mjs +118 -0
  97. package/dist/ui/src/components/empty.cjs +104 -0
  98. package/dist/ui/src/components/empty.mjs +97 -0
  99. package/dist/ui/src/components/form-builder/canvas.cjs +111 -0
  100. package/dist/ui/src/components/form-builder/canvas.mjs +109 -0
  101. package/dist/ui/src/components/form-builder/components/index.cjs +571 -0
  102. package/dist/ui/src/components/form-builder/components/index.mjs +554 -0
  103. package/dist/ui/src/components/form-builder/edit-field-dialog.cjs +131 -0
  104. package/dist/ui/src/components/form-builder/edit-field-dialog.mjs +129 -0
  105. package/dist/ui/src/components/form-builder/form-preview.cjs +73 -0
  106. package/dist/ui/src/components/form-builder/form-preview.mjs +71 -0
  107. package/dist/ui/src/components/form-builder/index.cjs +350 -0
  108. package/dist/ui/src/components/form-builder/index.mjs +344 -0
  109. package/dist/ui/src/components/form-builder/nested-field-editor-dialog.cjs +263 -0
  110. package/dist/ui/src/components/form-builder/nested-field-editor-dialog.mjs +261 -0
  111. package/dist/ui/src/components/form-builder/palette.cjs +52 -0
  112. package/dist/ui/src/components/form-builder/palette.mjs +49 -0
  113. package/dist/ui/src/components/form-builder/schema-utils.cjs +128 -0
  114. package/dist/ui/src/components/form-builder/schema-utils.mjs +122 -0
  115. package/dist/ui/src/components/form-builder/sortable-field.cjs +151 -0
  116. package/dist/ui/src/components/form-builder/sortable-field.mjs +148 -0
  117. package/dist/ui/src/components/form-builder/step-tabs.cjs +180 -0
  118. package/dist/ui/src/components/form-builder/step-tabs.mjs +178 -0
  119. package/dist/ui/src/components/form-builder/types.cjs +7 -0
  120. package/dist/ui/src/components/form-builder/types.mjs +5 -0
  121. package/dist/ui/src/components/form-builder/validation-schemas.cjs +67 -0
  122. package/dist/ui/src/components/form-builder/validation-schemas.mjs +56 -0
  123. package/dist/ui/src/components/form.cjs +120 -0
  124. package/dist/ui/src/components/form.mjs +98 -0
  125. package/dist/ui/src/components/input.cjs +23 -0
  126. package/dist/ui/src/components/input.mjs +21 -0
  127. package/dist/ui/src/components/label.cjs +39 -0
  128. package/dist/ui/src/components/label.mjs +23 -0
  129. package/dist/ui/src/components/markdown-content.cjs +334 -0
  130. package/dist/ui/src/components/markdown-content.mjs +325 -0
  131. package/dist/ui/src/components/markdown-editor.cjs +180 -0
  132. package/dist/ui/src/components/markdown-editor.mjs +178 -0
  133. package/dist/ui/src/components/multi-select.cjs +555 -0
  134. package/dist/ui/src/components/multi-select.mjs +540 -0
  135. package/dist/ui/src/components/popover.cjs +60 -0
  136. package/dist/ui/src/components/popover.mjs +42 -0
  137. package/dist/ui/src/components/radio-group.cjs +62 -0
  138. package/dist/ui/src/components/radio-group.mjs +45 -0
  139. package/dist/ui/src/components/search-select.cjs +99 -0
  140. package/dist/ui/src/components/search-select.mjs +83 -0
  141. package/dist/ui/src/components/select.cjs +161 -0
  142. package/dist/ui/src/components/select.mjs +139 -0
  143. package/dist/ui/src/components/separator.cjs +43 -0
  144. package/dist/ui/src/components/separator.mjs +27 -0
  145. package/dist/ui/src/components/switch.cjs +48 -0
  146. package/dist/ui/src/components/switch.mjs +32 -0
  147. package/dist/ui/src/components/tabs.cjs +84 -0
  148. package/dist/ui/src/components/tabs.mjs +65 -0
  149. package/dist/ui/src/components/textarea.cjs +20 -0
  150. package/dist/ui/src/components/textarea.mjs +18 -0
  151. package/dist/ui/src/hooks/use-debounce.cjs +16 -0
  152. package/dist/ui/src/hooks/use-debounce.mjs +14 -0
  153. package/dist/ui/src/lib/schema-converter.cjs +94 -0
  154. package/dist/ui/src/lib/schema-converter.mjs +92 -0
  155. package/dist/ui/src/lib/throttle.cjs +56 -0
  156. package/dist/ui/src/lib/throttle.mjs +54 -0
  157. package/dist/ui/src/lib/utils.cjs +10 -0
  158. package/dist/ui/src/lib/utils.mjs +8 -0
  159. package/dist/ui.css +131 -0
  160. package/package.json +176 -3
  161. package/README.md +0 -3
@@ -0,0 +1,102 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const jsxRuntime = require('react/jsx-runtime');
7
+ const form = require('../form.cjs');
8
+ const React = require('react');
9
+ const reactHookForm = require('react-hook-form');
10
+ const button = require('../button.cjs');
11
+ const utils = require('../../lib/utils.cjs');
12
+ const zod = require('@hookform/resolvers/zod');
13
+ const object = require('./fields/object.cjs');
14
+ const helpers = require('./helpers.cjs');
15
+
16
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
+
18
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
19
+
20
+ function AutoFormSubmit({
21
+ children,
22
+ className,
23
+ disabled
24
+ }) {
25
+ return /* @__PURE__ */ jsxRuntime.jsx(button.Button, { type: "submit", disabled, className, children: children ?? "Submit" });
26
+ }
27
+ function AutoForm({
28
+ formSchema,
29
+ values: valuesProp,
30
+ onValuesChange: onValuesChangeProp,
31
+ onParsedValuesChange,
32
+ onSubmit: onSubmitProp,
33
+ fieldConfig,
34
+ children,
35
+ className,
36
+ dependencies
37
+ }) {
38
+ const objectFormSchema = helpers.getObjectFormSchema(formSchema);
39
+ const defaultValues = helpers.getDefaultValues(objectFormSchema, fieldConfig);
40
+ const form$1 = reactHookForm.useForm({
41
+ // Cast to any to handle Zod v4 type compatibility with @hookform/resolvers
42
+ resolver: zod.zodResolver(formSchema),
43
+ defaultValues: defaultValues ?? void 0,
44
+ values: valuesProp
45
+ });
46
+ const [schemaSubmitError, setSchemaSubmitError] = React.useState(null);
47
+ function onSubmit(values) {
48
+ setSchemaSubmitError(null);
49
+ const parsedValues = formSchema.safeParse(values);
50
+ if (parsedValues.success) {
51
+ onSubmitProp?.(parsedValues.data, form$1);
52
+ } else {
53
+ const message = parsedValues.error.issues.map(
54
+ (issue) => `${issue.path.length ? `${issue.path.join(".")}: ` : ""}${issue.message}`
55
+ ).join(" \xB7 ");
56
+ setSchemaSubmitError(message);
57
+ }
58
+ }
59
+ React__default.useEffect(() => {
60
+ const subscription = form$1.watch((values) => {
61
+ onValuesChangeProp?.(values, form$1);
62
+ const parsedValues = formSchema.safeParse(values);
63
+ if (parsedValues.success) {
64
+ onParsedValuesChange?.(parsedValues.data, form$1);
65
+ }
66
+ });
67
+ return () => subscription.unsubscribe();
68
+ }, [form$1, formSchema, onValuesChangeProp, onParsedValuesChange]);
69
+ const renderChildren = typeof children === "function" ? children(form$1.formState) : children;
70
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full", children: /* @__PURE__ */ jsxRuntime.jsx(form.Form, { ...form$1, children: /* @__PURE__ */ jsxRuntime.jsxs(
71
+ "form",
72
+ {
73
+ onSubmit: (e) => {
74
+ form$1.handleSubmit(onSubmit, () => setSchemaSubmitError(null))(e);
75
+ },
76
+ className: utils.cn("space-y-5", className),
77
+ children: [
78
+ /* @__PURE__ */ jsxRuntime.jsx(
79
+ object,
80
+ {
81
+ schema: objectFormSchema,
82
+ form: form$1,
83
+ dependencies,
84
+ fieldConfig
85
+ }
86
+ ),
87
+ schemaSubmitError ? /* @__PURE__ */ jsxRuntime.jsx(
88
+ "div",
89
+ {
90
+ role: "alert",
91
+ className: "rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive",
92
+ children: schemaSubmitError
93
+ }
94
+ ) : null,
95
+ renderChildren
96
+ ]
97
+ }
98
+ ) }) });
99
+ }
100
+
101
+ exports.AutoFormSubmit = AutoFormSubmit;
102
+ exports.default = AutoForm;
@@ -0,0 +1,93 @@
1
+ 'use client';
2
+ import { jsx, jsxs } from 'react/jsx-runtime';
3
+ import { Form } from '../form.mjs';
4
+ import React__default, { useState } from 'react';
5
+ import { useForm } from 'react-hook-form';
6
+ import { Button } from '../button.mjs';
7
+ import { cn } from '../../lib/utils.mjs';
8
+ import { zodResolver } from '@hookform/resolvers/zod';
9
+ import AutoFormObject from './fields/object.mjs';
10
+ import { getObjectFormSchema, getDefaultValues } from './helpers.mjs';
11
+
12
+ function AutoFormSubmit({
13
+ children,
14
+ className,
15
+ disabled
16
+ }) {
17
+ return /* @__PURE__ */ jsx(Button, { type: "submit", disabled, className, children: children ?? "Submit" });
18
+ }
19
+ function AutoForm({
20
+ formSchema,
21
+ values: valuesProp,
22
+ onValuesChange: onValuesChangeProp,
23
+ onParsedValuesChange,
24
+ onSubmit: onSubmitProp,
25
+ fieldConfig,
26
+ children,
27
+ className,
28
+ dependencies
29
+ }) {
30
+ const objectFormSchema = getObjectFormSchema(formSchema);
31
+ const defaultValues = getDefaultValues(objectFormSchema, fieldConfig);
32
+ const form = useForm({
33
+ // Cast to any to handle Zod v4 type compatibility with @hookform/resolvers
34
+ resolver: zodResolver(formSchema),
35
+ defaultValues: defaultValues ?? void 0,
36
+ values: valuesProp
37
+ });
38
+ const [schemaSubmitError, setSchemaSubmitError] = useState(null);
39
+ function onSubmit(values) {
40
+ setSchemaSubmitError(null);
41
+ const parsedValues = formSchema.safeParse(values);
42
+ if (parsedValues.success) {
43
+ onSubmitProp?.(parsedValues.data, form);
44
+ } else {
45
+ const message = parsedValues.error.issues.map(
46
+ (issue) => `${issue.path.length ? `${issue.path.join(".")}: ` : ""}${issue.message}`
47
+ ).join(" \xB7 ");
48
+ setSchemaSubmitError(message);
49
+ }
50
+ }
51
+ React__default.useEffect(() => {
52
+ const subscription = form.watch((values) => {
53
+ onValuesChangeProp?.(values, form);
54
+ const parsedValues = formSchema.safeParse(values);
55
+ if (parsedValues.success) {
56
+ onParsedValuesChange?.(parsedValues.data, form);
57
+ }
58
+ });
59
+ return () => subscription.unsubscribe();
60
+ }, [form, formSchema, onValuesChangeProp, onParsedValuesChange]);
61
+ const renderChildren = typeof children === "function" ? children(form.formState) : children;
62
+ return /* @__PURE__ */ jsx("div", { className: "w-full", children: /* @__PURE__ */ jsx(Form, { ...form, children: /* @__PURE__ */ jsxs(
63
+ "form",
64
+ {
65
+ onSubmit: (e) => {
66
+ form.handleSubmit(onSubmit, () => setSchemaSubmitError(null))(e);
67
+ },
68
+ className: cn("space-y-5", className),
69
+ children: [
70
+ /* @__PURE__ */ jsx(
71
+ AutoFormObject,
72
+ {
73
+ schema: objectFormSchema,
74
+ form,
75
+ dependencies,
76
+ fieldConfig
77
+ }
78
+ ),
79
+ schemaSubmitError ? /* @__PURE__ */ jsx(
80
+ "div",
81
+ {
82
+ role: "alert",
83
+ className: "rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive",
84
+ children: schemaSubmitError
85
+ }
86
+ ) : null,
87
+ renderChildren
88
+ ]
89
+ }
90
+ ) }) });
91
+ }
92
+
93
+ export { AutoFormSubmit, AutoForm as default };
@@ -0,0 +1,374 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ const jsxRuntime = require('react/jsx-runtime');
5
+ const React = require('react');
6
+ const z = require('zod');
7
+ const index = require('./index.cjs');
8
+ const button = require('../button.cjs');
9
+ const separator = require('../separator.cjs');
10
+ const utils = require('../../lib/utils.cjs');
11
+
12
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
+
14
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
15
+
16
+ function extractStepsFromSchema(schema) {
17
+ try {
18
+ const jsonSchema = schema.toJSONSchema();
19
+ if (jsonSchema.steps && Array.isArray(jsonSchema.steps)) {
20
+ return jsonSchema.steps;
21
+ }
22
+ } catch {
23
+ }
24
+ return [];
25
+ }
26
+ function getFieldStepAssignments(schema) {
27
+ const assignments = /* @__PURE__ */ new Map();
28
+ try {
29
+ const jsonSchema = schema.toJSONSchema();
30
+ const stepGroupMap = jsonSchema.stepGroupMap;
31
+ if (stepGroupMap) {
32
+ for (const [fieldName, stepGroup] of Object.entries(stepGroupMap)) {
33
+ assignments.set(fieldName, stepGroup);
34
+ }
35
+ return assignments;
36
+ }
37
+ const properties = jsonSchema.properties;
38
+ if (properties) {
39
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
40
+ const stepGroup = fieldSchema.stepGroup;
41
+ if (typeof stepGroup === "number") {
42
+ assignments.set(fieldName, stepGroup);
43
+ } else {
44
+ assignments.set(fieldName, 0);
45
+ }
46
+ }
47
+ }
48
+ } catch {
49
+ }
50
+ return assignments;
51
+ }
52
+ function createStepSchema(fullSchema, stepIndex, fieldAssignments) {
53
+ const fieldsForStep = Array.from(fieldAssignments.entries()).filter(([, step]) => step === stepIndex).map(([field]) => field);
54
+ if (fieldsForStep.length === 0) {
55
+ return z.z.object({});
56
+ }
57
+ const pickObject = {};
58
+ for (const field of fieldsForStep) {
59
+ pickObject[field] = true;
60
+ }
61
+ return fullSchema.pick(pickObject);
62
+ }
63
+ function getObjectSchema(schema) {
64
+ if (schema instanceof z.z.ZodObject) {
65
+ return schema;
66
+ }
67
+ const schemaDef = schema._zod?.def ?? schema._def;
68
+ if (schemaDef) {
69
+ if (schemaDef.schema) {
70
+ return getObjectSchema(schemaDef.schema);
71
+ }
72
+ if (schemaDef.innerType) {
73
+ return getObjectSchema(schemaDef.innerType);
74
+ }
75
+ }
76
+ return null;
77
+ }
78
+ function DefaultStepper({
79
+ steps,
80
+ currentStepIndex,
81
+ onStepClick
82
+ }) {
83
+ return /* @__PURE__ */ jsxRuntime.jsx("nav", { "aria-label": "Form Steps", className: "mb-6", children: /* @__PURE__ */ jsxRuntime.jsx("ol", { className: "flex items-center justify-between gap-2", children: steps.map((step, index) => /* @__PURE__ */ jsxRuntime.jsxs(React__default.Fragment, { children: [
84
+ /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "flex items-center gap-2 shrink-0", children: [
85
+ /* @__PURE__ */ jsxRuntime.jsx(
86
+ button.Button,
87
+ {
88
+ type: "button",
89
+ variant: index <= currentStepIndex ? "default" : "secondary",
90
+ "aria-current": currentStepIndex === index ? "step" : void 0,
91
+ "aria-posinset": index + 1,
92
+ "aria-setsize": steps.length,
93
+ className: "size-10 rounded-full p-0",
94
+ onClick: () => onStepClick?.(step.id),
95
+ children: index + 1
96
+ }
97
+ ),
98
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-sm font-medium hidden sm:inline", children: step.label })
99
+ ] }),
100
+ index < steps.length - 1 && /* @__PURE__ */ jsxRuntime.jsx(
101
+ separator.Separator,
102
+ {
103
+ className: utils.cn(
104
+ "flex-1 min-w-4",
105
+ index < currentStepIndex ? "bg-primary" : "bg-muted"
106
+ )
107
+ }
108
+ )
109
+ ] }, step.id)) }) });
110
+ }
111
+ function SteppedAutoForm({
112
+ formSchema,
113
+ values: initialValues,
114
+ onValuesChange,
115
+ onSubmit,
116
+ fieldConfig,
117
+ children,
118
+ className,
119
+ nextButtonText = "Next",
120
+ backButtonText = "Back",
121
+ submitButtonText = "Submit",
122
+ StepperComponent = DefaultStepper,
123
+ isSubmitting = false
124
+ }) {
125
+ const steps = React.useMemo(() => extractStepsFromSchema(formSchema), [formSchema]);
126
+ const hasMultipleSteps = steps.length > 1;
127
+ const fieldAssignments = React.useMemo(
128
+ () => getFieldStepAssignments(formSchema),
129
+ [formSchema]
130
+ );
131
+ const [currentStepIndex, setCurrentStepIndex] = React.useState(0);
132
+ const [accumulatedValues, setAccumulatedValues] = React.useState(
133
+ initialValues ?? {}
134
+ );
135
+ const [completedSteps, setCompletedSteps] = React.useState(/* @__PURE__ */ new Set());
136
+ const [schemaErrors, setSchemaErrors] = React.useState(null);
137
+ const accumulatedValuesRef = React.useRef(accumulatedValues);
138
+ const onValuesChangeRef = React.useRef(onValuesChange);
139
+ React.useEffect(() => {
140
+ accumulatedValuesRef.current = accumulatedValues;
141
+ }, [accumulatedValues]);
142
+ React.useEffect(() => {
143
+ onValuesChangeRef.current = onValuesChange;
144
+ }, [onValuesChange]);
145
+ React.useEffect(() => {
146
+ if (initialValues) {
147
+ const newValues = {
148
+ ...accumulatedValuesRef.current,
149
+ ...initialValues
150
+ };
151
+ accumulatedValuesRef.current = newValues;
152
+ setAccumulatedValues(newValues);
153
+ }
154
+ }, [initialValues]);
155
+ const objectSchema = React.useMemo(() => getObjectSchema(formSchema), [formSchema]);
156
+ const stepSchemas = React.useMemo(() => {
157
+ if (!hasMultipleSteps || !objectSchema) return [];
158
+ return steps.map(
159
+ (_, index) => createStepSchema(objectSchema, index, fieldAssignments)
160
+ );
161
+ }, [hasMultipleSteps, objectSchema, steps, fieldAssignments]);
162
+ const currentStep = steps[currentStepIndex];
163
+ const currentStepSchema = stepSchemas[currentStepIndex];
164
+ const isFirst = currentStepIndex === 0;
165
+ const isLast = currentStepIndex === steps.length - 1;
166
+ const currentStepFieldConfig = React.useMemo(() => {
167
+ if (!fieldConfig) return void 0;
168
+ const stepFields = Array.from(fieldAssignments.entries()).filter(([, step]) => step === currentStepIndex).map(([field]) => field);
169
+ const filtered = {};
170
+ for (const field of stepFields) {
171
+ if (field in fieldConfig) {
172
+ filtered[field] = fieldConfig[field];
173
+ }
174
+ }
175
+ return filtered;
176
+ }, [fieldConfig, fieldAssignments, currentStepIndex]);
177
+ const currentStepValues = React.useMemo(() => {
178
+ const stepFields = Array.from(fieldAssignments.entries()).filter(([, step]) => step === currentStepIndex).map(([field]) => field);
179
+ const values = {};
180
+ for (const field of stepFields) {
181
+ if (field in accumulatedValues) {
182
+ values[field] = accumulatedValues[field];
183
+ }
184
+ }
185
+ return values;
186
+ }, [fieldAssignments, currentStepIndex, accumulatedValues]);
187
+ const handleStepClick = React.useCallback(
188
+ (stepId) => {
189
+ const stepIndex = steps.findIndex((s) => s.id === stepId);
190
+ if (stepIndex === -1) return;
191
+ const canNavigate = stepIndex === currentStepIndex || completedSteps.has(stepIndex) || stepIndex === currentStepIndex + 1 && completedSteps.has(currentStepIndex);
192
+ if (canNavigate) {
193
+ setCurrentStepIndex(stepIndex);
194
+ setSchemaErrors(null);
195
+ }
196
+ },
197
+ [steps, currentStepIndex, completedSteps]
198
+ );
199
+ const handleStepValuesChange = React.useCallback(
200
+ (stepValues, form) => {
201
+ const newAccumulated = { ...accumulatedValuesRef.current, ...stepValues };
202
+ accumulatedValuesRef.current = newAccumulated;
203
+ setAccumulatedValues(newAccumulated);
204
+ onValuesChangeRef.current?.(newAccumulated, form);
205
+ },
206
+ []
207
+ );
208
+ const handleStepSubmit = React.useCallback(
209
+ (stepValues) => {
210
+ const newAccumulated = { ...accumulatedValuesRef.current, ...stepValues };
211
+ accumulatedValuesRef.current = newAccumulated;
212
+ setAccumulatedValues(newAccumulated);
213
+ onValuesChangeRef.current?.(newAccumulated, void 0);
214
+ setCompletedSteps((prev) => /* @__PURE__ */ new Set([...prev, currentStepIndex]));
215
+ if (isLast) {
216
+ const allStepsCompleted = steps.every(
217
+ (_, index) => index === currentStepIndex || completedSteps.has(index)
218
+ );
219
+ if (!allStepsCompleted) {
220
+ const firstIncompleteStep = steps.findIndex(
221
+ (_, index) => index !== currentStepIndex && !completedSteps.has(index)
222
+ );
223
+ if (firstIncompleteStep !== -1) {
224
+ setCurrentStepIndex(firstIncompleteStep);
225
+ return;
226
+ }
227
+ }
228
+ const parseResult = formSchema.safeParse(newAccumulated);
229
+ if (parseResult.success) {
230
+ setSchemaErrors(null);
231
+ onSubmit?.(parseResult.data);
232
+ } else {
233
+ setSchemaErrors(parseResult.error);
234
+ const firstErrorPath = parseResult.error.issues[0]?.path[0];
235
+ if (typeof firstErrorPath === "string") {
236
+ const errorFieldStep = fieldAssignments.get(firstErrorPath);
237
+ if (errorFieldStep !== void 0 && errorFieldStep !== currentStepIndex) {
238
+ setCurrentStepIndex(errorFieldStep);
239
+ }
240
+ }
241
+ }
242
+ } else {
243
+ setCurrentStepIndex((prev) => prev + 1);
244
+ }
245
+ },
246
+ [isLast, onSubmit, currentStepIndex, steps, completedSteps, formSchema, fieldAssignments]
247
+ );
248
+ const handleBack = React.useCallback(() => {
249
+ if (!isFirst) {
250
+ setCurrentStepIndex((prev) => prev - 1);
251
+ setSchemaErrors(null);
252
+ }
253
+ }, [isFirst]);
254
+ if (!hasMultipleSteps) {
255
+ return /* @__PURE__ */ jsxRuntime.jsxs(
256
+ index.default,
257
+ {
258
+ formSchema,
259
+ values: initialValues,
260
+ onValuesChange,
261
+ onSubmit,
262
+ fieldConfig,
263
+ className,
264
+ children: [
265
+ /* @__PURE__ */ jsxRuntime.jsx(button.Button, { type: "submit", disabled: isSubmitting, className: "w-full mt-4", children: submitButtonText }),
266
+ children
267
+ ]
268
+ }
269
+ );
270
+ }
271
+ if (!currentStepSchema || !currentStep) {
272
+ return null;
273
+ }
274
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: utils.cn("w-full", className), children: [
275
+ /* @__PURE__ */ jsxRuntime.jsx(
276
+ StepperComponent,
277
+ {
278
+ steps: steps.map((s) => ({ id: s.id, label: s.title })),
279
+ currentStepIndex,
280
+ currentStepId: currentStep.id,
281
+ onStepClick: handleStepClick,
282
+ isFirst,
283
+ isLast
284
+ }
285
+ ),
286
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mb-4", children: [
287
+ /* @__PURE__ */ jsxRuntime.jsx("h3", { className: "text-lg font-semibold", children: currentStep.title }),
288
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "text-sm text-muted-foreground", children: [
289
+ "Step ",
290
+ currentStepIndex + 1,
291
+ " of ",
292
+ steps.length
293
+ ] })
294
+ ] }),
295
+ schemaErrors && schemaErrors.issues.length > 0 && /* @__PURE__ */ jsxRuntime.jsx(
296
+ "div",
297
+ {
298
+ role: "alert",
299
+ "aria-live": "assertive",
300
+ className: "rounded-md border border-destructive/50 bg-destructive/10 p-4 mb-4",
301
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-start gap-3", children: [
302
+ /* @__PURE__ */ jsxRuntime.jsx(
303
+ "svg",
304
+ {
305
+ className: "h-5 w-5 text-destructive shrink-0 mt-0.5",
306
+ fill: "none",
307
+ viewBox: "0 0 24 24",
308
+ stroke: "currentColor",
309
+ strokeWidth: 2,
310
+ children: /* @__PURE__ */ jsxRuntime.jsx(
311
+ "path",
312
+ {
313
+ strokeLinecap: "round",
314
+ strokeLinejoin: "round",
315
+ d: "M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
316
+ }
317
+ )
318
+ }
319
+ ),
320
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1", children: [
321
+ /* @__PURE__ */ jsxRuntime.jsx("h4", { className: "text-sm font-medium text-destructive", children: "Validation Failed" }),
322
+ /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "mt-2 text-sm text-destructive/90 list-disc list-inside space-y-1", children: schemaErrors.issues.map((issue, index) => /* @__PURE__ */ jsxRuntime.jsxs("li", { children: [
323
+ issue.path.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "font-medium", children: [
324
+ issue.path.join("."),
325
+ ": "
326
+ ] }),
327
+ issue.message
328
+ ] }, index)) })
329
+ ] }),
330
+ /* @__PURE__ */ jsxRuntime.jsx(
331
+ "button",
332
+ {
333
+ type: "button",
334
+ onClick: () => setSchemaErrors(null),
335
+ className: "text-destructive/70 hover:text-destructive shrink-0",
336
+ "aria-label": "Dismiss errors",
337
+ children: /* @__PURE__ */ jsxRuntime.jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 2, children: /* @__PURE__ */ jsxRuntime.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M6 18L18 6M6 6l12 12" }) })
338
+ }
339
+ )
340
+ ] })
341
+ }
342
+ ),
343
+ /* @__PURE__ */ jsxRuntime.jsxs(
344
+ index.default,
345
+ {
346
+ formSchema: currentStepSchema,
347
+ values: currentStepValues,
348
+ onValuesChange: handleStepValuesChange,
349
+ onSubmit: handleStepSubmit,
350
+ fieldConfig: currentStepFieldConfig,
351
+ className: "space-y-4",
352
+ children: [
353
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between pt-4", children: [
354
+ /* @__PURE__ */ jsxRuntime.jsx(
355
+ button.Button,
356
+ {
357
+ type: "button",
358
+ variant: "outline",
359
+ onClick: handleBack,
360
+ disabled: isFirst || isSubmitting,
361
+ children: backButtonText
362
+ }
363
+ ),
364
+ /* @__PURE__ */ jsxRuntime.jsx(button.Button, { type: "submit", disabled: isSubmitting, children: isLast ? submitButtonText : nextButtonText })
365
+ ] }),
366
+ children
367
+ ]
368
+ },
369
+ currentStep.id
370
+ )
371
+ ] });
372
+ }
373
+
374
+ exports.SteppedAutoForm = SteppedAutoForm;