Skip to content

Commit

Permalink
fix(vue): fix unexpected dep collection during $mount() alibaba#3015
Browse files Browse the repository at this point in the history
  • Loading branch information
arcturus011 committed Apr 23, 2022
1 parent 34e9420 commit 4424603
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/vue/src/components/ReactiveField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { inject, provide, Ref, ref, shallowRef, watch } 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 { isVue2, onUnmounted } from 'vue-demi'
import { SchemaOptionsSymbol, FieldSymbol, h, Fragment } from '../shared'
import { useAttach } from '../hooks/useAttach'
import { useField, useForm } from '../hooks'
Expand Down Expand Up @@ -93,6 +94,21 @@ const mergeSlots = (
return patchedSlots
}

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

export default observer({
name: 'ReactiveField',
props: {
Expand All @@ -109,11 +125,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 4424603

Please sign in to comment.