Skip to content

Commit 158b802

Browse files
committed
feat: add an option to customize default components for fields
1 parent 1289ef0 commit 158b802

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/runtime/components/AutoForm.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts" generic="T extends z.ZodObject<any>">
22
import type { FormSubmitEvent, InferInput, InferOutput } from '@nuxt/ui'
3-
import type { AutoFormConfig } from '../types'
3+
import type { AutoFormConfig, ComponentsMap } from '../types'
44
import { useAppConfig } from '#app'
55
import UButton from '@nuxt/ui/components/Button.vue'
66
import UCheckbox from '@nuxt/ui/components/Checkbox.vue'
@@ -43,7 +43,7 @@ interface ComponentDefinition {
4343
componentProps?: Record<string, any>
4444
}
4545
46-
const COMPONENTS_MAP: Record<string, (key: string, zodType: any) => ComponentDefinition | null> = {
46+
const COMPONENTS_MAP: ComponentsMap = {
4747
number: () => ({ component: UInputNumber }),
4848
string: () => ({ component: UInput }),
4949
boolean: () => ({ component: UCheckbox }),
@@ -99,7 +99,7 @@ function parseMeta(meta: any, key: string) {
9999
100100
function mapZodTypeToComponent(key: string, zodType: any): ComponentDefinition | null {
101101
const zodTypeKey = zodType._def.format ?? zodType._def.type
102-
const component = COMPONENTS_MAP[zodTypeKey]
102+
const component = defu(useAppConfig().autoForm?.components, COMPONENTS_MAP)[zodTypeKey]
103103
if (!component) {
104104
console.warn(`Unsupported Zod type: ${zodTypeKey}`)
105105
return null

src/runtime/types/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import type { ButtonProps } from '@nuxt/ui'
44
import type { ConcreteComponent } from '@vue/runtime-core'
55

6+
export type ComponentsMap = Record<string, (key: string, zodType: any) => ComponentDefinition | null>
7+
68
export interface AutoFormConfig {
79
/** Submit button configuration */
810
submit?: {
@@ -15,6 +17,17 @@ export interface AutoFormConfig {
1517
/** Props to pass to the Nuxt UI `UButton` component */
1618
props?: ButtonProps
1719
}
20+
/**
21+
* Customize the default components used for rendering form fields.
22+
*
23+
* @example
24+
* ```ts
25+
* components: {
26+
* email: () => ({ component: UInput, componentProps: { type: 'email' } }),
27+
* }
28+
* ```
29+
*/
30+
components?: ComponentsMap
1831
}
1932

2033
declare module 'nuxt/schema' {

src/runtime/types/zod.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ declare module 'zod' {
2323
component?: string | Component
2424
/**
2525
* Props forwarded to the input component at render time.
26-
* These are merged with the module's default props - user props take precedence..
26+
* These are merged with the module's default props - user props take precedence.
2727
*/
2828
props?: Record<string, any>
2929
}

0 commit comments

Comments
 (0)