@moonmangit/vue-autoform 0.1.4 → 0.1.7
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 +72 -2
- package/dist/style.css +1 -1
- package/dist/vue-autoform.cjs.js +4 -4
- package/dist/vue-autoform.cjs.js.map +1 -1
- package/dist/vue-autoform.d.ts +2 -62
- package/dist/vue-autoform.es.js +134 -232
- package/dist/vue-autoform.es.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -149,11 +149,81 @@ defineEmits<{ (e: "update:modelValue", v: string): void; (e: "blur"): void }>();
|
|
|
149
149
|
|
|
150
150
|
```ts
|
|
151
151
|
type FieldConfig = {
|
|
152
|
-
component: Component;
|
|
153
|
-
|
|
152
|
+
component: Component;
|
|
153
|
+
// plain object OR a getter function (for reactive/async values)
|
|
154
|
+
props?: Record<string, unknown> | (() => Record<string, unknown>);
|
|
154
155
|
};
|
|
155
156
|
```
|
|
156
157
|
|
|
158
|
+
### Async options (props as a getter function)
|
|
159
|
+
|
|
160
|
+
When your field needs options loaded from a server, pass `props` as a **getter function** instead of a plain object. The function is called at render time so reactive refs inside it stay live — no need to rebuild the `fields` object when data arrives.
|
|
161
|
+
|
|
162
|
+
```vue
|
|
163
|
+
<script setup lang="ts">
|
|
164
|
+
import { ref } from "vue";
|
|
165
|
+
import { z } from "zod";
|
|
166
|
+
import { AutoForm } from "@moonmangit/vue-autoform";
|
|
167
|
+
import SelectInput from "./components/SelectInput.vue";
|
|
168
|
+
import TextInput from "./components/TextInput.vue";
|
|
169
|
+
|
|
170
|
+
const loading = ref(false);
|
|
171
|
+
const options = ref<{ value: string; label: string }[]>([]);
|
|
172
|
+
|
|
173
|
+
// Fetch however you like — the library doesn't care
|
|
174
|
+
async function loadOptions() {
|
|
175
|
+
loading.value = true;
|
|
176
|
+
const res = await fetch("/api/frameworks");
|
|
177
|
+
options.value = await res.json();
|
|
178
|
+
loading.value = false;
|
|
179
|
+
}
|
|
180
|
+
loadOptions();
|
|
181
|
+
|
|
182
|
+
const schema = z.object({
|
|
183
|
+
name: z.string().min(1),
|
|
184
|
+
framework: z.string().min(1, "Please select a framework"),
|
|
185
|
+
// array-of-object field — Zod validates each item's shape
|
|
186
|
+
tags: z.array(z.object({ value: z.string(), label: z.string() })).min(1),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const fields = {
|
|
190
|
+
name: { component: TextInput, props: { label: "Name" } },
|
|
191
|
+
|
|
192
|
+
framework: {
|
|
193
|
+
component: SelectInput,
|
|
194
|
+
// getter — reads `loading` and `options` reactively at render time
|
|
195
|
+
props: () => ({
|
|
196
|
+
label: "Framework",
|
|
197
|
+
disabled: loading.value,
|
|
198
|
+
options: options.value,
|
|
199
|
+
}),
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
tags: {
|
|
203
|
+
component: SelectInput,
|
|
204
|
+
props: () => ({
|
|
205
|
+
label: "Tags",
|
|
206
|
+
disabled: loading.value,
|
|
207
|
+
options: options.value,
|
|
208
|
+
}),
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const formData = ref({ name: "", framework: "", tags: [] });
|
|
213
|
+
</script>
|
|
214
|
+
|
|
215
|
+
<template>
|
|
216
|
+
<AutoForm v-model="formData" :schema="schema" :fields="fields" />
|
|
217
|
+
</template>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**Key points:**
|
|
221
|
+
|
|
222
|
+
- Your component receives `disabled` and `options` as normal props — the library passes them through unchanged
|
|
223
|
+
- `tags` value is whatever your component emits via `update:modelValue` — pass an array of objects, receive an array of objects
|
|
224
|
+
- Zod validates the array contents exactly as defined in the schema
|
|
225
|
+
- The library has **no opinion on how you fetch** — use `fetch`, Axios, TanStack Query, Pinia, anything
|
|
226
|
+
|
|
157
227
|
---
|
|
158
228
|
|
|
159
229
|
## Layout
|
package/dist/style.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
.autoform{width:100
|
|
1
|
+
.autoform{width:100%}.autoform-flat-grid{display:grid;grid-template-columns:repeat(1,minmax(0,1fr));column-gap:var(--autoform-gap, 1rem);row-gap:var(--autoform-row-gap, 1rem);width:100%}.autoform-flat-grid .autoform-cell{min-width:0}.autoform-default-input{width:100%;padding:.5rem .75rem;border:1px solid #d1d5db;border-radius:.375rem;font-size:1rem;outline:none;box-sizing:border-box}.autoform-default-input:focus{border-color:#6366f1;box-shadow:0 0 0 2px #6366f133}
|
package/dist/vue-autoform.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue")
|
|
2
|
-
`):
|
|
3
|
-
`)};t.push(
|
|
4
|
-
`)});return
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue");function j(a,v,F,n){const d=e.reactive({}),$=e.computed(()=>Object.keys(a.shape));function m(o,s){var t;const i=a.shape;if(!(o in i))return;const u=i[o].safeParse(s);if(!u.success)return((t=u.error.errors[0])==null?void 0:t.message)??"Invalid value"}function E(){const o=a.safeParse(v.value);return o.success?(Object.keys(d).forEach(s=>delete d[s]),!0):(o.error.errors.forEach(s=>{const i=String(s.path[0]);d[i]=s.message}),!1)}const p={};function g(o,s){p[o]=s;const i={...v.value,[o]:s};if(F("update:modelValue",i),n.value==="input"){const r=m(o,s);r?d[o]=r:delete d[o]}}function V(o){if(n.value==="blur"){const s=o in p?p[o]:v.value[o],i=m(o,s);i?d[o]=i:delete d[o]}}return{errors:d,schemaKeys:$,onFieldInput:g,onFieldBlur:V,validateAll:E}}const C={sm:640,md:768,lg:1024,xl:1280,"2xl":1536};function O(a){return typeof a.default=="number"}function x(a){return Array.isArray(a.default)}const N=["data-autoform-id"],w={class:"autoform-flat-grid"},D=["data-field-key"],R=["value","onInput","onBlur"],U=e.defineComponent({__name:"AutoForm",props:{schema:{},layout:{},fields:{},modelValue:{},validateOn:{default:"blur"},breakpoints:{}},emits:["update:modelValue"],setup(a,{expose:v,emit:F}){const n=a,d=F,$=e.ref(`autoform-${Math.random().toString(36).slice(2,9)}`),{errors:m,schemaKeys:E,onFieldInput:p,onFieldBlur:g,validateAll:V}=j(n.schema,e.toRef(n,"modelValue"),(r,u)=>d(r,u),e.toRef(n,"validateOn")),o=e.computed(()=>n.layout?x(n.layout):!0);function s(r){const u=n.fields[r];if(!u)return{};const t=u.props;return typeof t=="function"?t():t??{}}const i=e.computed(()=>{if(!n.layout)return"";const r={...C,...n.breakpoints},u=$.value,t=[];if(O(n.layout)){const l=n.layout;t.push(`[data-autoform-id="${u}"] .autoform-flat-grid {`,` grid-template-columns: repeat(${l.default}, minmax(0, 1fr));`,"}");const S=Object.keys(l).filter(B=>B!=="default");for(const B of S){const b=r[B],c=l[B];b&&c&&t.push(`@media (min-width: ${b}px) {`,` [data-autoform-id="${u}"] .autoform-flat-grid {`,` grid-template-columns: repeat(${c}, minmax(0, 1fr));`," }","}")}}else if(x(n.layout)){const l=n.layout,S=(c,h)=>{const y=Math.max(...c.map(f=>f.length)),L=E.value,P=new Set(c.flat()),k=[];k.push(`[data-autoform-id="${u}"] .autoform-flat-grid { grid-template-columns: repeat(${y}, minmax(0, 1fr)); }`),L.forEach(f=>{P.has(f)||k.push(`[data-autoform-id="${u}"] [data-field-key="${f}"] { display: none; }`)});let A=1;return c.forEach(f=>{let I=1;f.forEach(T=>{k.push(`[data-autoform-id="${u}"] [data-field-key="${T}"] { display: block; grid-row: ${A}; grid-column: ${I}; }`),I++}),A++}),h?[`@media (min-width: ${h}px) {`,...k.map(f=>` ${f}`),"}"].join(`
|
|
2
|
+
`):k.join(`
|
|
3
|
+
`)};t.push(S(l.default));const b=Object.keys(l).filter(c=>c!=="default").sort((c,h)=>(r[c]??0)-(r[h]??0));for(const c of b){const h=r[c],y=l[c];h&&y&&t.push(S(y,String(h)))}}return t.join(`
|
|
4
|
+
`)});return v({validateAll:V,errors:m}),(r,u)=>(e.openBlock(),e.createElementBlock("div",{class:"autoform","data-autoform-id":$.value},[i.value?(e.openBlock(),e.createBlock(e.resolveDynamicComponent("style"),{key:0},{default:e.withCtx(()=>[e.createTextVNode(e.toDisplayString(i.value),1)]),_:1})):e.createCommentVNode("",!0),e.createElementVNode("div",w,[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(e.unref(E),t=>(e.openBlock(),e.createElementBlock("div",{key:t,class:"autoform-cell","data-field-key":o.value?t:void 0},[a.fields[t]?(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},[(e.openBlock(),e.createBlock(e.resolveDynamicComponent(a.fields[t].component),e.mergeProps({modelValue:a.modelValue[t],error:e.unref(m)[t]},{ref_for:!0},s(t),{"onUpdate:modelValue":l=>e.unref(p)(t,l),onBlur:()=>e.unref(g)(t)}),null,16,["modelValue","error","onUpdate:modelValue","onBlur"])),e.renderSlot(r.$slots,"error",{fieldKey:t,error:e.unref(m)[t]})],64)):(e.openBlock(),e.createElementBlock(e.Fragment,{key:1},[e.renderSlot(r.$slots,`field-${t}`,{fieldKey:t,value:a.modelValue[t],error:e.unref(m)[t],onUpdate:l=>e.unref(p)(t,l),onBlur:()=>e.unref(g)(t)},()=>[e.createElementVNode("input",{class:"autoform-default-input",value:String(a.modelValue[t]??""),onInput:l=>e.unref(p)(t,l.target.value),onBlur:()=>e.unref(g)(t)},null,40,R)]),e.renderSlot(r.$slots,"error",{fieldKey:t,error:e.unref(m)[t]})],64))],8,D))),128))])],8,N))}});exports.AutoForm=U;exports.DEFAULT_BREAKPOINTS=C;exports.isExplicitLayout=x;exports.isShorthandLayout=O;exports.useAutoForm=j;
|
|
5
5
|
//# sourceMappingURL=vue-autoform.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vue-autoform.cjs.js","sources":["../src/types.ts","../src/composables/useAutoForm.ts","../src/components/AutoForm.vue","../src/components/AutoFormRow.vue"],"sourcesContent":["import type { Component } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\n\nexport type BreakpointKey = 'sm' | 'md' | 'lg' | 'xl' | '2xl'\n\nexport type BreakpointMap = Record<BreakpointKey, number>\n\nexport const DEFAULT_BREAKPOINTS: BreakpointMap = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n '2xl': 1536,\n}\n\nexport interface FieldConfig {\n component: Component\n props?: Record<string, unknown>\n [key: string]: unknown\n}\n\nexport type LayoutRow = string[]\n\nexport type LayoutGrid = LayoutRow[]\n\nexport type ShorthandLayout = { default: number } & Partial<Record<BreakpointKey, number>>\n\nexport type ExplicitLayout = { default: LayoutGrid } & Partial<Record<BreakpointKey, LayoutGrid>>\n\nexport type AutoFormLayout = ShorthandLayout | ExplicitLayout\n\nexport function isShorthandLayout(layout: AutoFormLayout): layout is ShorthandLayout {\n return typeof layout.default === 'number'\n}\n\nexport function isExplicitLayout(layout: AutoFormLayout): layout is ExplicitLayout {\n return Array.isArray(layout.default)\n}\n\nexport interface AutoFormProps {\n schema: ZodObject<ZodRawShape>\n layout?: AutoFormLayout\n fields: Record<string, FieldConfig>\n modelValue: Record<string, unknown>\n validateOn?: 'blur' | 'input' | 'submit'\n breakpoints?: Partial<BreakpointMap>\n}\n\nexport interface FieldRenderInfo {\n key: string\n config: FieldConfig | undefined\n value: unknown\n error: string | undefined\n}\n","import { reactive, computed } from 'vue'\nimport type { Ref } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\nimport type { BreakpointMap, AutoFormLayout, LayoutGrid } from '../types'\nimport {\n DEFAULT_BREAKPOINTS,\n isShorthandLayout,\n isExplicitLayout,\n} from '../types'\n\nexport function useAutoForm(\n schema: ZodObject<ZodRawShape>,\n modelValue: Ref<Record<string, unknown>>,\n emit: (event: 'update:modelValue', value: Record<string, unknown>) => void,\n validateOn: Ref<'blur' | 'input' | 'submit'>,\n layout: Ref<AutoFormLayout | undefined>,\n breakpointsOverride: Ref<Partial<BreakpointMap> | undefined>,\n) {\n const errors = reactive<Record<string, string>>({})\n\n const breakpoints = computed<BreakpointMap>(() => ({\n ...DEFAULT_BREAKPOINTS,\n ...breakpointsOverride.value,\n }))\n\n const schemaKeys = computed<string[]>(() => Object.keys(schema.shape))\n\n const resolvedLayout = computed<LayoutGrid>(() => {\n const l = layout.value\n if (!l) {\n return schemaKeys.value.map((key: string) => [key])\n }\n if (isExplicitLayout(l)) {\n return l.default\n }\n if (isShorthandLayout(l)) {\n const cols = l.default\n const keys = schemaKeys.value\n const rows: LayoutGrid = []\n for (let i = 0; i < keys.length; i += cols) {\n rows.push(keys.slice(i, i + cols))\n }\n return rows\n }\n return schemaKeys.value.map((key: string) => [key])\n })\n\n function validateField(key: string, value: unknown): string | undefined {\n const shape = schema.shape\n if (!shape[key]) return undefined\n const fieldSchema = shape[key]\n const result = fieldSchema.safeParse(value)\n if (!result.success) {\n return result.error.errors[0]?.message ?? 'Invalid value'\n }\n return undefined\n }\n\n function validateAll(): boolean {\n const result = schema.safeParse(modelValue.value)\n if (!result.success) {\n result.error.errors.forEach((err) => {\n const key = String(err.path[0])\n errors[key] = err.message\n })\n return false\n }\n Object.keys(errors).forEach((k: string) => delete errors[k])\n return true\n }\n\n function onFieldInput(key: string, value: unknown) {\n const updated = { ...modelValue.value, [key]: value }\n emit('update:modelValue', updated)\n if (validateOn.value === 'input') {\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function onFieldBlur(key: string) {\n if (validateOn.value === 'blur') {\n const msg = validateField(key, modelValue.value[key])\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function getBreakpointLayouts(): Array<{ minWidth: number; grid: LayoutGrid; key: string }> {\n const l = layout.value\n if (!l) return []\n\n const bps = breakpoints.value\n const result: Array<{ minWidth: number; grid: LayoutGrid; key: string }> = []\n\n if (isExplicitLayout(l)) {\n const bpKeys = Object.keys(l).filter((k) => k !== 'default') as Array<keyof typeof l>\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap]\n const grid = l[bpKey as keyof typeof l] as LayoutGrid\n if (minWidth && grid) {\n result.push({ minWidth, grid, key: bpKey })\n }\n }\n } else if (isShorthandLayout(l)) {\n const bpKeys = Object.keys(l).filter((k) => k !== 'default') as Array<keyof typeof l>\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap]\n const cols = l[bpKey as keyof typeof l] as number\n if (minWidth && cols) {\n const keys = schemaKeys.value\n const rows: LayoutGrid = []\n for (let i = 0; i < keys.length; i += cols) {\n rows.push(keys.slice(i, i + cols))\n }\n result.push({ minWidth, grid: rows, key: bpKey })\n }\n }\n }\n\n return result.sort((a, b) => a.minWidth - b.minWidth)\n }\n\n return {\n errors,\n resolvedLayout,\n schemaKeys,\n onFieldInput,\n onFieldBlur,\n validateAll,\n getBreakpointLayouts,\n }\n}\n","<template>\n <div class=\"autoform\" :data-autoform-id=\"formId\">\n <component :is=\"'style'\" v-if=\"mediaQueryCss\">{{\n mediaQueryCss\n }}</component>\n\n <!-- Shorthand layout: single flat grid, CSS handles column count per breakpoint -->\n <div v-if=\"isShorthand\" class=\"autoform-flat-grid\">\n <div v-for=\"fieldKey in flatKeys\" :key=\"fieldKey\" class=\"autoform-cell\">\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"fields[fieldKey].props ?? {}\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n\n <!-- Explicit layout / no layout: flat grid, CSS positions each field per breakpoint -->\n <div v-else class=\"autoform-flat-grid\">\n <div\n v-for=\"fieldKey in flatKeys\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n :data-field-key=\"fieldKey\"\n >\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"fields[fieldKey].props ?? {}\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { computed, ref, toRef } from \"vue\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport { useAutoForm } from \"../composables/useAutoForm\";\nimport type {\n AutoFormLayout,\n BreakpointMap,\n FieldConfig,\n LayoutGrid,\n} from \"../types\";\nimport {\n isExplicitLayout,\n isShorthandLayout,\n DEFAULT_BREAKPOINTS,\n} from \"../types\";\n\nconst props = withDefaults(\n defineProps<{\n schema: ZodObject<ZodRawShape>;\n layout?: AutoFormLayout;\n fields: Record<string, FieldConfig>;\n modelValue: Record<string, unknown>;\n validateOn?: \"blur\" | \"input\" | \"submit\";\n breakpoints?: Partial<BreakpointMap>;\n }>(),\n {\n validateOn: \"blur\",\n },\n);\n\nconst emit = defineEmits<{\n (e: \"update:modelValue\", value: Record<string, unknown>): void;\n}>();\n\nconst formId = ref(`autoform-${Math.random().toString(36).slice(2, 9)}`);\n\nconst { errors, schemaKeys, onFieldInput, onFieldBlur, validateAll } =\n useAutoForm(\n props.schema,\n toRef(props, \"modelValue\"),\n (_, val) => emit(\"update:modelValue\", val),\n toRef(props, \"validateOn\"),\n toRef(props, \"layout\"),\n toRef(props, \"breakpoints\"),\n );\n\nconst isShorthand = computed(() =>\n props.layout ? isShorthandLayout(props.layout) : false,\n);\n\nconst flatKeys = computed(() => schemaKeys.value);\n\nconst mediaQueryCss = computed<string>(() => {\n if (!props.layout) return \"\";\n\n const bps: BreakpointMap = { ...DEFAULT_BREAKPOINTS, ...props.breakpoints };\n const id = formId.value;\n const lines: string[] = [];\n\n if (isShorthandLayout(props.layout)) {\n const layout = props.layout;\n // Base columns for the flat grid\n lines.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${layout.default}, minmax(0, 1fr));`,\n `}`,\n );\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const cols = layout[bpKey as keyof typeof layout] as number;\n if (minWidth && cols) {\n lines.push(\n `@media (min-width: ${minWidth}px) {`,\n ` [data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${cols}, minmax(0, 1fr));`,\n ` }`,\n `}`,\n );\n }\n }\n } else if (isExplicitLayout(props.layout)) {\n const layout = props.layout;\n // Helper: build CSS rules for a given grid at a given scope (media query or base)\n const gridToCss = (grid: LayoutGrid, wrap?: string): string => {\n const maxCols = Math.max(...grid.map((r: string[]) => r.length));\n const allKeys = schemaKeys.value;\n const keysInGrid = new Set(grid.flat());\n const fieldRules: string[] = [];\n\n // Set grid column count\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid { grid-template-columns: repeat(${maxCols}, minmax(0, 1fr)); }`,\n );\n\n // Hide fields not in this layout\n allKeys.forEach((key: string) => {\n if (!keysInGrid.has(key)) {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: none; }`,\n );\n }\n });\n\n // Position each field\n let rowStart = 1;\n grid.forEach((row: string[]) => {\n let colStart = 1;\n row.forEach((key: string) => {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: block; grid-row: ${rowStart}; grid-column: ${colStart}; }`,\n );\n colStart++;\n });\n rowStart++;\n });\n\n if (wrap) {\n return [\n `@media (min-width: ${wrap}px) {`,\n ...fieldRules.map((r) => ` ${r}`),\n `}`,\n ].join(\"\\n\");\n }\n return fieldRules.join(\"\\n\");\n };\n\n // Default layout base styles\n lines.push(gridToCss(layout.default));\n\n // Per-breakpoint overrides\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n const sortedBpKeys = bpKeys.sort(\n (a, b) =>\n (bps[a as keyof BreakpointMap] ?? 0) -\n (bps[b as keyof BreakpointMap] ?? 0),\n );\n for (const bpKey of sortedBpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const grid = layout[bpKey as keyof typeof layout] as LayoutGrid;\n if (minWidth && grid) {\n lines.push(gridToCss(grid, String(minWidth)));\n }\n }\n }\n\n return lines.join(\"\\n\");\n});\n\ndefineExpose({ validateAll, errors });\n</script>\n\n<style>\n.autoform {\n width: 100%;\n display: flex;\n flex-direction: column;\n gap: var(--autoform-row-gap, 1rem);\n}\n\n.autoform-flat-grid {\n display: grid;\n grid-template-columns: repeat(1, minmax(0, 1fr));\n gap: var(--autoform-gap, 1rem);\n width: 100%;\n}\n\n.autoform-flat-grid .autoform-cell {\n min-width: 0;\n}\n\n.autoform-default-input {\n width: 100%;\n padding: 0.5rem 0.75rem;\n border: 1px solid #d1d5db;\n border-radius: 0.375rem;\n font-size: 1rem;\n outline: none;\n box-sizing: border-box;\n}\n\n.autoform-default-input:focus {\n border-color: #6366f1;\n box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);\n}\n</style>\n","<template>\n <div\n class=\"autoform-row\"\n :style=\"{ '--autoform-cols': row.length }\"\n >\n <div\n v-for=\"fieldKey in row\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n >\n <slot :fieldKey=\"fieldKey\" />\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\ndefineProps<{\n row: string[]\n}>()\n</script>\n\n<style>\n.autoform-row {\n display: grid;\n grid-template-columns: repeat(var(--autoform-cols, 1), minmax(0, 1fr));\n gap: var(--autoform-gap, 1rem);\n width: 100%;\n}\n\n.autoform-cell {\n min-width: 0;\n}\n</style>\n"],"names":["DEFAULT_BREAKPOINTS","isShorthandLayout","layout","isExplicitLayout","useAutoForm","schema","modelValue","emit","validateOn","breakpointsOverride","errors","reactive","breakpoints","computed","schemaKeys","resolvedLayout","l","key","cols","keys","rows","i","validateField","value","shape","result","_a","validateAll","k","err","onFieldInput","updated","msg","onFieldBlur","getBreakpointLayouts","bps","bpKeys","bpKey","minWidth","grid","a","b","props","__props","__emit","formId","ref","toRef","_","val","isShorthand","flatKeys","mediaQueryCss","id","lines","gridToCss","wrap","maxCols","r","allKeys","keysInGrid","fieldRules","rowStart","row","colStart","sortedBpKeys","__expose","_createElementBlock","_openBlock","_createBlock","_resolveDynamicComponent","_hoisted_2","_Fragment","_renderList","fieldKey","_mergeProps","_unref","_renderSlot","_ctx","_createElementVNode","e","_hoisted_4","_normalizeStyle"],"mappings":"uGAOaA,EAAqC,CAChD,GAAI,IACJ,GAAI,IACJ,GAAI,KACJ,GAAI,KACJ,MAAO,IACT,EAkBO,SAASC,EAAkBC,EAAmD,CACnF,OAAO,OAAOA,EAAO,SAAY,QACnC,CAEO,SAASC,EAAiBD,EAAkD,CACjF,OAAO,MAAM,QAAQA,EAAO,OAAO,CACrC,CC3BO,SAASE,EACdC,EACAC,EACAC,EACAC,EACAN,EACAO,EACA,CACA,MAAMC,EAASC,EAAAA,SAAiC,EAAE,EAE5CC,EAAcC,EAAAA,SAAwB,KAAO,CACjD,GAAGb,EACH,GAAGS,EAAoB,KAAA,EACvB,EAEIK,EAAaD,EAAAA,SAAmB,IAAM,OAAO,KAAKR,EAAO,KAAK,CAAC,EAE/DU,EAAiBF,EAAAA,SAAqB,IAAM,CAChD,MAAMG,EAAId,EAAO,MACjB,GAAI,CAACc,EACH,OAAOF,EAAW,MAAM,IAAKG,GAAgB,CAACA,CAAG,CAAC,EAEpD,GAAId,EAAiBa,CAAC,EACpB,OAAOA,EAAE,QAEX,GAAIf,EAAkBe,CAAC,EAAG,CACxB,MAAME,EAAOF,EAAE,QACTG,EAAOL,EAAW,MAClBM,EAAmB,CAAA,EACzB,QAASC,EAAI,EAAGA,EAAIF,EAAK,OAAQE,GAAKH,EACpCE,EAAK,KAAKD,EAAK,MAAME,EAAGA,EAAIH,CAAI,CAAC,EAEnC,OAAOE,CACT,CACA,OAAON,EAAW,MAAM,IAAKG,GAAgB,CAACA,CAAG,CAAC,CACpD,CAAC,EAED,SAASK,EAAcL,EAAaM,EAAoC,OACtE,MAAMC,EAAQnB,EAAO,MACrB,GAAI,CAACmB,EAAMP,CAAG,EAAG,OAEjB,MAAMQ,EADcD,EAAMP,CAAG,EACF,UAAUM,CAAK,EAC1C,GAAI,CAACE,EAAO,QACV,QAAOC,EAAAD,EAAO,MAAM,OAAO,CAAC,IAAr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MAAA3C,CAAA,EAAYyC,CAAQ,CAAA,eAGrCG,aAAoEC,EAAA,OAAA,QAAA,CAAhD,SAAAJ,EAAqB,MAAOE,EAAAA,MAAAlE,CAAA,EAAOgE,CAAQ,CAAA,kICjFvEP,EAAAA,mBAWM,MAAA,CAVJ,MAAM,eACL,MAAKe,EAAAA,eAAA,CAAA,kBAAuBvC,EAAA,IAAI,OAAM,CAAA,oBAEvCwB,EAAAA,mBAMMK,EAAAA,SAAA,KAAAC,EAAAA,WALe9B,EAAA,IAAZ+B,kBADTP,EAAAA,mBAMM,MAAA,CAJH,IAAKO,EACN,MAAM,eAAA,GAENG,EAAAA,WAA6BC,EAAA,OAAA,UAAA,CAAtB,SAAAJ,EAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"vue-autoform.cjs.js","sources":["../src/composables/useAutoForm.ts","../src/types.ts","../src/components/AutoForm.vue"],"sourcesContent":["import { reactive, computed } from 'vue'\nimport type { Ref } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\n\nexport function useAutoForm(\n schema: ZodObject<ZodRawShape>,\n modelValue: Ref<Record<string, unknown>>,\n emit: (event: 'update:modelValue', value: Record<string, unknown>) => void,\n validateOn: Ref<'blur' | 'input' | 'submit'>,\n) {\n const errors = reactive<Record<string, string>>({})\n\n const schemaKeys = computed<string[]>(() => Object.keys(schema.shape))\n\n function validateField(key: string, value: unknown): string | undefined {\n const shape = schema.shape\n if (!(key in shape)) return undefined\n const fieldSchema = shape[key]\n const result = fieldSchema.safeParse(value)\n if (!result.success) {\n return result.error.errors[0]?.message ?? 'Invalid value'\n }\n return undefined\n }\n\n function validateAll(): boolean {\n const result = schema.safeParse(modelValue.value)\n if (!result.success) {\n result.error.errors.forEach((err) => {\n const key = String(err.path[0])\n errors[key] = err.message\n })\n return false\n }\n Object.keys(errors).forEach((k: string) => delete errors[k])\n return true\n }\n\n const lastValue: Record<string, unknown> = {}\n\n function onFieldInput(key: string, value: unknown) {\n lastValue[key] = value\n const updated = { ...modelValue.value, [key]: value }\n emit('update:modelValue', updated)\n if (validateOn.value === 'input') {\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function onFieldBlur(key: string) {\n if (validateOn.value === 'blur') {\n // Use lastValue if available — modelValue prop may not have propagated yet\n // when blur fires immediately after update:modelValue (e.g. checkbox toggles)\n const value = key in lastValue ? lastValue[key] : modelValue.value[key]\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n return {\n errors,\n schemaKeys,\n onFieldInput,\n onFieldBlur,\n validateAll,\n }\n}\n","import type { Component } from 'vue'\n\nexport type BreakpointKey = 'sm' | 'md' | 'lg' | 'xl' | '2xl'\n\nexport type BreakpointMap = Record<BreakpointKey, number>\n\nexport const DEFAULT_BREAKPOINTS: BreakpointMap = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n '2xl': 1536,\n}\n\nexport interface FieldConfig {\n component: Component\n props?: Record<string, unknown> | (() => Record<string, unknown>)\n}\n\nexport type LayoutRow = string[]\n\nexport type LayoutGrid = LayoutRow[]\n\nexport type ShorthandLayout = { default: number } & Partial<Record<BreakpointKey, number>>\n\nexport type ExplicitLayout = { default: LayoutGrid } & Partial<Record<BreakpointKey, LayoutGrid>>\n\nexport type AutoFormLayout = ShorthandLayout | ExplicitLayout\n\nexport function isShorthandLayout(layout: AutoFormLayout): layout is ShorthandLayout {\n return typeof layout.default === 'number'\n}\n\nexport function isExplicitLayout(layout: AutoFormLayout): layout is ExplicitLayout {\n return Array.isArray(layout.default)\n}\n\nexport interface FieldRenderInfo {\n key: string\n config: FieldConfig | undefined\n value: unknown\n error: string | undefined\n}\n","<template>\n <div class=\"autoform\" :data-autoform-id=\"formId\">\n <component :is=\"'style'\" v-if=\"mediaQueryCss\">{{\n mediaQueryCss\n }}</component>\n\n <!-- Flat grid: CSS handles column count (shorthand) or per-field position (explicit/none) -->\n <div class=\"autoform-flat-grid\">\n <div\n v-for=\"fieldKey in schemaKeys\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n :data-field-key=\"isExplicit ? fieldKey : undefined\"\n >\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"resolveProps(fieldKey)\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { computed, ref, toRef } from \"vue\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport { useAutoForm } from \"../composables/useAutoForm\";\nimport type {\n AutoFormLayout,\n BreakpointMap,\n FieldConfig,\n LayoutGrid,\n} from \"../types\";\nimport {\n isExplicitLayout,\n isShorthandLayout,\n DEFAULT_BREAKPOINTS,\n} from \"../types\";\n\nconst props = withDefaults(\n defineProps<{\n schema: ZodObject<ZodRawShape>;\n layout?: AutoFormLayout;\n fields: Record<string, FieldConfig>;\n modelValue: Record<string, unknown>;\n validateOn?: \"blur\" | \"input\" | \"submit\";\n breakpoints?: Partial<BreakpointMap>;\n }>(),\n {\n validateOn: \"blur\",\n },\n);\n\nconst emit = defineEmits<{\n (e: \"update:modelValue\", value: Record<string, unknown>): void;\n}>();\n\nconst formId = ref(`autoform-${Math.random().toString(36).slice(2, 9)}`);\n\nconst { errors, schemaKeys, onFieldInput, onFieldBlur, validateAll } =\n useAutoForm(\n props.schema,\n toRef(props, \"modelValue\"),\n (event, val) => emit(event, val),\n toRef(props, \"validateOn\"),\n );\n\nconst isExplicit = computed(() =>\n props.layout ? isExplicitLayout(props.layout) : true,\n);\n\nfunction resolveProps(fieldKey: string): Record<string, unknown> {\n const cfg = props.fields[fieldKey];\n if (!cfg) return {};\n const p = cfg.props;\n return typeof p === \"function\" ? p() : (p ?? {});\n}\n\nconst mediaQueryCss = computed<string>(() => {\n if (!props.layout) return \"\";\n\n const bps: BreakpointMap = { ...DEFAULT_BREAKPOINTS, ...props.breakpoints };\n const id = formId.value;\n const lines: string[] = [];\n\n if (isShorthandLayout(props.layout)) {\n const layout = props.layout;\n // Base columns for the flat grid\n lines.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${layout.default}, minmax(0, 1fr));`,\n `}`,\n );\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const cols = layout[bpKey as keyof typeof layout] as number;\n if (minWidth && cols) {\n lines.push(\n `@media (min-width: ${minWidth}px) {`,\n ` [data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${cols}, minmax(0, 1fr));`,\n ` }`,\n `}`,\n );\n }\n }\n } else if (isExplicitLayout(props.layout)) {\n const layout = props.layout;\n // Helper: build CSS rules for a given grid at a given scope (media query or base)\n const gridToCss = (grid: LayoutGrid, wrap?: string): string => {\n const maxCols = Math.max(...grid.map((r: string[]) => r.length));\n const allKeys = schemaKeys.value;\n const keysInGrid = new Set(grid.flat());\n const fieldRules: string[] = [];\n\n // Set grid column count\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid { grid-template-columns: repeat(${maxCols}, minmax(0, 1fr)); }`,\n );\n\n // Hide fields not in this layout\n allKeys.forEach((key: string) => {\n if (!keysInGrid.has(key)) {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: none; }`,\n );\n }\n });\n\n // Position each field\n let rowStart = 1;\n grid.forEach((row: string[]) => {\n let colStart = 1;\n row.forEach((key: string) => {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: block; grid-row: ${rowStart}; grid-column: ${colStart}; }`,\n );\n colStart++;\n });\n rowStart++;\n });\n\n if (wrap) {\n return [\n `@media (min-width: ${wrap}px) {`,\n ...fieldRules.map((r) => ` ${r}`),\n `}`,\n ].join(\"\\n\");\n }\n return fieldRules.join(\"\\n\");\n };\n\n // Default layout base styles\n lines.push(gridToCss(layout.default));\n\n // Per-breakpoint overrides\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n const sortedBpKeys = bpKeys.sort(\n (a, b) =>\n (bps[a as keyof BreakpointMap] ?? 0) -\n (bps[b as keyof BreakpointMap] ?? 0),\n );\n for (const bpKey of sortedBpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const grid = layout[bpKey as keyof typeof layout] as LayoutGrid;\n if (minWidth && grid) {\n lines.push(gridToCss(grid, String(minWidth)));\n }\n }\n }\n\n return lines.join(\"\\n\");\n});\n\ndefineExpose({ validateAll, errors });\n</script>\n\n<style>\n.autoform {\n width: 100%;\n}\n\n.autoform-flat-grid {\n display: grid;\n grid-template-columns: repeat(1, minmax(0, 1fr));\n column-gap: var(--autoform-gap, 1rem);\n row-gap: var(--autoform-row-gap, 1rem);\n width: 100%;\n}\n\n.autoform-flat-grid .autoform-cell {\n min-width: 0;\n}\n\n.autoform-default-input {\n width: 100%;\n padding: 0.5rem 0.75rem;\n border: 1px solid #d1d5db;\n border-radius: 0.375rem;\n font-size: 1rem;\n outline: none;\n box-sizing: border-box;\n}\n\n.autoform-default-input:focus {\n border-color: #6366f1;\n box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);\n}\n</style>\n"],"names":["useAutoForm","schema","modelValue","emit","validateOn","errors","reactive","schemaKeys","computed","validateField","key","value","shape","result","_a","validateAll","k","err","lastValue","onFieldInput","updated","msg","onFieldBlur","DEFAULT_BREAKPOINTS","isShorthandLayout","layout","isExplicitLayout","props","__props","__emit","formId","ref","toRef","event","val","isExplicit","resolveProps","fieldKey","cfg","p","mediaQueryCss","bps","id","lines","bpKeys","bpKey","minWidth","cols","gridToCss","grid","wrap","maxCols","r","allKeys","keysInGrid","fieldRules","rowStart","row","colStart","sortedBpKeys","a","b","__expose","_createElementBlock","_openBlock","_createBlock","_resolveDynamicComponent","_createElementVNode","_hoisted_2","_Fragment","_renderList","_unref","_mergeProps","_renderSlot","_ctx","e"],"mappings":"uGAIO,SAASA,EACdC,EACAC,EACAC,EACAC,EACA,CACA,MAAMC,EAASC,EAAAA,SAAiC,EAAE,EAE5CC,EAAaC,EAAAA,SAAmB,IAAM,OAAO,KAAKP,EAAO,KAAK,CAAC,EAErE,SAASQ,EAAcC,EAAaC,EAAoC,OACtE,MAAMC,EAAQX,EAAO,MACrB,GAAI,EAAES,KAAOE,GAAQ,OAErB,MAAMC,EADcD,EAAMF,CAAG,EACF,UAAUC,CAAK,EAC1C,GAAI,CAACE,EAAO,QACV,QAAOC,EAAAD,EAAO,MAAM,OAAO,CAAC,IAArB,YAAAC,EAAwB,UAAW,eAG9C,CAEA,SAASC,GAAuB,CAC9B,MAAMF,EAASZ,EAAO,UAAUC,EAAW,KAAK,EAChD,OAAKW,EAAO,SAOZ,OAAO,KAAKR,CAAM,EAAE,QAASW,GAAc,OAAOX,EAAOW,CAAC,CAAC,EACpD,KAPLH,EAAO,MAAM,OAAO,QAASI,GAAQ,CACnC,MAAMP,EAAM,O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MAAM,yBACL,MAAO,OAAOvC,EAAA,WAAWS,CAAQ,GAAA,EAAA,EACjC,QAAyBsC,GAA+BJ,QAAApD,CAAA,EAAakB,EAAWsC,EAAE,OAA4B,KAAK,EAInH,OAAI,IAAQJ,EAAAA,MAAAjD,CAAA,EAAYe,CAAQ,CAAA,eAGrCoC,aAAoEC,EAAA,OAAA,QAAA,CAAhD,SAAArC,EAAqB,MAAOkC,EAAAA,MAAAlE,CAAA,EAAOgC,CAAQ,CAAA"}
|
package/dist/vue-autoform.d.ts
CHANGED
|
@@ -39,16 +39,8 @@ validateOn: string;
|
|
|
39
39
|
validateOn: "blur" | "input" | "submit";
|
|
40
40
|
}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>;
|
|
41
41
|
|
|
42
|
-
declare const __VLS_component_2: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_2<{
|
|
43
|
-
row: string[];
|
|
44
|
-
}>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_2<{
|
|
45
|
-
row: string[];
|
|
46
|
-
}>>> & Readonly<{}>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>;
|
|
47
|
-
|
|
48
42
|
declare type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
|
|
49
43
|
|
|
50
|
-
declare type __VLS_NonUndefinedable_2<T> = T extends undefined ? never : T;
|
|
51
|
-
|
|
52
44
|
declare type __VLS_Prettify<T> = {
|
|
53
45
|
[K in keyof T]: T[K];
|
|
54
46
|
} & {};
|
|
@@ -59,12 +51,6 @@ declare function __VLS_template(): Partial<Record<`field-${string}`, (_: {
|
|
|
59
51
|
error: string;
|
|
60
52
|
onUpdate: (val: unknown) => void;
|
|
61
53
|
onBlur: () => void;
|
|
62
|
-
}) => any>> & Partial<Record<`field-${string}`, (_: {
|
|
63
|
-
fieldKey: string;
|
|
64
|
-
value: unknown;
|
|
65
|
-
error: string;
|
|
66
|
-
onUpdate: (val: unknown) => void;
|
|
67
|
-
onBlur: () => void;
|
|
68
54
|
}) => any>> & {
|
|
69
55
|
error?(_: {
|
|
70
56
|
fieldKey: string;
|
|
@@ -72,12 +58,6 @@ declare function __VLS_template(): Partial<Record<`field-${string}`, (_: {
|
|
|
72
58
|
}): any;
|
|
73
59
|
};
|
|
74
60
|
|
|
75
|
-
declare function __VLS_template_2(): {
|
|
76
|
-
default?(_: {
|
|
77
|
-
fieldKey: string;
|
|
78
|
-
}): any;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
61
|
declare type __VLS_TypePropsToRuntimeProps<T> = {
|
|
82
62
|
[K in keyof T]-?: {} extends Pick<T, K> ? {
|
|
83
63
|
type: PropType<__VLS_NonUndefinedable<T[K]>>;
|
|
@@ -87,15 +67,6 @@ declare type __VLS_TypePropsToRuntimeProps<T> = {
|
|
|
87
67
|
};
|
|
88
68
|
};
|
|
89
69
|
|
|
90
|
-
declare type __VLS_TypePropsToRuntimeProps_2<T> = {
|
|
91
|
-
[K in keyof T]-?: {} extends Pick<T, K> ? {
|
|
92
|
-
type: PropType<__VLS_NonUndefinedable_2<T[K]>>;
|
|
93
|
-
} : {
|
|
94
|
-
type: PropType<T[K]>;
|
|
95
|
-
required: true;
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
|
|
99
70
|
declare type __VLS_WithDefaults<P, D> = {
|
|
100
71
|
[K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
|
|
101
72
|
default: D[K];
|
|
@@ -108,27 +79,10 @@ declare type __VLS_WithTemplateSlots<T, S> = T & {
|
|
|
108
79
|
};
|
|
109
80
|
};
|
|
110
81
|
|
|
111
|
-
declare type __VLS_WithTemplateSlots_2<T, S> = T & {
|
|
112
|
-
new (): {
|
|
113
|
-
$slots: S;
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
|
|
117
82
|
export declare const AutoForm: __VLS_WithTemplateSlots<typeof __VLS_component, ReturnType<typeof __VLS_template>>;
|
|
118
83
|
|
|
119
84
|
export declare type AutoFormLayout = ShorthandLayout | ExplicitLayout;
|
|
120
85
|
|
|
121
|
-
export declare interface AutoFormProps {
|
|
122
|
-
schema: ZodObject<ZodRawShape>;
|
|
123
|
-
layout?: AutoFormLayout;
|
|
124
|
-
fields: Record<string, FieldConfig>;
|
|
125
|
-
modelValue: Record<string, unknown>;
|
|
126
|
-
validateOn?: 'blur' | 'input' | 'submit';
|
|
127
|
-
breakpoints?: Partial<BreakpointMap>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export declare const AutoFormRow: __VLS_WithTemplateSlots_2<typeof __VLS_component_2, ReturnType<typeof __VLS_template_2>>;
|
|
131
|
-
|
|
132
86
|
export declare type BreakpointKey = 'sm' | 'md' | 'lg' | 'xl' | '2xl';
|
|
133
87
|
|
|
134
88
|
export declare type BreakpointMap = Record<BreakpointKey, number>;
|
|
@@ -141,15 +95,7 @@ export declare type ExplicitLayout = {
|
|
|
141
95
|
|
|
142
96
|
export declare interface FieldConfig {
|
|
143
97
|
component: Component;
|
|
144
|
-
props?: Record<string, unknown
|
|
145
|
-
[key: string]: unknown;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export declare interface FieldRenderInfo {
|
|
149
|
-
key: string;
|
|
150
|
-
config: FieldConfig | undefined;
|
|
151
|
-
value: unknown;
|
|
152
|
-
error: string | undefined;
|
|
98
|
+
props?: Record<string, unknown> | (() => Record<string, unknown>);
|
|
153
99
|
}
|
|
154
100
|
|
|
155
101
|
export declare function isExplicitLayout(layout: AutoFormLayout): layout is ExplicitLayout;
|
|
@@ -164,18 +110,12 @@ export declare type ShorthandLayout = {
|
|
|
164
110
|
default: number;
|
|
165
111
|
} & Partial<Record<BreakpointKey, number>>;
|
|
166
112
|
|
|
167
|
-
export declare function useAutoForm(schema: ZodObject<ZodRawShape>, modelValue: Ref<Record<string, unknown>>, emit: (event: 'update:modelValue', value: Record<string, unknown>) => void, validateOn: Ref<'blur' | 'input' | 'submit'
|
|
113
|
+
export declare function useAutoForm(schema: ZodObject<ZodRawShape>, modelValue: Ref<Record<string, unknown>>, emit: (event: 'update:modelValue', value: Record<string, unknown>) => void, validateOn: Ref<'blur' | 'input' | 'submit'>): {
|
|
168
114
|
errors: Record<string, string>;
|
|
169
|
-
resolvedLayout: ComputedRef<LayoutGrid>;
|
|
170
115
|
schemaKeys: ComputedRef<string[]>;
|
|
171
116
|
onFieldInput: (key: string, value: unknown) => void;
|
|
172
117
|
onFieldBlur: (key: string) => void;
|
|
173
118
|
validateAll: () => boolean;
|
|
174
|
-
getBreakpointLayouts: () => Array<{
|
|
175
|
-
minWidth: number;
|
|
176
|
-
grid: LayoutGrid;
|
|
177
|
-
key: string;
|
|
178
|
-
}>;
|
|
179
119
|
};
|
|
180
120
|
|
|
181
121
|
export { }
|
package/dist/vue-autoform.es.js
CHANGED
|
@@ -1,104 +1,58 @@
|
|
|
1
|
-
import { reactive as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
function N(a) {
|
|
13
|
-
return Array.isArray(a.default);
|
|
14
|
-
}
|
|
15
|
-
function tt(a, k, w, u, F, A) {
|
|
16
|
-
const l = H({}), E = S(() => ({
|
|
17
|
-
...G,
|
|
18
|
-
...A.value
|
|
19
|
-
})), p = S(() => Object.keys(a.shape)), v = S(() => {
|
|
20
|
-
const o = F.value;
|
|
21
|
-
if (!o)
|
|
22
|
-
return p.value.map((t) => [t]);
|
|
23
|
-
if (N(o))
|
|
24
|
-
return o.default;
|
|
25
|
-
if (C(o)) {
|
|
26
|
-
const t = o.default, e = p.value, c = [];
|
|
27
|
-
for (let r = 0; r < e.length; r += t)
|
|
28
|
-
c.push(e.slice(r, r + t));
|
|
29
|
-
return c;
|
|
30
|
-
}
|
|
31
|
-
return p.value.map((t) => [t]);
|
|
32
|
-
});
|
|
33
|
-
function I(o, t) {
|
|
34
|
-
var f;
|
|
35
|
-
const e = a.shape;
|
|
36
|
-
if (!e[o]) return;
|
|
37
|
-
const r = e[o].safeParse(t);
|
|
38
|
-
if (!r.success)
|
|
39
|
-
return ((f = r.error.errors[0]) == null ? void 0 : f.message) ?? "Invalid value";
|
|
1
|
+
import { reactive as M, computed as C, defineComponent as W, ref as G, toRef as P, openBlock as v, createElementBlock as y, createBlock as L, resolveDynamicComponent as N, withCtx as Q, createTextVNode as q, toDisplayString as z, createCommentVNode as H, createElementVNode as T, Fragment as I, renderList as J, unref as c, mergeProps as X, renderSlot as j } from "vue";
|
|
2
|
+
function Y(a, g, k, r) {
|
|
3
|
+
const d = M({}), S = C(() => Object.keys(a.shape));
|
|
4
|
+
function m(o, n) {
|
|
5
|
+
var t;
|
|
6
|
+
const i = a.shape;
|
|
7
|
+
if (!(o in i)) return;
|
|
8
|
+
const s = i[o].safeParse(n);
|
|
9
|
+
if (!s.success)
|
|
10
|
+
return ((t = s.error.errors[0]) == null ? void 0 : t.message) ?? "Invalid value";
|
|
40
11
|
}
|
|
41
|
-
function
|
|
42
|
-
const o = a.safeParse(
|
|
43
|
-
return o.success ? (Object.keys(
|
|
44
|
-
const
|
|
45
|
-
|
|
12
|
+
function V() {
|
|
13
|
+
const o = a.safeParse(g.value);
|
|
14
|
+
return o.success ? (Object.keys(d).forEach((n) => delete d[n]), !0) : (o.error.errors.forEach((n) => {
|
|
15
|
+
const i = String(n.path[0]);
|
|
16
|
+
d[i] = n.message;
|
|
46
17
|
}), !1);
|
|
47
18
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function x(o) {
|
|
56
|
-
if (u.value === "blur") {
|
|
57
|
-
const t = I(o, k.value[o]);
|
|
58
|
-
t ? l[o] = t : delete l[o];
|
|
19
|
+
const p = {};
|
|
20
|
+
function $(o, n) {
|
|
21
|
+
p[o] = n;
|
|
22
|
+
const i = { ...g.value, [o]: n };
|
|
23
|
+
if (k("update:modelValue", i), r.value === "input") {
|
|
24
|
+
const e = m(o, n);
|
|
25
|
+
e ? d[o] = e : delete d[o];
|
|
59
26
|
}
|
|
60
27
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (N(o)) {
|
|
66
|
-
const c = Object.keys(o).filter((r) => r !== "default");
|
|
67
|
-
for (const r of c) {
|
|
68
|
-
const f = t[r], s = o[r];
|
|
69
|
-
f && s && e.push({ minWidth: f, grid: s, key: r });
|
|
70
|
-
}
|
|
71
|
-
} else if (C(o)) {
|
|
72
|
-
const c = Object.keys(o).filter((r) => r !== "default");
|
|
73
|
-
for (const r of c) {
|
|
74
|
-
const f = t[r], s = o[r];
|
|
75
|
-
if (f && s) {
|
|
76
|
-
const h = p.value, $ = [];
|
|
77
|
-
for (let B = 0; B < h.length; B += s)
|
|
78
|
-
$.push(h.slice(B, B + s));
|
|
79
|
-
e.push({ minWidth: f, grid: $, key: r });
|
|
80
|
-
}
|
|
81
|
-
}
|
|
28
|
+
function A(o) {
|
|
29
|
+
if (r.value === "blur") {
|
|
30
|
+
const n = o in p ? p[o] : g.value[o], i = m(o, n);
|
|
31
|
+
i ? d[o] = i : delete d[o];
|
|
82
32
|
}
|
|
83
|
-
return e.sort((c, r) => c.minWidth - r.minWidth);
|
|
84
33
|
}
|
|
85
34
|
return {
|
|
86
|
-
errors:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
validateAll: U,
|
|
92
|
-
getBreakpointLayouts: m
|
|
35
|
+
errors: d,
|
|
36
|
+
schemaKeys: S,
|
|
37
|
+
onFieldInput: $,
|
|
38
|
+
onFieldBlur: A,
|
|
39
|
+
validateAll: V
|
|
93
40
|
};
|
|
94
41
|
}
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
42
|
+
const Z = {
|
|
43
|
+
sm: 640,
|
|
44
|
+
md: 768,
|
|
45
|
+
lg: 1024,
|
|
46
|
+
xl: 1280,
|
|
47
|
+
"2xl": 1536
|
|
48
|
+
};
|
|
49
|
+
function K(a) {
|
|
50
|
+
return typeof a.default == "number";
|
|
51
|
+
}
|
|
52
|
+
function U(a) {
|
|
53
|
+
return Array.isArray(a.default);
|
|
54
|
+
}
|
|
55
|
+
const tt = ["data-autoform-id"], ot = { class: "autoform-flat-grid" }, et = ["data-field-key"], rt = ["value", "onInput", "onBlur"], nt = /* @__PURE__ */ W({
|
|
102
56
|
__name: "AutoForm",
|
|
103
57
|
props: {
|
|
104
58
|
schema: {},
|
|
@@ -109,190 +63,138 @@ const ot = ["data-autoform-id"], et = {
|
|
|
109
63
|
breakpoints: {}
|
|
110
64
|
},
|
|
111
65
|
emits: ["update:modelValue"],
|
|
112
|
-
setup(a, { expose:
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (!
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
66
|
+
setup(a, { expose: g, emit: k }) {
|
|
67
|
+
const r = a, d = k, S = G(`autoform-${Math.random().toString(36).slice(2, 9)}`), { errors: m, schemaKeys: V, onFieldInput: p, onFieldBlur: $, validateAll: A } = Y(
|
|
68
|
+
r.schema,
|
|
69
|
+
P(r, "modelValue"),
|
|
70
|
+
(e, s) => d(e, s),
|
|
71
|
+
P(r, "validateOn")
|
|
72
|
+
), o = C(
|
|
73
|
+
() => r.layout ? U(r.layout) : !0
|
|
74
|
+
);
|
|
75
|
+
function n(e) {
|
|
76
|
+
const s = r.fields[e];
|
|
77
|
+
if (!s) return {};
|
|
78
|
+
const t = s.props;
|
|
79
|
+
return typeof t == "function" ? t() : t ?? {};
|
|
80
|
+
}
|
|
81
|
+
const i = C(() => {
|
|
82
|
+
if (!r.layout) return "";
|
|
83
|
+
const e = { ...Z, ...r.breakpoints }, s = S.value, t = [];
|
|
84
|
+
if (K(r.layout)) {
|
|
85
|
+
const u = r.layout;
|
|
127
86
|
t.push(
|
|
128
|
-
`[data-autoform-id="${
|
|
129
|
-
` grid-template-columns: repeat(${
|
|
87
|
+
`[data-autoform-id="${s}"] .autoform-flat-grid {`,
|
|
88
|
+
` grid-template-columns: repeat(${u.default}, minmax(0, 1fr));`,
|
|
130
89
|
"}"
|
|
131
90
|
);
|
|
132
|
-
const
|
|
133
|
-
for (const
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
`@media (min-width: ${
|
|
137
|
-
` [data-autoform-id="${
|
|
138
|
-
` grid-template-columns: repeat(${
|
|
91
|
+
const B = Object.keys(u).filter((b) => b !== "default");
|
|
92
|
+
for (const b of B) {
|
|
93
|
+
const E = e[b], l = u[b];
|
|
94
|
+
E && l && t.push(
|
|
95
|
+
`@media (min-width: ${E}px) {`,
|
|
96
|
+
` [data-autoform-id="${s}"] .autoform-flat-grid {`,
|
|
97
|
+
` grid-template-columns: repeat(${l}, minmax(0, 1fr));`,
|
|
139
98
|
" }",
|
|
140
99
|
"}"
|
|
141
100
|
);
|
|
142
101
|
}
|
|
143
|
-
} else if (
|
|
144
|
-
const
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
`[data-autoform-id="${
|
|
148
|
-
),
|
|
149
|
-
|
|
150
|
-
`[data-autoform-id="${
|
|
102
|
+
} else if (U(r.layout)) {
|
|
103
|
+
const u = r.layout, B = (l, h) => {
|
|
104
|
+
const F = Math.max(...l.map((f) => f.length)), _ = V.value, D = new Set(l.flat()), x = [];
|
|
105
|
+
x.push(
|
|
106
|
+
`[data-autoform-id="${s}"] .autoform-flat-grid { grid-template-columns: repeat(${F}, minmax(0, 1fr)); }`
|
|
107
|
+
), _.forEach((f) => {
|
|
108
|
+
D.has(f) || x.push(
|
|
109
|
+
`[data-autoform-id="${s}"] [data-field-key="${f}"] { display: none; }`
|
|
151
110
|
);
|
|
152
111
|
});
|
|
153
|
-
let
|
|
154
|
-
return
|
|
155
|
-
let
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
`[data-autoform-id="${
|
|
159
|
-
),
|
|
160
|
-
}),
|
|
112
|
+
let O = 1;
|
|
113
|
+
return l.forEach((f) => {
|
|
114
|
+
let w = 1;
|
|
115
|
+
f.forEach((R) => {
|
|
116
|
+
x.push(
|
|
117
|
+
`[data-autoform-id="${s}"] [data-field-key="${R}"] { display: block; grid-row: ${O}; grid-column: ${w}; }`
|
|
118
|
+
), w++;
|
|
119
|
+
}), O++;
|
|
161
120
|
}), h ? [
|
|
162
121
|
`@media (min-width: ${h}px) {`,
|
|
163
|
-
...
|
|
122
|
+
...x.map((f) => ` ${f}`),
|
|
164
123
|
"}"
|
|
165
124
|
].join(`
|
|
166
|
-
`) :
|
|
125
|
+
`) : x.join(`
|
|
167
126
|
`);
|
|
168
127
|
};
|
|
169
|
-
t.push(
|
|
170
|
-
const
|
|
171
|
-
(
|
|
128
|
+
t.push(B(u.default));
|
|
129
|
+
const E = Object.keys(u).filter((l) => l !== "default").sort(
|
|
130
|
+
(l, h) => (e[l] ?? 0) - (e[h] ?? 0)
|
|
172
131
|
);
|
|
173
|
-
for (const
|
|
174
|
-
const h =
|
|
175
|
-
h &&
|
|
132
|
+
for (const l of E) {
|
|
133
|
+
const h = e[l], F = u[l];
|
|
134
|
+
h && F && t.push(B(F, String(h)));
|
|
176
135
|
}
|
|
177
136
|
}
|
|
178
137
|
return t.join(`
|
|
179
138
|
`);
|
|
180
139
|
});
|
|
181
|
-
return
|
|
140
|
+
return g({ validateAll: A, errors: m }), (e, s) => (v(), y("div", {
|
|
182
141
|
class: "autoform",
|
|
183
|
-
"data-autoform-id":
|
|
142
|
+
"data-autoform-id": S.value
|
|
184
143
|
}, [
|
|
185
|
-
|
|
186
|
-
default:
|
|
187
|
-
|
|
144
|
+
i.value ? (v(), L(N("style"), { key: 0 }, {
|
|
145
|
+
default: Q(() => [
|
|
146
|
+
q(z(i.value), 1)
|
|
188
147
|
]),
|
|
189
148
|
_: 1
|
|
190
|
-
})) :
|
|
191
|
-
|
|
192
|
-
(
|
|
193
|
-
key: t,
|
|
194
|
-
class: "autoform-cell"
|
|
195
|
-
}, [
|
|
196
|
-
a.fields[t] ? (i(), d(b, { key: 0 }, [
|
|
197
|
-
(i(), L(W(a.fields[t].component), D({
|
|
198
|
-
modelValue: a.modelValue[t],
|
|
199
|
-
error: n(l)[t]
|
|
200
|
-
}, { ref_for: !0 }, a.fields[t].props ?? {}, {
|
|
201
|
-
"onUpdate:modelValue": (e) => n(p)(t, e),
|
|
202
|
-
onBlur: () => n(v)(t)
|
|
203
|
-
}), null, 16, ["modelValue", "error", "onUpdate:modelValue", "onBlur"])),
|
|
204
|
-
V(m.$slots, "error", {
|
|
205
|
-
fieldKey: t,
|
|
206
|
-
error: n(l)[t]
|
|
207
|
-
})
|
|
208
|
-
], 64)) : (i(), d(b, { key: 1 }, [
|
|
209
|
-
V(m.$slots, `field-${t}`, {
|
|
210
|
-
fieldKey: t,
|
|
211
|
-
value: a.modelValue[t],
|
|
212
|
-
error: n(l)[t],
|
|
213
|
-
onUpdate: (e) => n(p)(t, e),
|
|
214
|
-
onBlur: () => n(v)(t)
|
|
215
|
-
}, () => [
|
|
216
|
-
M("input", {
|
|
217
|
-
class: "autoform-default-input",
|
|
218
|
-
value: String(a.modelValue[t] ?? ""),
|
|
219
|
-
onInput: (e) => n(p)(t, e.target.value),
|
|
220
|
-
onBlur: () => n(v)(t)
|
|
221
|
-
}, null, 40, rt)
|
|
222
|
-
]),
|
|
223
|
-
V(m.$slots, "error", {
|
|
224
|
-
fieldKey: t,
|
|
225
|
-
error: n(l)[t]
|
|
226
|
-
})
|
|
227
|
-
], 64))
|
|
228
|
-
]))), 128))
|
|
229
|
-
])) : (i(), d("div", at, [
|
|
230
|
-
(i(!0), d(b, null, R(j.value, (t) => (i(), d("div", {
|
|
149
|
+
})) : H("", !0),
|
|
150
|
+
T("div", ot, [
|
|
151
|
+
(v(!0), y(I, null, J(c(V), (t) => (v(), y("div", {
|
|
231
152
|
key: t,
|
|
232
153
|
class: "autoform-cell",
|
|
233
|
-
"data-field-key": t
|
|
154
|
+
"data-field-key": o.value ? t : void 0
|
|
234
155
|
}, [
|
|
235
|
-
a.fields[t] ? (
|
|
236
|
-
(
|
|
156
|
+
a.fields[t] ? (v(), y(I, { key: 0 }, [
|
|
157
|
+
(v(), L(N(a.fields[t].component), X({
|
|
237
158
|
modelValue: a.modelValue[t],
|
|
238
|
-
error:
|
|
239
|
-
}, { ref_for: !0 },
|
|
240
|
-
"onUpdate:modelValue": (
|
|
241
|
-
onBlur: () =>
|
|
159
|
+
error: c(m)[t]
|
|
160
|
+
}, { ref_for: !0 }, n(t), {
|
|
161
|
+
"onUpdate:modelValue": (u) => c(p)(t, u),
|
|
162
|
+
onBlur: () => c($)(t)
|
|
242
163
|
}), null, 16, ["modelValue", "error", "onUpdate:modelValue", "onBlur"])),
|
|
243
|
-
|
|
164
|
+
j(e.$slots, "error", {
|
|
244
165
|
fieldKey: t,
|
|
245
|
-
error:
|
|
166
|
+
error: c(m)[t]
|
|
246
167
|
})
|
|
247
|
-
], 64)) : (
|
|
248
|
-
|
|
168
|
+
], 64)) : (v(), y(I, { key: 1 }, [
|
|
169
|
+
j(e.$slots, `field-${t}`, {
|
|
249
170
|
fieldKey: t,
|
|
250
171
|
value: a.modelValue[t],
|
|
251
|
-
error:
|
|
252
|
-
onUpdate: (
|
|
253
|
-
onBlur: () =>
|
|
172
|
+
error: c(m)[t],
|
|
173
|
+
onUpdate: (u) => c(p)(t, u),
|
|
174
|
+
onBlur: () => c($)(t)
|
|
254
175
|
}, () => [
|
|
255
|
-
|
|
176
|
+
T("input", {
|
|
256
177
|
class: "autoform-default-input",
|
|
257
178
|
value: String(a.modelValue[t] ?? ""),
|
|
258
|
-
onInput: (
|
|
259
|
-
onBlur: () =>
|
|
260
|
-
}, null, 40,
|
|
179
|
+
onInput: (u) => c(p)(t, u.target.value),
|
|
180
|
+
onBlur: () => c($)(t)
|
|
181
|
+
}, null, 40, rt)
|
|
261
182
|
]),
|
|
262
|
-
|
|
183
|
+
j(e.$slots, "error", {
|
|
263
184
|
fieldKey: t,
|
|
264
|
-
error:
|
|
185
|
+
error: c(m)[t]
|
|
265
186
|
})
|
|
266
187
|
], 64))
|
|
267
|
-
], 8,
|
|
268
|
-
])
|
|
269
|
-
], 8,
|
|
270
|
-
}
|
|
271
|
-
}), ct = /* @__PURE__ */ z({
|
|
272
|
-
__name: "AutoFormRow",
|
|
273
|
-
props: {
|
|
274
|
-
row: {}
|
|
275
|
-
},
|
|
276
|
-
setup(a) {
|
|
277
|
-
return (k, w) => (i(), d("div", {
|
|
278
|
-
class: "autoform-row",
|
|
279
|
-
style: K({ "--autoform-cols": a.row.length })
|
|
280
|
-
}, [
|
|
281
|
-
(i(!0), d(b, null, R(a.row, (u) => (i(), d("div", {
|
|
282
|
-
key: u,
|
|
283
|
-
class: "autoform-cell"
|
|
284
|
-
}, [
|
|
285
|
-
V(k.$slots, "default", { fieldKey: u })
|
|
286
|
-
]))), 128))
|
|
287
|
-
], 4));
|
|
188
|
+
], 8, et))), 128))
|
|
189
|
+
])
|
|
190
|
+
], 8, tt));
|
|
288
191
|
}
|
|
289
192
|
});
|
|
290
193
|
export {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
tt as useAutoForm
|
|
194
|
+
nt as AutoForm,
|
|
195
|
+
Z as DEFAULT_BREAKPOINTS,
|
|
196
|
+
U as isExplicitLayout,
|
|
197
|
+
K as isShorthandLayout,
|
|
198
|
+
Y as useAutoForm
|
|
297
199
|
};
|
|
298
200
|
//# sourceMappingURL=vue-autoform.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vue-autoform.es.js","sources":["../src/types.ts","../src/composables/useAutoForm.ts","../src/components/AutoForm.vue","../src/components/AutoFormRow.vue"],"sourcesContent":["import type { Component } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\n\nexport type BreakpointKey = 'sm' | 'md' | 'lg' | 'xl' | '2xl'\n\nexport type BreakpointMap = Record<BreakpointKey, number>\n\nexport const DEFAULT_BREAKPOINTS: BreakpointMap = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n '2xl': 1536,\n}\n\nexport interface FieldConfig {\n component: Component\n props?: Record<string, unknown>\n [key: string]: unknown\n}\n\nexport type LayoutRow = string[]\n\nexport type LayoutGrid = LayoutRow[]\n\nexport type ShorthandLayout = { default: number } & Partial<Record<BreakpointKey, number>>\n\nexport type ExplicitLayout = { default: LayoutGrid } & Partial<Record<BreakpointKey, LayoutGrid>>\n\nexport type AutoFormLayout = ShorthandLayout | ExplicitLayout\n\nexport function isShorthandLayout(layout: AutoFormLayout): layout is ShorthandLayout {\n return typeof layout.default === 'number'\n}\n\nexport function isExplicitLayout(layout: AutoFormLayout): layout is ExplicitLayout {\n return Array.isArray(layout.default)\n}\n\nexport interface AutoFormProps {\n schema: ZodObject<ZodRawShape>\n layout?: AutoFormLayout\n fields: Record<string, FieldConfig>\n modelValue: Record<string, unknown>\n validateOn?: 'blur' | 'input' | 'submit'\n breakpoints?: Partial<BreakpointMap>\n}\n\nexport interface FieldRenderInfo {\n key: string\n config: FieldConfig | undefined\n value: unknown\n error: string | undefined\n}\n","import { reactive, computed } from 'vue'\nimport type { Ref } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\nimport type { BreakpointMap, AutoFormLayout, LayoutGrid } from '../types'\nimport {\n DEFAULT_BREAKPOINTS,\n isShorthandLayout,\n isExplicitLayout,\n} from '../types'\n\nexport function useAutoForm(\n schema: ZodObject<ZodRawShape>,\n modelValue: Ref<Record<string, unknown>>,\n emit: (event: 'update:modelValue', value: Record<string, unknown>) => void,\n validateOn: Ref<'blur' | 'input' | 'submit'>,\n layout: Ref<AutoFormLayout | undefined>,\n breakpointsOverride: Ref<Partial<BreakpointMap> | undefined>,\n) {\n const errors = reactive<Record<string, string>>({})\n\n const breakpoints = computed<BreakpointMap>(() => ({\n ...DEFAULT_BREAKPOINTS,\n ...breakpointsOverride.value,\n }))\n\n const schemaKeys = computed<string[]>(() => Object.keys(schema.shape))\n\n const resolvedLayout = computed<LayoutGrid>(() => {\n const l = layout.value\n if (!l) {\n return schemaKeys.value.map((key: string) => [key])\n }\n if (isExplicitLayout(l)) {\n return l.default\n }\n if (isShorthandLayout(l)) {\n const cols = l.default\n const keys = schemaKeys.value\n const rows: LayoutGrid = []\n for (let i = 0; i < keys.length; i += cols) {\n rows.push(keys.slice(i, i + cols))\n }\n return rows\n }\n return schemaKeys.value.map((key: string) => [key])\n })\n\n function validateField(key: string, value: unknown): string | undefined {\n const shape = schema.shape\n if (!shape[key]) return undefined\n const fieldSchema = shape[key]\n const result = fieldSchema.safeParse(value)\n if (!result.success) {\n return result.error.errors[0]?.message ?? 'Invalid value'\n }\n return undefined\n }\n\n function validateAll(): boolean {\n const result = schema.safeParse(modelValue.value)\n if (!result.success) {\n result.error.errors.forEach((err) => {\n const key = String(err.path[0])\n errors[key] = err.message\n })\n return false\n }\n Object.keys(errors).forEach((k: string) => delete errors[k])\n return true\n }\n\n function onFieldInput(key: string, value: unknown) {\n const updated = { ...modelValue.value, [key]: value }\n emit('update:modelValue', updated)\n if (validateOn.value === 'input') {\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function onFieldBlur(key: string) {\n if (validateOn.value === 'blur') {\n const msg = validateField(key, modelValue.value[key])\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function getBreakpointLayouts(): Array<{ minWidth: number; grid: LayoutGrid; key: string }> {\n const l = layout.value\n if (!l) return []\n\n const bps = breakpoints.value\n const result: Array<{ minWidth: number; grid: LayoutGrid; key: string }> = []\n\n if (isExplicitLayout(l)) {\n const bpKeys = Object.keys(l).filter((k) => k !== 'default') as Array<keyof typeof l>\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap]\n const grid = l[bpKey as keyof typeof l] as LayoutGrid\n if (minWidth && grid) {\n result.push({ minWidth, grid, key: bpKey })\n }\n }\n } else if (isShorthandLayout(l)) {\n const bpKeys = Object.keys(l).filter((k) => k !== 'default') as Array<keyof typeof l>\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap]\n const cols = l[bpKey as keyof typeof l] as number\n if (minWidth && cols) {\n const keys = schemaKeys.value\n const rows: LayoutGrid = []\n for (let i = 0; i < keys.length; i += cols) {\n rows.push(keys.slice(i, i + cols))\n }\n result.push({ minWidth, grid: rows, key: bpKey })\n }\n }\n }\n\n return result.sort((a, b) => a.minWidth - b.minWidth)\n }\n\n return {\n errors,\n resolvedLayout,\n schemaKeys,\n onFieldInput,\n onFieldBlur,\n validateAll,\n getBreakpointLayouts,\n }\n}\n","<template>\n <div class=\"autoform\" :data-autoform-id=\"formId\">\n <component :is=\"'style'\" v-if=\"mediaQueryCss\">{{\n mediaQueryCss\n }}</component>\n\n <!-- Shorthand layout: single flat grid, CSS handles column count per breakpoint -->\n <div v-if=\"isShorthand\" class=\"autoform-flat-grid\">\n <div v-for=\"fieldKey in flatKeys\" :key=\"fieldKey\" class=\"autoform-cell\">\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"fields[fieldKey].props ?? {}\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n\n <!-- Explicit layout / no layout: flat grid, CSS positions each field per breakpoint -->\n <div v-else class=\"autoform-flat-grid\">\n <div\n v-for=\"fieldKey in flatKeys\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n :data-field-key=\"fieldKey\"\n >\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"fields[fieldKey].props ?? {}\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { computed, ref, toRef } from \"vue\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport { useAutoForm } from \"../composables/useAutoForm\";\nimport type {\n AutoFormLayout,\n BreakpointMap,\n FieldConfig,\n LayoutGrid,\n} from \"../types\";\nimport {\n isExplicitLayout,\n isShorthandLayout,\n DEFAULT_BREAKPOINTS,\n} from \"../types\";\n\nconst props = withDefaults(\n defineProps<{\n schema: ZodObject<ZodRawShape>;\n layout?: AutoFormLayout;\n fields: Record<string, FieldConfig>;\n modelValue: Record<string, unknown>;\n validateOn?: \"blur\" | \"input\" | \"submit\";\n breakpoints?: Partial<BreakpointMap>;\n }>(),\n {\n validateOn: \"blur\",\n },\n);\n\nconst emit = defineEmits<{\n (e: \"update:modelValue\", value: Record<string, unknown>): void;\n}>();\n\nconst formId = ref(`autoform-${Math.random().toString(36).slice(2, 9)}`);\n\nconst { errors, schemaKeys, onFieldInput, onFieldBlur, validateAll } =\n useAutoForm(\n props.schema,\n toRef(props, \"modelValue\"),\n (_, val) => emit(\"update:modelValue\", val),\n toRef(props, \"validateOn\"),\n toRef(props, \"layout\"),\n toRef(props, \"breakpoints\"),\n );\n\nconst isShorthand = computed(() =>\n props.layout ? isShorthandLayout(props.layout) : false,\n);\n\nconst flatKeys = computed(() => schemaKeys.value);\n\nconst mediaQueryCss = computed<string>(() => {\n if (!props.layout) return \"\";\n\n const bps: BreakpointMap = { ...DEFAULT_BREAKPOINTS, ...props.breakpoints };\n const id = formId.value;\n const lines: string[] = [];\n\n if (isShorthandLayout(props.layout)) {\n const layout = props.layout;\n // Base columns for the flat grid\n lines.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${layout.default}, minmax(0, 1fr));`,\n `}`,\n );\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const cols = layout[bpKey as keyof typeof layout] as number;\n if (minWidth && cols) {\n lines.push(\n `@media (min-width: ${minWidth}px) {`,\n ` [data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${cols}, minmax(0, 1fr));`,\n ` }`,\n `}`,\n );\n }\n }\n } else if (isExplicitLayout(props.layout)) {\n const layout = props.layout;\n // Helper: build CSS rules for a given grid at a given scope (media query or base)\n const gridToCss = (grid: LayoutGrid, wrap?: string): string => {\n const maxCols = Math.max(...grid.map((r: string[]) => r.length));\n const allKeys = schemaKeys.value;\n const keysInGrid = new Set(grid.flat());\n const fieldRules: string[] = [];\n\n // Set grid column count\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid { grid-template-columns: repeat(${maxCols}, minmax(0, 1fr)); }`,\n );\n\n // Hide fields not in this layout\n allKeys.forEach((key: string) => {\n if (!keysInGrid.has(key)) {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: none; }`,\n );\n }\n });\n\n // Position each field\n let rowStart = 1;\n grid.forEach((row: string[]) => {\n let colStart = 1;\n row.forEach((key: string) => {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: block; grid-row: ${rowStart}; grid-column: ${colStart}; }`,\n );\n colStart++;\n });\n rowStart++;\n });\n\n if (wrap) {\n return [\n `@media (min-width: ${wrap}px) {`,\n ...fieldRules.map((r) => ` ${r}`),\n `}`,\n ].join(\"\\n\");\n }\n return fieldRules.join(\"\\n\");\n };\n\n // Default layout base styles\n lines.push(gridToCss(layout.default));\n\n // Per-breakpoint overrides\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n const sortedBpKeys = bpKeys.sort(\n (a, b) =>\n (bps[a as keyof BreakpointMap] ?? 0) -\n (bps[b as keyof BreakpointMap] ?? 0),\n );\n for (const bpKey of sortedBpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const grid = layout[bpKey as keyof typeof layout] as LayoutGrid;\n if (minWidth && grid) {\n lines.push(gridToCss(grid, String(minWidth)));\n }\n }\n }\n\n return lines.join(\"\\n\");\n});\n\ndefineExpose({ validateAll, errors });\n</script>\n\n<style>\n.autoform {\n width: 100%;\n display: flex;\n flex-direction: column;\n gap: var(--autoform-row-gap, 1rem);\n}\n\n.autoform-flat-grid {\n display: grid;\n grid-template-columns: repeat(1, minmax(0, 1fr));\n gap: var(--autoform-gap, 1rem);\n width: 100%;\n}\n\n.autoform-flat-grid .autoform-cell {\n min-width: 0;\n}\n\n.autoform-default-input {\n width: 100%;\n padding: 0.5rem 0.75rem;\n border: 1px solid #d1d5db;\n border-radius: 0.375rem;\n font-size: 1rem;\n outline: none;\n box-sizing: border-box;\n}\n\n.autoform-default-input:focus {\n border-color: #6366f1;\n box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);\n}\n</style>\n","<template>\n <div\n class=\"autoform-row\"\n :style=\"{ '--autoform-cols': row.length }\"\n >\n <div\n v-for=\"fieldKey in row\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n >\n <slot :fieldKey=\"fieldKey\" />\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\ndefineProps<{\n row: string[]\n}>()\n</script>\n\n<style>\n.autoform-row {\n display: grid;\n grid-template-columns: repeat(var(--autoform-cols, 1), minmax(0, 1fr));\n gap: var(--autoform-gap, 1rem);\n width: 100%;\n}\n\n.autoform-cell {\n min-width: 0;\n}\n</style>\n"],"names":["DEFAULT_BREAKPOINTS","isShorthandLayout","layout","isExplicitLayout","useAutoForm","schema","modelValue","emit","validateOn","breakpointsOverride","errors","reactive","breakpoints","computed","schemaKeys","resolvedLayout","l","key","cols","keys","rows","i","validateField","value","shape","result","_a","validateAll","k","err","onFieldInput","updated","msg","onFieldBlur","getBreakpointLayouts","bps","bpKeys","bpKey","minWidth","grid","a","b","props","__props","__emit","formId","ref","toRef","_","val","isShorthand","flatKeys","mediaQueryCss","id","lines","gridToCss","wrap","maxCols","r","allKeys","keysInGrid","fieldRules","rowStart","row","colStart","sortedBpKeys","__expose","_createElementBlock","_openBlock","_createBlock","_resolveDynamicComponent","_hoisted_2","_Fragment","_renderList","fieldKey","_mergeProps","_unref","_renderSlot","_ctx","_createElementVNode","_hoisted_4","_normalizeStyle"],"mappings":";AAOO,MAAMA,IAAqC;AAAA,EAChD,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,OAAO;AACT;AAkBO,SAASC,EAAkBC,GAAmD;AACnF,SAAO,OAAOA,EAAO,WAAY;AACnC;AAEO,SAASC,EAAiBD,GAAkD;AACjF,SAAO,MAAM,QAAQA,EAAO,OAAO;AACrC;AC3BO,SAASE,GACdC,GACAC,GACAC,GACAC,GACAN,GACAO,GACA;AACA,QAAMC,IAASC,EAAiC,EAAE,GAE5CC,IAAcC,EAAwB,OAAO;AAAA,IACjD,GAAGb;AAAA,IACH,GAAGS,EAAoB;AAAA,EAAA,EACvB,GAEIK,IAAaD,EAAmB,MAAM,OAAO,KAAKR,EAAO,KAAK,CAAC,GAE/DU,IAAiBF,EAAqB,MAAM;AAChD,UAAMG,IAAId,EAAO;AACjB,QAAI,CAACc;AACH,aAAOF,EAAW,MAAM,IAAI,CAACG,MAAgB,CAACA,CAAG,CAAC;AAEpD,QAAId,EAAiBa,CAAC;AACpB,aAAOA,EAAE;AAEX,QAAIf,EAAkBe,CAAC,GAAG;AACxB,YAAME,IAAOF,EAAE,SACTG,IAAOL,EAAW,OAClBM,IAAmB,CAAA;AACzB,eAASC,IAAI,GAAGA,IAAIF,EAAK,QAAQE,KAAKH;AACpC,QAAAE,EAAK,KAAKD,EAAK,MAAME,GAAGA,IAAIH,CAAI,CAAC;AAEnC,aAAOE;AAAA,IACT;AACA,WAAON,EAAW,MAAM,IAAI,CAACG,MAAgB,CAACA,CAAG,CAAC;AAAA,EACpD,CAAC;AAED,WAASK,EAAcL,GAAaM,GAAoC;;AACtE,UAAMC,IAAQnB,EAAO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","sources":["../src/composables/useAutoForm.ts","../src/types.ts","../src/components/AutoForm.vue"],"sourcesContent":["import { reactive, computed } from 'vue'\nimport type { Ref } from 'vue'\nimport type { ZodObject, ZodRawShape } from 'zod'\n\nexport function useAutoForm(\n schema: ZodObject<ZodRawShape>,\n modelValue: Ref<Record<string, unknown>>,\n emit: (event: 'update:modelValue', value: Record<string, unknown>) => void,\n validateOn: Ref<'blur' | 'input' | 'submit'>,\n) {\n const errors = reactive<Record<string, string>>({})\n\n const schemaKeys = computed<string[]>(() => Object.keys(schema.shape))\n\n function validateField(key: string, value: unknown): string | undefined {\n const shape = schema.shape\n if (!(key in shape)) return undefined\n const fieldSchema = shape[key]\n const result = fieldSchema.safeParse(value)\n if (!result.success) {\n return result.error.errors[0]?.message ?? 'Invalid value'\n }\n return undefined\n }\n\n function validateAll(): boolean {\n const result = schema.safeParse(modelValue.value)\n if (!result.success) {\n result.error.errors.forEach((err) => {\n const key = String(err.path[0])\n errors[key] = err.message\n })\n return false\n }\n Object.keys(errors).forEach((k: string) => delete errors[k])\n return true\n }\n\n const lastValue: Record<string, unknown> = {}\n\n function onFieldInput(key: string, value: unknown) {\n lastValue[key] = value\n const updated = { ...modelValue.value, [key]: value }\n emit('update:modelValue', updated)\n if (validateOn.value === 'input') {\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n function onFieldBlur(key: string) {\n if (validateOn.value === 'blur') {\n // Use lastValue if available — modelValue prop may not have propagated yet\n // when blur fires immediately after update:modelValue (e.g. checkbox toggles)\n const value = key in lastValue ? lastValue[key] : modelValue.value[key]\n const msg = validateField(key, value)\n if (msg) {\n errors[key] = msg\n } else {\n delete errors[key]\n }\n }\n }\n\n return {\n errors,\n schemaKeys,\n onFieldInput,\n onFieldBlur,\n validateAll,\n }\n}\n","import type { Component } from 'vue'\n\nexport type BreakpointKey = 'sm' | 'md' | 'lg' | 'xl' | '2xl'\n\nexport type BreakpointMap = Record<BreakpointKey, number>\n\nexport const DEFAULT_BREAKPOINTS: BreakpointMap = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n '2xl': 1536,\n}\n\nexport interface FieldConfig {\n component: Component\n props?: Record<string, unknown> | (() => Record<string, unknown>)\n}\n\nexport type LayoutRow = string[]\n\nexport type LayoutGrid = LayoutRow[]\n\nexport type ShorthandLayout = { default: number } & Partial<Record<BreakpointKey, number>>\n\nexport type ExplicitLayout = { default: LayoutGrid } & Partial<Record<BreakpointKey, LayoutGrid>>\n\nexport type AutoFormLayout = ShorthandLayout | ExplicitLayout\n\nexport function isShorthandLayout(layout: AutoFormLayout): layout is ShorthandLayout {\n return typeof layout.default === 'number'\n}\n\nexport function isExplicitLayout(layout: AutoFormLayout): layout is ExplicitLayout {\n return Array.isArray(layout.default)\n}\n\nexport interface FieldRenderInfo {\n key: string\n config: FieldConfig | undefined\n value: unknown\n error: string | undefined\n}\n","<template>\n <div class=\"autoform\" :data-autoform-id=\"formId\">\n <component :is=\"'style'\" v-if=\"mediaQueryCss\">{{\n mediaQueryCss\n }}</component>\n\n <!-- Flat grid: CSS handles column count (shorthand) or per-field position (explicit/none) -->\n <div class=\"autoform-flat-grid\">\n <div\n v-for=\"fieldKey in schemaKeys\"\n :key=\"fieldKey\"\n class=\"autoform-cell\"\n :data-field-key=\"isExplicit ? fieldKey : undefined\"\n >\n <template v-if=\"fields[fieldKey]\">\n <component\n :is=\"fields[fieldKey].component\"\n :modelValue=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n v-bind=\"resolveProps(fieldKey)\"\n @update:modelValue=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n <template v-else>\n <slot\n :name=\"`field-${fieldKey}`\"\n :fieldKey=\"fieldKey\"\n :value=\"modelValue[fieldKey]\"\n :error=\"errors[fieldKey]\"\n :onUpdate=\"(val: unknown) => onFieldInput(fieldKey, val)\"\n :onBlur=\"() => onFieldBlur(fieldKey)\"\n >\n <input\n class=\"autoform-default-input\"\n :value=\"String(modelValue[fieldKey] ?? '')\"\n @input=\"\n (e: Event) =>\n onFieldInput(fieldKey, (e.target as HTMLInputElement).value)\n \"\n @blur=\"() => onFieldBlur(fieldKey)\"\n />\n </slot>\n <slot name=\"error\" :fieldKey=\"fieldKey\" :error=\"errors[fieldKey]\" />\n </template>\n </div>\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { computed, ref, toRef } from \"vue\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport { useAutoForm } from \"../composables/useAutoForm\";\nimport type {\n AutoFormLayout,\n BreakpointMap,\n FieldConfig,\n LayoutGrid,\n} from \"../types\";\nimport {\n isExplicitLayout,\n isShorthandLayout,\n DEFAULT_BREAKPOINTS,\n} from \"../types\";\n\nconst props = withDefaults(\n defineProps<{\n schema: ZodObject<ZodRawShape>;\n layout?: AutoFormLayout;\n fields: Record<string, FieldConfig>;\n modelValue: Record<string, unknown>;\n validateOn?: \"blur\" | \"input\" | \"submit\";\n breakpoints?: Partial<BreakpointMap>;\n }>(),\n {\n validateOn: \"blur\",\n },\n);\n\nconst emit = defineEmits<{\n (e: \"update:modelValue\", value: Record<string, unknown>): void;\n}>();\n\nconst formId = ref(`autoform-${Math.random().toString(36).slice(2, 9)}`);\n\nconst { errors, schemaKeys, onFieldInput, onFieldBlur, validateAll } =\n useAutoForm(\n props.schema,\n toRef(props, \"modelValue\"),\n (event, val) => emit(event, val),\n toRef(props, \"validateOn\"),\n );\n\nconst isExplicit = computed(() =>\n props.layout ? isExplicitLayout(props.layout) : true,\n);\n\nfunction resolveProps(fieldKey: string): Record<string, unknown> {\n const cfg = props.fields[fieldKey];\n if (!cfg) return {};\n const p = cfg.props;\n return typeof p === \"function\" ? p() : (p ?? {});\n}\n\nconst mediaQueryCss = computed<string>(() => {\n if (!props.layout) return \"\";\n\n const bps: BreakpointMap = { ...DEFAULT_BREAKPOINTS, ...props.breakpoints };\n const id = formId.value;\n const lines: string[] = [];\n\n if (isShorthandLayout(props.layout)) {\n const layout = props.layout;\n // Base columns for the flat grid\n lines.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${layout.default}, minmax(0, 1fr));`,\n `}`,\n );\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n for (const bpKey of bpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const cols = layout[bpKey as keyof typeof layout] as number;\n if (minWidth && cols) {\n lines.push(\n `@media (min-width: ${minWidth}px) {`,\n ` [data-autoform-id=\"${id}\"] .autoform-flat-grid {`,\n ` grid-template-columns: repeat(${cols}, minmax(0, 1fr));`,\n ` }`,\n `}`,\n );\n }\n }\n } else if (isExplicitLayout(props.layout)) {\n const layout = props.layout;\n // Helper: build CSS rules for a given grid at a given scope (media query or base)\n const gridToCss = (grid: LayoutGrid, wrap?: string): string => {\n const maxCols = Math.max(...grid.map((r: string[]) => r.length));\n const allKeys = schemaKeys.value;\n const keysInGrid = new Set(grid.flat());\n const fieldRules: string[] = [];\n\n // Set grid column count\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] .autoform-flat-grid { grid-template-columns: repeat(${maxCols}, minmax(0, 1fr)); }`,\n );\n\n // Hide fields not in this layout\n allKeys.forEach((key: string) => {\n if (!keysInGrid.has(key)) {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: none; }`,\n );\n }\n });\n\n // Position each field\n let rowStart = 1;\n grid.forEach((row: string[]) => {\n let colStart = 1;\n row.forEach((key: string) => {\n fieldRules.push(\n `[data-autoform-id=\"${id}\"] [data-field-key=\"${key}\"] { display: block; grid-row: ${rowStart}; grid-column: ${colStart}; }`,\n );\n colStart++;\n });\n rowStart++;\n });\n\n if (wrap) {\n return [\n `@media (min-width: ${wrap}px) {`,\n ...fieldRules.map((r) => ` ${r}`),\n `}`,\n ].join(\"\\n\");\n }\n return fieldRules.join(\"\\n\");\n };\n\n // Default layout base styles\n lines.push(gridToCss(layout.default));\n\n // Per-breakpoint overrides\n const bpKeys = Object.keys(layout).filter((k) => k !== \"default\") as Array<\n keyof typeof layout\n >;\n const sortedBpKeys = bpKeys.sort(\n (a, b) =>\n (bps[a as keyof BreakpointMap] ?? 0) -\n (bps[b as keyof BreakpointMap] ?? 0),\n );\n for (const bpKey of sortedBpKeys) {\n const minWidth = bps[bpKey as keyof BreakpointMap];\n const grid = layout[bpKey as keyof typeof layout] as LayoutGrid;\n if (minWidth && grid) {\n lines.push(gridToCss(grid, String(minWidth)));\n }\n }\n }\n\n return lines.join(\"\\n\");\n});\n\ndefineExpose({ validateAll, errors });\n</script>\n\n<style>\n.autoform {\n width: 100%;\n}\n\n.autoform-flat-grid {\n display: grid;\n grid-template-columns: repeat(1, minmax(0, 1fr));\n column-gap: var(--autoform-gap, 1rem);\n row-gap: var(--autoform-row-gap, 1rem);\n width: 100%;\n}\n\n.autoform-flat-grid .autoform-cell {\n min-width: 0;\n}\n\n.autoform-default-input {\n width: 100%;\n padding: 0.5rem 0.75rem;\n border: 1px solid #d1d5db;\n border-radius: 0.375rem;\n font-size: 1rem;\n outline: none;\n box-sizing: border-box;\n}\n\n.autoform-default-input:focus {\n border-color: #6366f1;\n box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);\n}\n</style>\n"],"names":["useAutoForm","schema","modelValue","emit","validateOn","errors","reactive","schemaKeys","computed","validateField","key","value","shape","result","_a","validateAll","k","err","lastValue","onFieldInput","updated","msg","onFieldBlur","DEFAULT_BREAKPOINTS","isShorthandLayout","layout","isExplicitLayout","props","__props","__emit","formId","ref","toRef","event","val","isExplicit","resolveProps","fieldKey","cfg","p","mediaQueryCss","bps","id","lines","bpKeys","bpKey","minWidth","cols","gridToCss","grid","wrap","maxCols","r","allKeys","keysInGrid","fieldRules","rowStart","row","colStart","sortedBpKeys","a","b","__expose","_createElementBlock","_openBlock","_createBlock","_resolveDynamicComponent","_createElementVNode","_hoisted_2","_Fragment","_renderList","_unref","_mergeProps","_renderSlot","_ctx","e"],"mappings":";AAIO,SAASA,EACdC,GACAC,GACAC,GACAC,GACA;AACA,QAAMC,IAASC,EAAiC,EAAE,GAE5CC,IAAaC,EAAmB,MAAM,OAAO,KAAKP,EAAO,KAAK,CAAC;AAErE,WAASQ,EAAcC,GAAaC,GAAoC;;AACtE,UAAMC,IAAQX,EAAO;AACrB,QAAI,EAAES,KAAOE,GAAQ;AAErB,UAAMC,IADcD,EAAMF,CAAG,EACF,UAAUC,CAAK;AAC1C,QAAI,CAACE,EAAO;AACV,eAAOC,IAAAD,EAAO,MAAM,OAAO,CAAC,MAArB,gBAAAC,EAAwB,YAAW;AAAA,EAG9C;AAEA,WAASC,IAAuB;AAC9B,UAAMF,IAASZ,EAAO,UAAUC,EAAW,KAAK;AAChD,WAAKW,EAAO,WAOZ,OAAO,KAAKR,CAAM,EAAE,QAAQ,CAACW,MAAc,OAAOX,EAAOW,CAAC,CAAC,GACpD,OAPLH,EAAO,MAAM,OAAO,QAAQ,CAACI,MAAQ;AACnC,YAAMP,IAAM,OAAOO,EAAI,KAAK,CAAC,CAAC;AAC9B,MAAAZ,EAAOK,CAAG,IAAIO,EAAI;AAAA,IACpB,CAAC,GACM;AAAA,EAIX;AAEA,QAAMC,IAAqC,CAAA;AAE3C,WAASC,EAAaT,GAAaC,GAAgB;AACjD,IAAAO,EAAUR,CAAG,IAAIC;AACjB,UAAMS,IAAU,EAAE,GAAGlB,EAAW,OAAO,CAACQ,CAAG,GAAGC,EAAA;AAE9C,QADAR,EAAK,qBAAqBiB,CAAO,GAC7BhB,EAAW,UAAU,SAAS;AAChC,YAAMiB,IAAMZ,EAAcC,GAAKC,CAAK;AACpC,MAAIU,IACFhB,EAAOK,CAAG,IAAIW,IAEd,OAAOhB,EAAOK,CAAG;AAAA,IAErB;AAAA,EACF;AAEA,WAASY,EAAYZ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