@sfxcode/nuxt-ui-formkit 1.2.1 → 1.2.2
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/README.md +18 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/components/FUAutoForm.d.vue.ts +65 -0
- package/dist/runtime/components/FUAutoForm.vue +59 -0
- package/dist/runtime/components/FUAutoForm.vue.d.ts +65 -0
- package/dist/runtime/components/FUDataEdit.d.vue.ts +1 -1
- package/dist/runtime/components/FUDataEdit.vue.d.ts +1 -1
- package/dist/runtime/components/FUDataView.d.vue.ts +1 -1
- package/dist/runtime/components/FUDataView.vue.d.ts +1 -1
- package/dist/runtime/composables/useFormKitAutoForm.d.ts +38 -0
- package/dist/runtime/composables/useFormKitAutoForm.js +349 -0
- package/dist/runtime/composables/useFormKitForm.d.ts +11 -0
- package/dist/runtime/composables/useFormKitForm.js +38 -0
- package/dist/runtime/composables/useFormKitMultiStep.d.ts +5 -0
- package/dist/runtime/composables/useFormKitMultiStep.js +65 -0
- package/dist/runtime/composables/useFormKitRepeater.d.ts +4 -0
- package/dist/runtime/composables/useFormKitRepeater.js +138 -0
- package/dist/runtime/formkit/createNuxtUiFormkitConfig.d.ts +19 -0
- package/dist/runtime/formkit/createNuxtUiFormkitConfig.js +12 -0
- package/dist/runtime/formkit/createNuxtUiFormkitConfig.mjs +12 -0
- package/dist/runtime/formkit/definitions/index.d.ts +3 -0
- package/dist/runtime/formkit/definitions/index.js +4 -0
- package/dist/runtime/formkit/definitions/index.mjs +4 -0
- package/dist/runtime/formkit/definitions/multiStep.d.ts +3 -0
- package/dist/runtime/formkit/definitions/multiStep.js +90 -0
- package/dist/runtime/formkit/definitions/multiStep.mjs +90 -0
- package/dist/runtime/formkit/definitions/repeater.js +3 -119
- package/dist/runtime/formkit/definitions/repeater.mjs +3 -119
- package/dist/runtime/formkit/index.d.ts +10 -0
- package/dist/runtime/formkit/index.js +1 -0
- package/dist/runtime/formkit/index.mjs +1 -0
- package/dist/runtime/formkit/plugins/index.d.ts +1 -0
- package/dist/runtime/formkit/plugins/index.js +1 -0
- package/dist/runtime/formkit/plugins/index.mjs +1 -0
- package/dist/runtime/plugin.js +2 -1
- package/dist/runtime/utils/dateValueConversion.d.ts +8 -7
- package/dist/runtime/utils/dateValueConversion.js +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -43,6 +43,10 @@ FormKit Nuxt UI bridges the gap between [FormKit](https://formkit.com/)'s powerf
|
|
|
43
43
|
🔁 **Repeater Component** - Dynamic repeatable form sections
|
|
44
44
|
- `nuxtUIRepeater` - Create dynamic lists with add, remove, clone, and reorder functionality
|
|
45
45
|
|
|
46
|
+
🧭 **Multi-Step Forms** - Wizard-style forms with tab navigation and validation gating
|
|
47
|
+
- `nuxtUIMultiStep` - Tab-strip wizard container built on `@formkit/addons`' `createMultiStepPlugin`
|
|
48
|
+
- `nuxtUIStep` - A single step's content, with Nuxt UI-styled previous/next actions
|
|
49
|
+
|
|
46
50
|
📊 **6 Output Components** - Display-only components for read-only data
|
|
47
51
|
- `nuxtUIOutputBoolean` - Boolean display with custom icons
|
|
48
52
|
- `nuxtUIOutputDate` - Formatted date/time display
|
|
@@ -55,11 +59,19 @@ FormKit Nuxt UI bridges the gap between [FormKit](https://formkit.com/)'s powerf
|
|
|
55
59
|
- `FUDataEdit` - Edit forms with schema-based configuration
|
|
56
60
|
- `FUDataView` - Read-only data display with schema support
|
|
57
61
|
- `FUDataDebug` - Development tool for form debugging
|
|
62
|
+
- `FUAutoForm` - Schema-free forms: inputs inferred from your data's value shapes, or a Valibot/Zod schema, with an override map for fine-tuning
|
|
63
|
+
|
|
64
|
+
⚙️ **Config Helper** - One-line `formkit.config.ts` setup
|
|
65
|
+
- `createNuxtUiFormkitConfig` - Assembles all `nuxtUIXxx` inputs/outputs and this module's plugins into `{ inputs, plugins }` you spread into your own config
|
|
58
66
|
|
|
59
67
|
🔧 **Composables & Utilities** - Reusable form logic
|
|
60
68
|
- `useFormKitSchema` - Schema-based form generation with element builders
|
|
61
69
|
- `useFormKitInput` - Input component utilities and prop handling
|
|
62
70
|
- `useFormKitOutput` - Output component utilities and prop handling
|
|
71
|
+
- `useFormKitRepeater` - Repeater insert/remove/clone/move/drag handlers
|
|
72
|
+
- `useFormKitMultiStep` - Multi-step tab-item mapping and navigation bridging
|
|
73
|
+
- `useFormKitForm` - Submit/reset/error-management wrapper for a form's imperative APIs, callable from outside the form
|
|
74
|
+
- `useFormKitAutoForm` - Schema inference from data value shapes, Valibot, or Zod schemas (`inferFormSchema`/`inferFormSchemaFromValibot`/`inferFormSchemaFromZod`)
|
|
63
75
|
- `colorConverter` - Color format conversion utilities
|
|
64
76
|
- `durationConverter` - Duration format conversion utilities
|
|
65
77
|
|
|
@@ -321,6 +333,12 @@ The playground includes comprehensive examples for all components:
|
|
|
321
333
|
- [Repeater](./playground/app/pages/form/repeater-sample.vue)
|
|
322
334
|
- [Drag-and-Drop Repeater](./playground/app/pages/form/repeater-drag.vue)
|
|
323
335
|
|
|
336
|
+
### Multi-Step
|
|
337
|
+
- [Multi-Step Form](./playground/app/pages/form/multi-step.vue)
|
|
338
|
+
|
|
339
|
+
### Form Composable
|
|
340
|
+
- [useFormKitForm](./playground/app/pages/form/form-composable.vue)
|
|
341
|
+
|
|
324
342
|
## Development
|
|
325
343
|
|
|
326
344
|
<details>
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -15,7 +15,7 @@ const module$1 = defineNuxtModule({
|
|
|
15
15
|
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
16
16
|
addImportsDir(resolver.resolve("./runtime/composables"));
|
|
17
17
|
const NPM_PCK_FORMKIT_NUXT_UI = "@sfxcode/nuxt-ui-formkit";
|
|
18
|
-
const componentNames = ["FUDataView", "FUDataEdit", "FUDataDebug"];
|
|
18
|
+
const componentNames = ["FUDataView", "FUDataEdit", "FUDataDebug", "FUAutoForm"];
|
|
19
19
|
componentNames.forEach(
|
|
20
20
|
(name) => addComponent({
|
|
21
21
|
name,
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { FormKitSchemaDefinition } from '@formkit/core';
|
|
2
|
+
import type { PropType } from 'vue';
|
|
3
|
+
import type { AutoFormOverrides, ValibotLikeSchema, ZodLikeSchema } from '../composables/useFormKitAutoForm.js';
|
|
4
|
+
declare var __VLS_10: string, __VLS_11: any;
|
|
5
|
+
type __VLS_Slots = {} & {
|
|
6
|
+
[K in NonNullable<typeof __VLS_10>]?: (props: typeof __VLS_11) => any;
|
|
7
|
+
};
|
|
8
|
+
declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
9
|
+
data: {
|
|
10
|
+
type: ObjectConstructor;
|
|
11
|
+
default: null;
|
|
12
|
+
};
|
|
13
|
+
overrides: {
|
|
14
|
+
type: PropType<AutoFormOverrides>;
|
|
15
|
+
default: undefined;
|
|
16
|
+
};
|
|
17
|
+
valibotSchema: {
|
|
18
|
+
type: PropType<ValibotLikeSchema>;
|
|
19
|
+
default: undefined;
|
|
20
|
+
};
|
|
21
|
+
zodSchema: {
|
|
22
|
+
type: PropType<ZodLikeSchema>;
|
|
23
|
+
default: undefined;
|
|
24
|
+
};
|
|
25
|
+
modelValue: {
|
|
26
|
+
type: PropType<FormKitSchemaDefinition>;
|
|
27
|
+
};
|
|
28
|
+
}>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
29
|
+
"update:modelValue": (value: FormKitSchemaDefinition | undefined) => any;
|
|
30
|
+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
31
|
+
data: {
|
|
32
|
+
type: ObjectConstructor;
|
|
33
|
+
default: null;
|
|
34
|
+
};
|
|
35
|
+
overrides: {
|
|
36
|
+
type: PropType<AutoFormOverrides>;
|
|
37
|
+
default: undefined;
|
|
38
|
+
};
|
|
39
|
+
valibotSchema: {
|
|
40
|
+
type: PropType<ValibotLikeSchema>;
|
|
41
|
+
default: undefined;
|
|
42
|
+
};
|
|
43
|
+
zodSchema: {
|
|
44
|
+
type: PropType<ZodLikeSchema>;
|
|
45
|
+
default: undefined;
|
|
46
|
+
};
|
|
47
|
+
modelValue: {
|
|
48
|
+
type: PropType<FormKitSchemaDefinition>;
|
|
49
|
+
};
|
|
50
|
+
}>> & Readonly<{
|
|
51
|
+
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
52
|
+
}>, {
|
|
53
|
+
data: Record<string, any>;
|
|
54
|
+
overrides: AutoFormOverrides;
|
|
55
|
+
valibotSchema: ValibotLikeSchema;
|
|
56
|
+
zodSchema: ZodLikeSchema;
|
|
57
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
58
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
59
|
+
declare const _default: typeof __VLS_export;
|
|
60
|
+
export default _default;
|
|
61
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
62
|
+
new (): {
|
|
63
|
+
$slots: S;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { ref, watch } from "vue";
|
|
3
|
+
import { inferFormSchema, inferFormSchemaFromValibot, inferFormSchemaFromZod } from "../composables/useFormKitAutoForm";
|
|
4
|
+
import FUDataEdit from "./FUDataEdit.vue";
|
|
5
|
+
defineOptions({ inheritAttrs: false });
|
|
6
|
+
const props = defineProps({
|
|
7
|
+
data: {
|
|
8
|
+
type: Object,
|
|
9
|
+
default: null
|
|
10
|
+
},
|
|
11
|
+
overrides: {
|
|
12
|
+
type: Object,
|
|
13
|
+
default: void 0
|
|
14
|
+
},
|
|
15
|
+
valibotSchema: {
|
|
16
|
+
type: Object,
|
|
17
|
+
default: void 0
|
|
18
|
+
},
|
|
19
|
+
zodSchema: {
|
|
20
|
+
type: Object,
|
|
21
|
+
default: void 0
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const formData = defineModel({ type: [Object, String, Array] });
|
|
25
|
+
if (!formData.value && props.data) {
|
|
26
|
+
formData.value = props.data;
|
|
27
|
+
}
|
|
28
|
+
function buildSchema(data) {
|
|
29
|
+
if (props.valibotSchema) {
|
|
30
|
+
return inferFormSchemaFromValibot(props.valibotSchema, props.overrides);
|
|
31
|
+
}
|
|
32
|
+
if (props.zodSchema) {
|
|
33
|
+
return inferFormSchemaFromZod(props.zodSchema, props.overrides);
|
|
34
|
+
}
|
|
35
|
+
return inferFormSchema(data ?? formData.value ?? {}, props.overrides);
|
|
36
|
+
}
|
|
37
|
+
const formSchema = ref(buildSchema(props.data));
|
|
38
|
+
watch([() => props.data, () => props.overrides, () => props.valibotSchema, () => props.zodSchema], ([data]) => {
|
|
39
|
+
formSchema.value = buildSchema(data);
|
|
40
|
+
});
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<template>
|
|
44
|
+
<FUDataEdit
|
|
45
|
+
v-bind="$attrs"
|
|
46
|
+
v-model="formData"
|
|
47
|
+
:schema="formSchema"
|
|
48
|
+
>
|
|
49
|
+
<template
|
|
50
|
+
v-for="(_, name) in $slots"
|
|
51
|
+
#[name]="slotProps"
|
|
52
|
+
>
|
|
53
|
+
<slot
|
|
54
|
+
:name="name"
|
|
55
|
+
v-bind="slotProps ?? {}"
|
|
56
|
+
/>
|
|
57
|
+
</template>
|
|
58
|
+
</FUDataEdit>
|
|
59
|
+
</template>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { FormKitSchemaDefinition } from '@formkit/core';
|
|
2
|
+
import type { PropType } from 'vue';
|
|
3
|
+
import type { AutoFormOverrides, ValibotLikeSchema, ZodLikeSchema } from '../composables/useFormKitAutoForm.js';
|
|
4
|
+
declare var __VLS_10: string, __VLS_11: any;
|
|
5
|
+
type __VLS_Slots = {} & {
|
|
6
|
+
[K in NonNullable<typeof __VLS_10>]?: (props: typeof __VLS_11) => any;
|
|
7
|
+
};
|
|
8
|
+
declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
9
|
+
data: {
|
|
10
|
+
type: ObjectConstructor;
|
|
11
|
+
default: null;
|
|
12
|
+
};
|
|
13
|
+
overrides: {
|
|
14
|
+
type: PropType<AutoFormOverrides>;
|
|
15
|
+
default: undefined;
|
|
16
|
+
};
|
|
17
|
+
valibotSchema: {
|
|
18
|
+
type: PropType<ValibotLikeSchema>;
|
|
19
|
+
default: undefined;
|
|
20
|
+
};
|
|
21
|
+
zodSchema: {
|
|
22
|
+
type: PropType<ZodLikeSchema>;
|
|
23
|
+
default: undefined;
|
|
24
|
+
};
|
|
25
|
+
modelValue: {
|
|
26
|
+
type: PropType<FormKitSchemaDefinition>;
|
|
27
|
+
};
|
|
28
|
+
}>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
29
|
+
"update:modelValue": (value: FormKitSchemaDefinition | undefined) => any;
|
|
30
|
+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
31
|
+
data: {
|
|
32
|
+
type: ObjectConstructor;
|
|
33
|
+
default: null;
|
|
34
|
+
};
|
|
35
|
+
overrides: {
|
|
36
|
+
type: PropType<AutoFormOverrides>;
|
|
37
|
+
default: undefined;
|
|
38
|
+
};
|
|
39
|
+
valibotSchema: {
|
|
40
|
+
type: PropType<ValibotLikeSchema>;
|
|
41
|
+
default: undefined;
|
|
42
|
+
};
|
|
43
|
+
zodSchema: {
|
|
44
|
+
type: PropType<ZodLikeSchema>;
|
|
45
|
+
default: undefined;
|
|
46
|
+
};
|
|
47
|
+
modelValue: {
|
|
48
|
+
type: PropType<FormKitSchemaDefinition>;
|
|
49
|
+
};
|
|
50
|
+
}>> & Readonly<{
|
|
51
|
+
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
52
|
+
}>, {
|
|
53
|
+
data: Record<string, any>;
|
|
54
|
+
overrides: AutoFormOverrides;
|
|
55
|
+
valibotSchema: ValibotLikeSchema;
|
|
56
|
+
zodSchema: ZodLikeSchema;
|
|
57
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
58
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
59
|
+
declare const _default: typeof __VLS_export;
|
|
60
|
+
export default _default;
|
|
61
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
62
|
+
new (): {
|
|
63
|
+
$slots: S;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
@@ -149,9 +149,9 @@ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPro
|
|
|
149
149
|
}>> & Readonly<{
|
|
150
150
|
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
151
151
|
}>, {
|
|
152
|
+
schema: FormKitSchemaDefinition;
|
|
152
153
|
data: Record<string, any>;
|
|
153
154
|
id: string;
|
|
154
|
-
schema: FormKitSchemaDefinition;
|
|
155
155
|
formClass: string;
|
|
156
156
|
actionsClass: string;
|
|
157
157
|
submitSeverity: string;
|
|
@@ -149,9 +149,9 @@ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPro
|
|
|
149
149
|
}>> & Readonly<{
|
|
150
150
|
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
151
151
|
}>, {
|
|
152
|
+
schema: FormKitSchemaDefinition;
|
|
152
153
|
data: Record<string, any>;
|
|
153
154
|
id: string;
|
|
154
|
-
schema: FormKitSchemaDefinition;
|
|
155
155
|
formClass: string;
|
|
156
156
|
actionsClass: string;
|
|
157
157
|
submitSeverity: string;
|
|
@@ -57,8 +57,8 @@ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPro
|
|
|
57
57
|
}>> & Readonly<{
|
|
58
58
|
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
59
59
|
}>, {
|
|
60
|
-
data: Record<string, any>;
|
|
61
60
|
schema: FormKitSchemaDefinition;
|
|
61
|
+
data: Record<string, any>;
|
|
62
62
|
formClass: string;
|
|
63
63
|
debugData: boolean;
|
|
64
64
|
debugSchema: boolean;
|
|
@@ -57,8 +57,8 @@ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPro
|
|
|
57
57
|
}>> & Readonly<{
|
|
58
58
|
"onUpdate:modelValue"?: ((value: FormKitSchemaDefinition | undefined) => any) | undefined;
|
|
59
59
|
}>, {
|
|
60
|
-
data: Record<string, any>;
|
|
61
60
|
schema: FormKitSchemaDefinition;
|
|
61
|
+
data: Record<string, any>;
|
|
62
62
|
formClass: string;
|
|
63
63
|
debugData: boolean;
|
|
64
64
|
debugSchema: boolean;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FormKitSchemaNode } from '@formkit/core';
|
|
2
|
+
export type AutoFormOverrides = Record<string, Record<string, unknown> | boolean>;
|
|
3
|
+
export declare function inferFormSchema(data: Record<string, unknown>, overrides?: AutoFormOverrides): FormKitSchemaNode[];
|
|
4
|
+
export interface ValibotLikePipeEntry {
|
|
5
|
+
kind: string;
|
|
6
|
+
type: string;
|
|
7
|
+
requirement?: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface ValibotLikeSchema {
|
|
10
|
+
kind: 'schema';
|
|
11
|
+
type: string;
|
|
12
|
+
entries?: Record<string, ValibotLikeSchema>;
|
|
13
|
+
item?: ValibotLikeSchema;
|
|
14
|
+
wrapped?: ValibotLikeSchema;
|
|
15
|
+
default?: unknown;
|
|
16
|
+
pipe?: readonly ValibotLikePipeEntry[];
|
|
17
|
+
}
|
|
18
|
+
export declare function inferFormSchemaFromValibot(schema: object, overrides?: AutoFormOverrides): FormKitSchemaNode[];
|
|
19
|
+
export interface ZodLikeSchema {
|
|
20
|
+
type: string;
|
|
21
|
+
shape?: Record<string, ZodLikeSchema>;
|
|
22
|
+
element?: ZodLikeSchema;
|
|
23
|
+
minLength?: number | null;
|
|
24
|
+
maxLength?: number | null;
|
|
25
|
+
minValue?: number | null;
|
|
26
|
+
maxValue?: number | null;
|
|
27
|
+
format?: string | null;
|
|
28
|
+
unwrap?: () => ZodLikeSchema;
|
|
29
|
+
def?: {
|
|
30
|
+
defaultValue?: unknown;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export declare function inferFormSchemaFromZod(schema: object, overrides?: AutoFormOverrides): FormKitSchemaNode[];
|
|
34
|
+
export declare function useFormKitAutoForm(): {
|
|
35
|
+
inferFormSchema: typeof inferFormSchema;
|
|
36
|
+
inferFormSchemaFromValibot: typeof inferFormSchemaFromValibot;
|
|
37
|
+
inferFormSchemaFromZod: typeof inferFormSchemaFromZod;
|
|
38
|
+
};
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { isDateValue } from "../utils/dateValueConversion.js";
|
|
2
|
+
const REPEATER_BUTTON_DEFAULTS = {
|
|
3
|
+
displayCloneButton: true,
|
|
4
|
+
displayDeleteButton: true,
|
|
5
|
+
buttonGroupClass: "flex gap-2 justify-end"
|
|
6
|
+
};
|
|
7
|
+
const ISO_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
8
|
+
function isIsoDateString(value) {
|
|
9
|
+
return ISO_DATE_PATTERN.test(value) && !Number.isNaN(Date.parse(value));
|
|
10
|
+
}
|
|
11
|
+
function humanizeKey(key) {
|
|
12
|
+
return key.replace(/[_-]+/g, " ").replace(/([a-z\d])([A-Z])/g, "$1 $2").split(" ").filter(Boolean).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
13
|
+
}
|
|
14
|
+
function isPlainObject(value) {
|
|
15
|
+
if (typeof value !== "object" || value === null) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const prototype = Object.getPrototypeOf(value);
|
|
19
|
+
return prototype === Object.prototype || prototype === null;
|
|
20
|
+
}
|
|
21
|
+
function blankValue(value) {
|
|
22
|
+
if (typeof value === "boolean") {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (typeof value === "number") {
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
return isIsoDateString(value) ? null : "";
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
if (isPlainObject(value)) {
|
|
35
|
+
return blankItem(value);
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function blankItem(item) {
|
|
40
|
+
const blank = {};
|
|
41
|
+
for (const [key, value] of Object.entries(item)) {
|
|
42
|
+
blank[key] = blankValue(value);
|
|
43
|
+
}
|
|
44
|
+
return blank;
|
|
45
|
+
}
|
|
46
|
+
function inferNode(name, value, overrides, path) {
|
|
47
|
+
const label = humanizeKey(name);
|
|
48
|
+
if (typeof value === "boolean") {
|
|
49
|
+
return { $formkit: "nuxtUISwitch", name, label };
|
|
50
|
+
}
|
|
51
|
+
if (typeof value === "number") {
|
|
52
|
+
return { $formkit: "nuxtUIInputNumber", name, label };
|
|
53
|
+
}
|
|
54
|
+
if (value instanceof Date) {
|
|
55
|
+
return { $formkit: "nuxtUIInputDate", name, label, valueType: "date" };
|
|
56
|
+
}
|
|
57
|
+
if (isDateValue(value)) {
|
|
58
|
+
return { $formkit: "nuxtUIInputDate", name, label, valueType: "calendar" };
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
if (isIsoDateString(value)) {
|
|
62
|
+
return { $formkit: "nuxtUIInputDate", name, label, valueType: "iso" };
|
|
63
|
+
}
|
|
64
|
+
if (value.includes("\n")) {
|
|
65
|
+
return { $formkit: "nuxtUITextarea", name, label };
|
|
66
|
+
}
|
|
67
|
+
return { $formkit: "nuxtUIInput", name, label };
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(value)) {
|
|
70
|
+
const [first] = value;
|
|
71
|
+
if (isPlainObject(first)) {
|
|
72
|
+
return {
|
|
73
|
+
$formkit: "nuxtUIRepeater",
|
|
74
|
+
name,
|
|
75
|
+
label,
|
|
76
|
+
newItem: blankItem(first),
|
|
77
|
+
children: inferNodes(first, overrides, path),
|
|
78
|
+
...REPEATER_BUTTON_DEFAULTS
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return { $formkit: "nuxtUIInputTags", name, label };
|
|
82
|
+
}
|
|
83
|
+
if (isPlainObject(value)) {
|
|
84
|
+
return { $formkit: "group", name, children: inferNodes(value, overrides, path) };
|
|
85
|
+
}
|
|
86
|
+
return void 0;
|
|
87
|
+
}
|
|
88
|
+
function isDirectChildPath(path, prefix) {
|
|
89
|
+
if (!prefix) {
|
|
90
|
+
return !path.includes(".");
|
|
91
|
+
}
|
|
92
|
+
return path.startsWith(`${prefix}.`) && !path.slice(prefix.length + 1).includes(".");
|
|
93
|
+
}
|
|
94
|
+
function buildNodes(source, inferEntry, overrides, pathPrefix = "") {
|
|
95
|
+
const nodes = [];
|
|
96
|
+
const consumedPaths = /* @__PURE__ */ new Set();
|
|
97
|
+
for (const [key, value] of Object.entries(source)) {
|
|
98
|
+
const path = pathPrefix ? `${pathPrefix}.${key}` : key;
|
|
99
|
+
const override = overrides?.[path];
|
|
100
|
+
if (override !== void 0) {
|
|
101
|
+
consumedPaths.add(path);
|
|
102
|
+
}
|
|
103
|
+
if (override === false) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const node = inferEntry(key, value, path);
|
|
107
|
+
if (override && override !== true) {
|
|
108
|
+
nodes.push({ $formkit: "nuxtUIInput", name: key, label: humanizeKey(key), ...node, ...override });
|
|
109
|
+
} else if (node) {
|
|
110
|
+
nodes.push(node);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (overrides) {
|
|
114
|
+
for (const [path, override] of Object.entries(overrides)) {
|
|
115
|
+
if (typeof override === "boolean" || consumedPaths.has(path) || !isDirectChildPath(path, pathPrefix)) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const key = pathPrefix ? path.slice(pathPrefix.length + 1) : path;
|
|
119
|
+
nodes.push({ $formkit: "nuxtUIInput", name: key, label: humanizeKey(key), ...override });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return nodes;
|
|
123
|
+
}
|
|
124
|
+
function inferNodes(data, overrides, pathPrefix = "") {
|
|
125
|
+
return buildNodes(data, (key, value, path) => inferNode(key, value, overrides, path), overrides, pathPrefix);
|
|
126
|
+
}
|
|
127
|
+
export function inferFormSchema(data, overrides) {
|
|
128
|
+
return inferNodes(data, overrides);
|
|
129
|
+
}
|
|
130
|
+
const OPTIONAL_WRAPPER_TYPES = /* @__PURE__ */ new Set(["optional", "nullable", "nullish", "exact_optional"]);
|
|
131
|
+
function unwrapValibot(entry) {
|
|
132
|
+
let schema = entry;
|
|
133
|
+
let required = true;
|
|
134
|
+
while (OPTIONAL_WRAPPER_TYPES.has(schema.type) && schema.wrapped) {
|
|
135
|
+
required = false;
|
|
136
|
+
schema = schema.wrapped;
|
|
137
|
+
}
|
|
138
|
+
return { schema, required };
|
|
139
|
+
}
|
|
140
|
+
function deriveValibotValidation(schema, required) {
|
|
141
|
+
const rules = [];
|
|
142
|
+
if (required && schema.type !== "boolean") {
|
|
143
|
+
rules.push("required");
|
|
144
|
+
}
|
|
145
|
+
let minLength;
|
|
146
|
+
let maxLength;
|
|
147
|
+
for (const entry of schema.pipe ?? []) {
|
|
148
|
+
if (entry.kind !== "validation") {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (entry.type === "email" || entry.type === "url") {
|
|
152
|
+
rules.push(entry.type);
|
|
153
|
+
} else if (entry.type === "min_length" && typeof entry.requirement === "number") {
|
|
154
|
+
minLength = entry.requirement;
|
|
155
|
+
} else if (entry.type === "max_length" && typeof entry.requirement === "number") {
|
|
156
|
+
maxLength = entry.requirement;
|
|
157
|
+
} else if (entry.type === "min_value" && typeof entry.requirement === "number") {
|
|
158
|
+
rules.push(`min:${entry.requirement}`);
|
|
159
|
+
} else if (entry.type === "max_value" && typeof entry.requirement === "number") {
|
|
160
|
+
rules.push(`max:${entry.requirement}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (maxLength !== void 0) {
|
|
164
|
+
rules.push(`length:${minLength ?? 0},${maxLength}`);
|
|
165
|
+
} else if (minLength !== void 0) {
|
|
166
|
+
rules.push(`length:${minLength}`);
|
|
167
|
+
}
|
|
168
|
+
return rules.length ? rules.join("|") : void 0;
|
|
169
|
+
}
|
|
170
|
+
function blankValueFromValibot(entry) {
|
|
171
|
+
if (entry.default !== void 0) {
|
|
172
|
+
return entry.default;
|
|
173
|
+
}
|
|
174
|
+
const { schema } = unwrapValibot(entry);
|
|
175
|
+
switch (schema.type) {
|
|
176
|
+
case "string":
|
|
177
|
+
return "";
|
|
178
|
+
case "number":
|
|
179
|
+
return 0;
|
|
180
|
+
case "boolean":
|
|
181
|
+
return false;
|
|
182
|
+
case "array":
|
|
183
|
+
return [];
|
|
184
|
+
case "object":
|
|
185
|
+
return schema.entries ? blankItemFromValibot(schema.entries) : {};
|
|
186
|
+
default:
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function blankItemFromValibot(entries) {
|
|
191
|
+
const blank = {};
|
|
192
|
+
for (const [key, entry] of Object.entries(entries)) {
|
|
193
|
+
blank[key] = blankValueFromValibot(entry);
|
|
194
|
+
}
|
|
195
|
+
return blank;
|
|
196
|
+
}
|
|
197
|
+
function inferValibotNode(name, entry, overrides, path) {
|
|
198
|
+
const { schema, required } = unwrapValibot(entry);
|
|
199
|
+
const label = humanizeKey(name);
|
|
200
|
+
const validation = deriveValibotValidation(schema, required);
|
|
201
|
+
const base = validation ? { name, label, validation } : { name, label };
|
|
202
|
+
switch (schema.type) {
|
|
203
|
+
case "string":
|
|
204
|
+
return { $formkit: "nuxtUIInput", ...base };
|
|
205
|
+
case "number":
|
|
206
|
+
return { $formkit: "nuxtUIInputNumber", ...base };
|
|
207
|
+
case "boolean":
|
|
208
|
+
return { $formkit: "nuxtUISwitch", ...base };
|
|
209
|
+
case "date":
|
|
210
|
+
return { $formkit: "nuxtUIInputDate", ...base, valueType: "date" };
|
|
211
|
+
case "array": {
|
|
212
|
+
const item = schema.item ? unwrapValibot(schema.item).schema : void 0;
|
|
213
|
+
if (item?.type === "object" && item.entries) {
|
|
214
|
+
return {
|
|
215
|
+
$formkit: "nuxtUIRepeater",
|
|
216
|
+
...base,
|
|
217
|
+
newItem: blankItemFromValibot(item.entries),
|
|
218
|
+
children: inferValibotNodes(item.entries, overrides, path),
|
|
219
|
+
...REPEATER_BUTTON_DEFAULTS
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
return { $formkit: "nuxtUIInputTags", ...base };
|
|
223
|
+
}
|
|
224
|
+
case "object":
|
|
225
|
+
return schema.entries ? { $formkit: "group", name, children: inferValibotNodes(schema.entries, overrides, path) } : void 0;
|
|
226
|
+
default:
|
|
227
|
+
return void 0;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function inferValibotNodes(entries, overrides, pathPrefix = "") {
|
|
231
|
+
return buildNodes(entries, (key, entry, path) => inferValibotNode(key, entry, overrides, path), overrides, pathPrefix);
|
|
232
|
+
}
|
|
233
|
+
export function inferFormSchemaFromValibot(schema, overrides) {
|
|
234
|
+
const root = schema;
|
|
235
|
+
if (root.kind !== "schema" || root.type !== "object" || !root.entries) {
|
|
236
|
+
return [];
|
|
237
|
+
}
|
|
238
|
+
return inferValibotNodes(root.entries, overrides);
|
|
239
|
+
}
|
|
240
|
+
const ZOD_UNWRAPPABLE_TYPES = /* @__PURE__ */ new Set(["optional", "nullable", "nullish", "default"]);
|
|
241
|
+
function unwrapZod(entry) {
|
|
242
|
+
let schema = entry;
|
|
243
|
+
let required = true;
|
|
244
|
+
while (ZOD_UNWRAPPABLE_TYPES.has(schema.type) && typeof schema.unwrap === "function") {
|
|
245
|
+
required = false;
|
|
246
|
+
schema = schema.unwrap();
|
|
247
|
+
}
|
|
248
|
+
return { schema, required };
|
|
249
|
+
}
|
|
250
|
+
function deriveZodValidation(schema, required) {
|
|
251
|
+
const rules = [];
|
|
252
|
+
if (required && schema.type !== "boolean") {
|
|
253
|
+
rules.push("required");
|
|
254
|
+
}
|
|
255
|
+
if (schema.format === "email" || schema.format === "url") {
|
|
256
|
+
rules.push(schema.format);
|
|
257
|
+
}
|
|
258
|
+
if (Number.isFinite(schema.minValue)) {
|
|
259
|
+
rules.push(`min:${schema.minValue}`);
|
|
260
|
+
}
|
|
261
|
+
if (Number.isFinite(schema.maxValue)) {
|
|
262
|
+
rules.push(`max:${schema.maxValue}`);
|
|
263
|
+
}
|
|
264
|
+
if (typeof schema.maxLength === "number") {
|
|
265
|
+
rules.push(`length:${schema.minLength ?? 0},${schema.maxLength}`);
|
|
266
|
+
} else if (typeof schema.minLength === "number") {
|
|
267
|
+
rules.push(`length:${schema.minLength}`);
|
|
268
|
+
}
|
|
269
|
+
return rules.length ? rules.join("|") : void 0;
|
|
270
|
+
}
|
|
271
|
+
function blankValueFromZod(entry) {
|
|
272
|
+
let schema = entry;
|
|
273
|
+
while (true) {
|
|
274
|
+
if (schema.def?.defaultValue !== void 0) {
|
|
275
|
+
return schema.def.defaultValue;
|
|
276
|
+
}
|
|
277
|
+
if (!ZOD_UNWRAPPABLE_TYPES.has(schema.type) || typeof schema.unwrap !== "function") {
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
schema = schema.unwrap();
|
|
281
|
+
}
|
|
282
|
+
switch (schema.type) {
|
|
283
|
+
case "string":
|
|
284
|
+
return "";
|
|
285
|
+
case "number":
|
|
286
|
+
return 0;
|
|
287
|
+
case "boolean":
|
|
288
|
+
return false;
|
|
289
|
+
case "array":
|
|
290
|
+
return [];
|
|
291
|
+
case "object":
|
|
292
|
+
return schema.shape ? blankItemFromZod(schema.shape) : {};
|
|
293
|
+
default:
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function blankItemFromZod(shape) {
|
|
298
|
+
const blank = {};
|
|
299
|
+
for (const [key, entry] of Object.entries(shape)) {
|
|
300
|
+
blank[key] = blankValueFromZod(entry);
|
|
301
|
+
}
|
|
302
|
+
return blank;
|
|
303
|
+
}
|
|
304
|
+
function inferZodNode(name, entry, overrides, path) {
|
|
305
|
+
const { schema, required } = unwrapZod(entry);
|
|
306
|
+
const label = humanizeKey(name);
|
|
307
|
+
const validation = deriveZodValidation(schema, required);
|
|
308
|
+
const base = validation ? { name, label, validation } : { name, label };
|
|
309
|
+
switch (schema.type) {
|
|
310
|
+
case "string":
|
|
311
|
+
return { $formkit: "nuxtUIInput", ...base };
|
|
312
|
+
case "number":
|
|
313
|
+
return { $formkit: "nuxtUIInputNumber", ...base };
|
|
314
|
+
case "boolean":
|
|
315
|
+
return { $formkit: "nuxtUISwitch", ...base };
|
|
316
|
+
case "date":
|
|
317
|
+
return { $formkit: "nuxtUIInputDate", ...base, valueType: "date" };
|
|
318
|
+
case "array": {
|
|
319
|
+
const item = schema.element ? unwrapZod(schema.element).schema : void 0;
|
|
320
|
+
if (item?.type === "object" && item.shape) {
|
|
321
|
+
return {
|
|
322
|
+
$formkit: "nuxtUIRepeater",
|
|
323
|
+
...base,
|
|
324
|
+
newItem: blankItemFromZod(item.shape),
|
|
325
|
+
children: inferZodNodes(item.shape, overrides, path),
|
|
326
|
+
...REPEATER_BUTTON_DEFAULTS
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
return { $formkit: "nuxtUIInputTags", ...base };
|
|
330
|
+
}
|
|
331
|
+
case "object":
|
|
332
|
+
return schema.shape ? { $formkit: "group", name, children: inferZodNodes(schema.shape, overrides, path) } : void 0;
|
|
333
|
+
default:
|
|
334
|
+
return void 0;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function inferZodNodes(shape, overrides, pathPrefix = "") {
|
|
338
|
+
return buildNodes(shape, (key, entry, path) => inferZodNode(key, entry, overrides, path), overrides, pathPrefix);
|
|
339
|
+
}
|
|
340
|
+
export function inferFormSchemaFromZod(schema, overrides) {
|
|
341
|
+
const root = schema;
|
|
342
|
+
if (root.type !== "object" || !root.shape) {
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
return inferZodNodes(root.shape, overrides);
|
|
346
|
+
}
|
|
347
|
+
export function useFormKitAutoForm() {
|
|
348
|
+
return { inferFormSchema, inferFormSchemaFromValibot, inferFormSchemaFromZod };
|
|
349
|
+
}
|