From 20cd96e400facdabe0328a9b8c4718da2c3727f2 Mon Sep 17 00:00:00 2001 From: zkwolf Date: Sun, 7 Feb 2021 18:32:29 +0800 Subject: [PATCH] fix: improve form type definition #3638 (#3669) --- components/form/Form.tsx | 8 +++++--- components/form/FormItem.tsx | 16 +++++++++++++--- components/form/index.tsx | 3 ++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/components/form/Form.tsx b/components/form/Form.tsx index caabb1895..2961a52fa 100755 --- a/components/form/Form.tsx +++ b/components/form/Form.tsx @@ -1,4 +1,4 @@ -import { defineComponent, inject, provide, PropType, computed } from 'vue'; +import { defineComponent, inject, provide, PropType, computed, ExtractPropTypes } from 'vue'; import PropTypes from '../_util/vue-types'; import classNames from '../_util/classNames'; import warning from '../_util/warning'; @@ -43,7 +43,7 @@ export type ValidationRule = { trigger?: string; }; -export const FormProps = { +export const formProps = { layout: PropTypes.oneOf(tuple('horizontal', 'inline', 'vertical')), labelCol: { type: Object as PropType }, wrapperCol: { type: Object as PropType }, @@ -64,6 +64,8 @@ export const FormProps = { validateTrigger: { type: [String, Array] as PropType }, }; +export type FormProps = Partial>; + function isEqualName(name1: NamePath, name2: NamePath) { return isEqual(toArray(name1), toArray(name2)); } @@ -71,7 +73,7 @@ function isEqualName(name1: NamePath, name2: NamePath) { const Form = defineComponent({ name: 'AForm', inheritAttrs: false, - props: initDefaultProps(FormProps, { + props: initDefaultProps(formProps, { layout: 'horizontal', hideRequiredMark: false, colon: true, diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index 198a9c980..3f9c05598 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -1,4 +1,12 @@ -import { inject, provide, PropType, defineComponent, computed, nextTick } from 'vue'; +import { + inject, + provide, + PropType, + defineComponent, + computed, + nextTick, + ExtractPropTypes, +} from 'vue'; import cloneDeep from 'lodash-es/cloneDeep'; import PropTypes from '../_util/vue-types'; import classNames from '../_util/classNames'; @@ -66,7 +74,7 @@ function getPropByPath(obj: any, namePathList: any, strict?: boolean) { v: tempObj ? tempObj[keyArr[i]] : undefined, }; } -export const FormItemProps = { +export const formItemProps = { id: PropTypes.string, htmlFor: PropTypes.string, prefixCls: PropTypes.string, @@ -89,12 +97,14 @@ export const FormItemProps = { messageVariables: { type: Object as PropType> }, }; +export type FormItemProps = Partial>; + export default defineComponent({ name: 'AFormItem', mixins: [BaseMixin], inheritAttrs: false, __ANT_NEW_FORM_ITEM: true, - props: FormItemProps, + props: formItemProps, setup(props) { const FormContext = inject('FormContext', {}) as any; const fieldName = computed(() => props.name || props.prop); diff --git a/components/form/index.tsx b/components/form/index.tsx index 5495e7dc3..c322a387b 100644 --- a/components/form/index.tsx +++ b/components/form/index.tsx @@ -1,7 +1,8 @@ import { App, Plugin } from 'vue'; import Form from './Form'; -export { FormProps } from './Form'; +export { FormProps, formProps } from './Form'; +export { FormItemProps, formItemProps } from './FormItem'; /* istanbul ignore next */ Form.install = function(app: App) {