Skip to content

Commit

Permalink
fix(vue): fix unexpected dep collection during $mount() #3015 (#3065)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcturus011 authored Apr 25, 2022
1 parent 43fbf03 commit b2128ae
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/vue/src/components/ReactiveField.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { inject, provide, Ref, ref, shallowRef, watch } from 'vue-demi'
import { inject, provide, Ref, ref, shallowRef, watch, isVue2 } from 'vue-demi'
import { GeneralField, isVoidField } from '@formily/core'
import { FormPath } from '@formily/shared'
import { observer } from '@formily/reactive-vue'
import { toJS } from '@formily/reactive'
import { toJS, reaction } from '@formily/reactive'
import { SchemaOptionsSymbol, FieldSymbol, h, Fragment } from '../shared'
import { useAttach } from '../hooks/useAttach'
import { useField, useForm } from '../hooks'
Expand Down Expand Up @@ -93,6 +93,17 @@ const mergeSlots = (
return patchedSlots
}

const createFieldInVue2 = (innerCreateField) => {
return () => {
let res: GeneralField
const disposer = reaction(() => {
res = innerCreateField()
})
disposer()
return res
}
}

export default observer({
name: 'ReactiveField',
props: {
Expand All @@ -109,11 +120,16 @@ export default observer({
const formRef = useForm()
const parentRef = useField()
const optionsRef = inject(SchemaOptionsSymbol, ref(null))
const createField = () =>
let createField = () =>
formRef?.value?.[`create${props.fieldType}`]?.({
...props.fieldProps,
basePath: props.fieldProps?.basePath ?? parentRef.value?.address,
})

if (isVue2) {
createField = createFieldInVue2(createField)
}

const fieldRef = shallowRef(createField()) as Ref<GeneralField>
watch(
() => props.fieldProps,
Expand Down

0 comments on commit b2128ae

Please sign in to comment.