-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue): support useParentForm hook (#2306)
- Loading branch information
Showing
12 changed files
with
198 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# useParentForm | ||
|
||
## 描述 | ||
|
||
用于读取最近的 Form 或者 ObjectField 实例,主要方便于调用子表单的 submit/validate | ||
|
||
## 签名 | ||
|
||
```ts | ||
interface useParentForm { | ||
(): Form | ObjectField | ||
} | ||
``` | ||
|
||
## 用例 | ||
|
||
<dumi-previewer demoPath="api/hooks/use-parent-form" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template> | ||
<FormProvider :form="form"> | ||
<ObjectField name="object"> | ||
<Custom></Custom> | ||
</ObjectField> | ||
<Custom></Custom> | ||
<VoidField name="void"> | ||
<Custom></Custom> | ||
</VoidField> | ||
</FormProvider> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent, h } from '@vue/composition-api' | ||
import { createForm } from '@formily/core' | ||
import { | ||
FormProvider, | ||
ObjectField, | ||
VoidField, | ||
useParentForm, | ||
} from '@formily/vue' | ||
import { observer } from '@formily/reactive-vue' | ||
const Custom = observer( | ||
defineComponent({ | ||
setup() { | ||
const formRef = useParentForm() | ||
return () => { | ||
const form = formRef.value | ||
return h('div', {}, [form.displayName]) | ||
} | ||
}, | ||
}) | ||
) | ||
export default { | ||
components: { | ||
FormProvider, | ||
ObjectField, | ||
VoidField, | ||
VoidField, | ||
Custom, | ||
}, | ||
data() { | ||
const form = createForm() | ||
return { | ||
form, | ||
} | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isObjectField, GeneralField, Form, ObjectField } from '@formily/core' | ||
import { computed, Ref } from 'vue-demi' | ||
import { useField } from './useField' | ||
import { useForm } from './useForm' | ||
|
||
export const useParentForm = (): Ref<Form | ObjectField> => { | ||
const field = useField() | ||
const form = useForm() | ||
const findObjectParent = (field: GeneralField) => { | ||
if (!field) return form.value | ||
if (isObjectField(field)) return field | ||
return findObjectParent(field?.parent) | ||
} | ||
return computed(() => findObjectParent(field.value)) | ||
} |