1
- import type { PropType , ExtractPropTypes , HTMLAttributes , ComponentPublicInstance } from 'vue' ;
1
+ import type { ExtractPropTypes , HTMLAttributes , ComponentPublicInstance } from 'vue' ;
2
2
import { defineComponent , computed , watch , ref } from 'vue' ;
3
3
import PropTypes from '../_util/vue-types' ;
4
4
import classNames from '../_util/classNames' ;
@@ -13,7 +13,15 @@ import isEqual from 'lodash-es/isEqual';
13
13
import type { Options } from 'scroll-into-view-if-needed' ;
14
14
import scrollIntoView from 'scroll-into-view-if-needed' ;
15
15
import initDefaultProps from '../_util/props-util/initDefaultProps' ;
16
- import { tuple } from '../_util/type' ;
16
+ import {
17
+ anyType ,
18
+ booleanType ,
19
+ functionType ,
20
+ objectType ,
21
+ someType ,
22
+ stringType ,
23
+ tuple ,
24
+ } from '../_util/type' ;
17
25
import type { ColProps } from '../grid/Col' ;
18
26
import type {
19
27
InternalNamePath ,
@@ -31,7 +39,9 @@ import { useProvideForm } from './context';
31
39
import type { SizeType } from '../config-provider' ;
32
40
import useForm from './useForm' ;
33
41
import { useInjectGlobalForm } from '../config-provider/context' ;
34
-
42
+ import useStyle from './style' ;
43
+ import { useProviderSize } from '../config-provider/SizeContext' ;
44
+ import { useProviderDisabled } from '../config-provider/DisabledContext' ;
35
45
export type RequiredMark = boolean | 'optional' ;
36
46
export type FormLayout = 'horizontal' | 'inline' | 'vertical' ;
37
47
@@ -40,36 +50,31 @@ export type ValidationRule = Rule;
40
50
41
51
export const formProps = ( ) => ( {
42
52
layout : PropTypes . oneOf ( tuple ( 'horizontal' , 'inline' , 'vertical' ) ) ,
43
- labelCol : { type : Object as PropType < ColProps & HTMLAttributes > } ,
44
- wrapperCol : { type : Object as PropType < ColProps & HTMLAttributes > } ,
45
- colon : { type : Boolean , default : undefined } ,
46
- labelAlign : {
47
- ...PropTypes . oneOf ( tuple ( 'left' , 'right' ) ) ,
48
- type : String as PropType < FormLabelAlign > ,
49
- } ,
50
- labelWrap : { type : Boolean , default : undefined } ,
53
+ labelCol : objectType < ColProps & HTMLAttributes > ( ) ,
54
+ wrapperCol : objectType < ColProps & HTMLAttributes > ( ) ,
55
+ colon : booleanType ( ) ,
56
+ labelAlign : stringType < FormLabelAlign > ( ) ,
57
+ labelWrap : booleanType ( ) ,
51
58
prefixCls : String ,
52
- requiredMark : { type : [ String , Boolean ] as PropType < RequiredMark | '' > , default : undefined } ,
59
+ requiredMark : someType < RequiredMark | '' > ( [ String , Boolean ] ) ,
53
60
/** @deprecated Will warning in future branch. Pls use `requiredMark` instead. */
54
- hideRequiredMark : { type : Boolean , default : undefined } ,
61
+ hideRequiredMark : booleanType ( ) ,
55
62
model : PropTypes . object ,
56
- rules : { type : Object as PropType < { [ k : string ] : Rule [ ] | Rule } > } ,
57
- validateMessages : {
58
- type : Object as PropType < ValidateMessages > ,
59
- default : undefined as ValidateMessages ,
60
- } ,
61
- validateOnRuleChange : { type : Boolean , default : undefined } ,
63
+ rules : objectType < { [ k : string ] : Rule [ ] | Rule } > ( ) ,
64
+ validateMessages : objectType < ValidateMessages > ( ) ,
65
+ validateOnRuleChange : booleanType ( ) ,
62
66
// 提交失败自动滚动到第一个错误字段
63
- scrollToFirstError : { type : [ Boolean , Object ] as PropType < boolean | Options > } ,
64
- onSubmit : Function as PropType < ( e : Event ) => void > ,
67
+ scrollToFirstError : anyType < boolean | Options > ( ) ,
68
+ onSubmit : functionType < ( e : Event ) => void > ( ) ,
65
69
name : String ,
66
- validateTrigger : { type : [ String , Array ] as PropType < string | string [ ] > } ,
67
- size : { type : String as PropType < SizeType > } ,
68
- onValuesChange : { type : Function as PropType < Callbacks [ 'onValuesChange' ] > } ,
69
- onFieldsChange : { type : Function as PropType < Callbacks [ 'onFieldsChange' ] > } ,
70
- onFinish : { type : Function as PropType < Callbacks [ 'onFinish' ] > } ,
71
- onFinishFailed : { type : Function as PropType < Callbacks [ 'onFinishFailed' ] > } ,
72
- onValidate : { type : Function as PropType < Callbacks [ 'onValidate' ] > } ,
70
+ validateTrigger : someType < string | string [ ] > ( [ String , Array ] ) ,
71
+ size : stringType < SizeType > ( ) ,
72
+ disabled : booleanType ( ) ,
73
+ onValuesChange : functionType < Callbacks [ 'onValuesChange' ] > ( ) ,
74
+ onFieldsChange : functionType < Callbacks [ 'onFieldsChange' ] > ( ) ,
75
+ onFinish : functionType < Callbacks [ 'onFinish' ] > ( ) ,
76
+ onFinishFailed : functionType < Callbacks [ 'onFinishFailed' ] > ( ) ,
77
+ onValidate : functionType < Callbacks [ 'onValidate' ] > ( ) ,
73
78
} ) ;
74
79
75
80
export type FormProps = Partial < ExtractPropTypes < ReturnType < typeof formProps > > > ;
@@ -114,7 +119,13 @@ const Form = defineComponent({
114
119
useForm,
115
120
// emits: ['finishFailed', 'submit', 'finish', 'validate'],
116
121
setup ( props , { emit, slots, expose, attrs } ) {
117
- const { prefixCls, direction, form : contextForm , size } = useConfigInject ( 'form' , props ) ;
122
+ const {
123
+ prefixCls,
124
+ direction,
125
+ form : contextForm ,
126
+ size,
127
+ disabled,
128
+ } = useConfigInject ( 'form' , props ) ;
118
129
const requiredMark = computed ( ( ) => props . requiredMark === '' || props . requiredMark ) ;
119
130
const mergedRequiredMark = computed ( ( ) => {
120
131
if ( requiredMark . value !== undefined ) {
@@ -130,6 +141,8 @@ const Form = defineComponent({
130
141
}
131
142
return true ;
132
143
} ) ;
144
+ useProviderSize ( size ) ;
145
+ useProviderDisabled ( disabled ) ;
133
146
const mergedColon = computed ( ( ) => props . colon ?? contextForm . value ?. colon ) ;
134
147
const { validateMessages : globalValidateMessages } = useInjectGlobalForm ( ) ;
135
148
const validateMessages = computed ( ( ) => {
@@ -139,13 +152,21 @@ const Form = defineComponent({
139
152
...props . validateMessages ,
140
153
} ;
141
154
} ) ;
155
+
156
+ // Style
157
+ const [ wrapSSR , hashId ] = useStyle ( prefixCls ) ;
158
+
142
159
const formClassName = computed ( ( ) =>
143
- classNames ( prefixCls . value , {
144
- [ `${ prefixCls . value } -${ props . layout } ` ] : true ,
145
- [ `${ prefixCls . value } -hide-required-mark` ] : mergedRequiredMark . value === false ,
146
- [ `${ prefixCls . value } -rtl` ] : direction . value === 'rtl' ,
147
- [ `${ prefixCls . value } -${ size . value } ` ] : size . value ,
148
- } ) ,
160
+ classNames (
161
+ prefixCls . value ,
162
+ {
163
+ [ `${ prefixCls . value } -${ props . layout } ` ] : true ,
164
+ [ `${ prefixCls . value } -hide-required-mark` ] : mergedRequiredMark . value === false ,
165
+ [ `${ prefixCls . value } -rtl` ] : direction . value === 'rtl' ,
166
+ [ `${ prefixCls . value } -${ size . value } ` ] : size . value ,
167
+ } ,
168
+ hashId . value ,
169
+ ) ,
149
170
) ;
150
171
const lastValidatePromise = ref ( ) ;
151
172
const fields : Record < string , FieldExpose > = { } ;
@@ -380,10 +401,10 @@ const Form = defineComponent({
380
401
) ;
381
402
382
403
return ( ) => {
383
- return (
404
+ return wrapSSR (
384
405
< form { ...attrs } onSubmit = { handleSubmit } class = { [ formClassName . value , attrs . class ] } >
385
406
{ slots . default ?.( ) }
386
- </ form >
407
+ </ form > ,
387
408
) ;
388
409
} ;
389
410
} ,
0 commit comments