From 72f82771f13b9bf550910da64229dc21ad01c753 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Tue, 21 Dec 2021 16:13:53 +0800 Subject: [PATCH] feat: export formInstance type --- components/components.ts | 2 +- components/form/Form.tsx | 28 +++++++++++++++++++++++++--- components/form/index.tsx | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/components/components.ts b/components/components.ts index aa3bbd4ab..ade728c8c 100644 --- a/components/components.ts +++ b/components/components.ts @@ -73,7 +73,7 @@ export { default as Drawer } from './drawer'; export type { EmptyProps } from './empty'; export { default as Empty } from './empty'; -export type { FormProps, FormItemProps } from './form'; +export type { FormProps, FormItemProps, FormInstance } from './form'; export { default as Form, FormItem, FormItemRest } from './form'; export { default as Grid } from './grid'; diff --git a/components/form/Form.tsx b/components/form/Form.tsx index 95d229a2f..250a7b190 100755 --- a/components/form/Form.tsx +++ b/components/form/Form.tsx @@ -1,4 +1,4 @@ -import type { PropType, ExtractPropTypes, HTMLAttributes } from 'vue'; +import type { PropType, ExtractPropTypes, HTMLAttributes, ComponentPublicInstance } from 'vue'; import { defineComponent, computed, watch, ref } from 'vue'; import PropTypes from '../_util/vue-types'; import classNames from '../_util/classNames'; @@ -89,6 +89,29 @@ export const formProps = { export type FormProps = Partial>; +export type FormExpose = { + resetFields: (name?: NamePath) => void; + clearValidate: (name?: NamePath) => void; + validateFields: ( + nameList?: NamePath[], + options?: ValidateOptions, + ) => Promise<{ + [key: string]: any; + }>; + getFieldsValue: (nameList?: InternalNamePath[] | true) => { + [key: string]: any; + }; + validate: ( + nameList?: NamePath[], + options?: ValidateOptions, + ) => Promise<{ + [key: string]: any; + }>; + scrollToField: (name: NamePath, options?: {}) => void; +}; + +export type FormInstance = ComponentPublicInstance; + function isEqualName(name1: NamePath, name2: NamePath) { return isEqual(toArray(name1), toArray(name2)); } @@ -328,7 +351,6 @@ const Form = defineComponent({ }); } }; - expose({ resetFields, clearValidate, @@ -336,7 +358,7 @@ const Form = defineComponent({ getFieldsValue, validate, scrollToField, - }); + } as FormExpose); useProvideForm({ model: computed(() => props.model), diff --git a/components/form/index.tsx b/components/form/index.tsx index 83a75f858..3c03ef833 100644 --- a/components/form/index.tsx +++ b/components/form/index.tsx @@ -5,7 +5,7 @@ import useForm from './useForm'; import FormItemRest, { useInjectFormItemContext } from './FormItemContext'; export type { Rule, RuleObject } from './interface'; -export type { FormProps } from './Form'; +export type { FormProps, FormInstance } from './Form'; export type { FormItemProps } from './FormItem'; Form.useInjectFormItemContext = useInjectFormItemContext;