feat: export formInstance type

doc-form
tangjinzhou 2021-12-21 16:13:53 +08:00
parent 89435951ae
commit 72f82771f1
3 changed files with 27 additions and 5 deletions

View File

@ -73,7 +73,7 @@ export { default as Drawer } from './drawer';
export type { EmptyProps } from './empty'; export type { EmptyProps } from './empty';
export { default as Empty } 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 Form, FormItem, FormItemRest } from './form';
export { default as Grid } from './grid'; export { default as Grid } from './grid';

View File

@ -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 { defineComponent, computed, watch, ref } from 'vue';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
@ -89,6 +89,29 @@ export const formProps = {
export type FormProps = Partial<ExtractPropTypes<typeof formProps>>; export type FormProps = Partial<ExtractPropTypes<typeof formProps>>;
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<FormProps, FormExpose>;
function isEqualName(name1: NamePath, name2: NamePath) { function isEqualName(name1: NamePath, name2: NamePath) {
return isEqual(toArray(name1), toArray(name2)); return isEqual(toArray(name1), toArray(name2));
} }
@ -328,7 +351,6 @@ const Form = defineComponent({
}); });
} }
}; };
expose({ expose({
resetFields, resetFields,
clearValidate, clearValidate,
@ -336,7 +358,7 @@ const Form = defineComponent({
getFieldsValue, getFieldsValue,
validate, validate,
scrollToField, scrollToField,
}); } as FormExpose);
useProvideForm({ useProvideForm({
model: computed(() => props.model), model: computed(() => props.model),

View File

@ -5,7 +5,7 @@ import useForm from './useForm';
import FormItemRest, { useInjectFormItemContext } from './FormItemContext'; import FormItemRest, { useInjectFormItemContext } from './FormItemContext';
export type { Rule, RuleObject } from './interface'; export type { Rule, RuleObject } from './interface';
export type { FormProps } from './Form'; export type { FormProps, FormInstance } from './Form';
export type { FormItemProps } from './FormItem'; export type { FormItemProps } from './FormItem';
Form.useInjectFormItemContext = useInjectFormItemContext; Form.useInjectFormItemContext = useInjectFormItemContext;