Skip to content

Commit

Permalink
feat(VForm): expose component instances
Browse files Browse the repository at this point in the history
closes #19365
  • Loading branch information
KaelWD committed Mar 13, 2024
1 parent 03759b1 commit 56c5c62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/vuetify/src/composables/form.ts
Expand Up @@ -2,17 +2,18 @@
import { useProxiedModel } from '@/composables/proxiedModel'

// Utilities
import { computed, inject, provide, ref, shallowRef, toRef, watch } from 'vue'
import { computed, inject, markRaw, provide, ref, shallowRef, toRef, watch } from 'vue'
import { consoleWarn, propsFactory } from '@/util'

// Types
import type { ComputedRef, InjectionKey, PropType, Ref } from 'vue'
import type { ComponentInternalInstance, ComputedRef, InjectionKey, PropType, Raw, Ref } from 'vue'
import type { ValidationProps } from './validation'
import type { EventProp } from '@/util'

export interface FormProvide {
register: (item: {
id: number | string
vm: ComponentInternalInstance
validate: () => Promise<string[]>
reset: () => void
resetValidation: () => void
Expand All @@ -32,6 +33,7 @@ export interface FormField {
validate: () => Promise<string[]>
reset: () => void
resetValidation: () => void
vm: Raw<ComponentInternalInstance>
isValid: boolean | null
errorMessages: string[]
}
Expand Down Expand Up @@ -141,7 +143,7 @@ export function createForm (props: FormProps) {
}, { deep: true, flush: 'post' })

provide(FormKey, {
register: ({ id, validate, reset, resetValidation }) => {
register: ({ id, vm, validate, reset, resetValidation }) => {
if (items.value.some(item => item.id === id)) {
consoleWarn(`Duplicate input name "${id}"`)
}
Expand All @@ -151,6 +153,7 @@ export function createForm (props: FormProps) {
validate,
reset,
resetValidation,
vm: markRaw(vm),
isValid: null,
errorMessages: [],
})
Expand Down
4 changes: 3 additions & 1 deletion packages/vuetify/src/composables/validation.ts
Expand Up @@ -6,7 +6,7 @@ import { useToggleScope } from '@/composables/toggleScope'

// Utilities
import { computed, nextTick, onBeforeMount, onBeforeUnmount, onMounted, ref, shallowRef, unref, watch } from 'vue'
import { getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util'
import { getCurrentInstance, getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util'

// Types
import type { PropType } from 'vue'
Expand Down Expand Up @@ -120,11 +120,13 @@ export function useValidation (
}
})

const vm = getCurrentInstance('validation')
const uid = computed(() => props.name ?? unref(id))

onBeforeMount(() => {
form?.register({
id: uid.value,
vm,
validate,
reset,
resetValidation,
Expand Down

0 comments on commit 56c5c62

Please sign in to comment.