Skip to content

Commit aa91775

Browse files
committed
refactor: create parseMeta utility function
1 parent 2438442 commit aa91775

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/runtime/components/AutoForm.vue

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ const fields = Object.entries(shape).map(([key, zodType]) =>
4444
getComponentForZodType(key, zodType),
4545
)
4646
47+
function parseMeta(zodType: any, key: string) {
48+
const meta = typeof zodType.meta === 'function' ? zodType.meta() || {} : {}
49+
50+
return {
51+
label: meta.title ?? upperFirst(splitByCase(key).join(' ').toLowerCase()),
52+
description: meta.description,
53+
class: meta.autoForm?.floatRight ? 'flex items-center justify-between text-left' : '',
54+
}
55+
}
56+
4757
function getComponentForZodType(key: string, zodType: any) {
4858
let component = UInput as any
4959
let componentProps = {}
@@ -67,9 +77,10 @@ function getComponentForZodType(key: string, zodType: any) {
6777
const result = getComponentForZodType(key, zodType.unwrap()) as any
6878
result.props.multiple = true
6979
70-
// todo: method for parsing meta
71-
const meta = zodType.meta() || {}
72-
result.formField.label = meta.title ?? key
80+
result.formField = {
81+
...result.formField,
82+
...parseMeta(zodType, key),
83+
}
7384
return result
7485
}
7586
}
@@ -84,14 +95,12 @@ function getComponentForZodType(key: string, zodType: any) {
8495
console.warn('Unknown zod type', zodType.constructor.name)
8596
}
8697
87-
const meta = zodType.meta() || {}
98+
const meta = parseMeta(zodType, key)
8899
return {
89100
key,
90101
formField: {
91102
name: key,
92-
label: meta.title ?? upperFirst(splitByCase(key).join(' ').toLowerCase()),
93-
description: meta.description,
94-
class: meta.autoForm?.floatRight ? 'flex items-center justify-between text-left' : '',
103+
...meta,
95104
},
96105
component,
97106
props: componentProps,

0 commit comments

Comments
 (0)