Skip to content

Commit df5d37f

Browse files
authored
feat(forms): get types for optional, map options for NativeEnum, map … (#251)
* feat(forms): get types for optional, map options for NativeEnum, map additional metadata * Create calm-actors-kneel.md
1 parent 7b0d09a commit df5d37f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

.changeset/calm-actors-kneel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@saas-ui/forms": patch
3+
---
4+
5+
feat(forms): get types for optional, map options for NativeEnum, map …

packages/saas-ui-forms/zod/src/zod-resolver.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ExtraProps = {
1414
const getType = (
1515
field: z.ZodTypeAny
1616
): 'array' | 'object' | 'number' | 'date' | 'select' | 'text' => {
17-
if (field._def.typeName === 'ZodDefault') {
17+
if (['ZodDefault', 'ZodOptional'].includes(field._def.typeName)) {
1818
return getType(field._def.innerType)
1919
}
2020

@@ -27,6 +27,7 @@ const getType = (
2727
return 'number'
2828
case 'ZodDate':
2929
return 'date'
30+
case 'ZodNativeEnum':
3031
case 'ZodEnum':
3132
return 'select'
3233
case 'ZodString':
@@ -73,6 +74,8 @@ export const getFieldsFromSchema = (schema: z.ZodTypeAny): FieldProps[] => {
7374
props.options = def.values.map((value: string) => {
7475
return { label: value, value }
7576
})
77+
} else if (def.typeName === 'ZodNativeEnum') {
78+
props.options = Object.entries(def.values).map(([label, value]) => ({ label, value: value as string }))
7679
}
7780

7881
const meta = field.description && zodParseMeta(field.description)
@@ -81,6 +84,8 @@ export const getFieldsFromSchema = (schema: z.ZodTypeAny): FieldProps[] => {
8184
name,
8285
label: meta?.label || field.description || name,
8386
type: meta?.type || getType(field),
87+
help: meta?.help,
88+
placeholder: meta?.placeholder,
8489
defaultValue: field._def.defaultValue?.(),
8590
...props,
8691
})
@@ -108,22 +113,37 @@ export interface ZodMeta {
108113
* The label of the field
109114
*/
110115
label: string
116+
111117
/**
112118
* The type of the field
113119
*/
114120
type?: string
121+
122+
/**
123+
* The placeholder of the field
124+
*/
125+
placeholder?: string
126+
127+
/**
128+
* The help text of the field
129+
*/
130+
help?: string
131+
115132
/**
116133
* Object field column count
117134
*/
118135
columns?: number
136+
119137
/**
120138
* Array field min rows
121139
*/
122140
min?: number
141+
123142
/**
124143
* Array field max rows
125144
*/
126145
max?: number
146+
127147
[key: string]: any
128148
}
129149

0 commit comments

Comments
 (0)