@@ -5,7 +5,7 @@ import classNames from 'classnames';
5
5
import getTransitionProps from '../_util/getTransitionProps' ;
6
6
import Row from '../grid/Row' ;
7
7
import Col , { ColProps } from '../grid/Col' ;
8
- import {
8
+ import hasProp , {
9
9
initDefaultProps ,
10
10
findDOMNode ,
11
11
getComponent ,
@@ -24,6 +24,7 @@ import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
24
24
import { validateRules } from './utils/validateUtil' ;
25
25
import { getNamePath } from './utils/valueUtil' ;
26
26
import { toArray } from './utils/typeUtil' ;
27
+ import { warning } from '../vc-util/warning' ;
27
28
28
29
const iconMap = {
29
30
success : CheckCircleFilled ,
@@ -32,29 +33,35 @@ const iconMap = {
32
33
validating : LoadingOutlined ,
33
34
} ;
34
35
35
- function getPropByPath ( obj , path , strict ) {
36
+ function getPropByPath ( obj , namePathList , strict ) {
36
37
let tempObj = obj ;
37
- path = path . replace ( / \[ ( \w + ) \] / g, '.$1' ) ;
38
- path = path . replace ( / ^ \. / , '' ) ;
39
38
40
- let keyArr = path . split ( '.' ) ;
39
+ const keyArr = namePathList ;
41
40
let i = 0 ;
42
- for ( let len = keyArr . length ; i < len - 1 ; ++ i ) {
43
- if ( ! tempObj && ! strict ) break ;
44
- let key = keyArr [ i ] ;
45
- if ( key in tempObj ) {
46
- tempObj = tempObj [ key ] ;
47
- } else {
48
- if ( strict ) {
49
- throw new Error ( 'please transfer a valid prop path to form item!' ) ;
41
+ try {
42
+ for ( let len = keyArr . length ; i < len - 1 ; ++ i ) {
43
+ if ( ! tempObj && ! strict ) break ;
44
+ let key = keyArr [ i ] ;
45
+ if ( key in tempObj ) {
46
+ tempObj = tempObj [ key ] ;
47
+ } else {
48
+ if ( strict ) {
49
+ throw Error ( 'please transfer a valid name path to form item!' ) ;
50
+ }
51
+ break ;
50
52
}
51
- break ;
52
53
}
54
+ if ( strict && ! tempObj ) {
55
+ throw Error ( 'please transfer a valid name path to form item!' ) ;
56
+ }
57
+ } catch ( error ) {
58
+ console . error ( 'please transfer a valid name path to form item!' ) ;
53
59
}
60
+
54
61
return {
55
62
o : tempObj ,
56
63
k : keyArr [ i ] ,
57
- v : tempObj ? tempObj [ keyArr [ i ] ] : null ,
64
+ v : tempObj ? tempObj [ keyArr [ i ] ] : undefined ,
58
65
} ;
59
66
}
60
67
export const FormItemProps = {
@@ -69,7 +76,8 @@ export const FormItemProps = {
69
76
hasFeedback : PropTypes . bool ,
70
77
colon : PropTypes . bool ,
71
78
labelAlign : PropTypes . oneOf ( [ 'left' , 'right' ] ) ,
72
- prop : PropTypes . string ,
79
+ prop : PropTypes . oneOfType ( [ Array , String , Number ] ) ,
80
+ name : PropTypes . oneOfType ( [ Array , String , Number ] ) ,
73
81
rules : PropTypes . oneOfType ( [ Array , Object ] ) ,
74
82
autoLink : PropTypes . bool ,
75
83
required : PropTypes . bool ,
@@ -94,6 +102,7 @@ export default {
94
102
} ;
95
103
} ,
96
104
data ( ) {
105
+ warning ( hasProp ( this , 'prop' ) , `\`prop\` is deprecated. Please use \`name\` instead.` ) ;
97
106
return {
98
107
validateState : this . validateStatus ,
99
108
validateMessage : '' ,
@@ -105,21 +114,29 @@ export default {
105
114
} ,
106
115
107
116
computed : {
117
+ fieldName ( ) {
118
+ return this . name || this . prop ;
119
+ } ,
120
+ namePath ( ) {
121
+ return getNamePath ( this . fieldName ) ;
122
+ } ,
108
123
fieldId ( ) {
109
- return this . id || ( this . FormContext . name && this . prop )
110
- ? `${ this . FormContext . name } _${ this . prop } `
111
- : undefined ;
124
+ if ( this . id ) {
125
+ return this . id ;
126
+ } else if ( ! this . namePath . length ) {
127
+ return undefined ;
128
+ } else {
129
+ const formName = this . FormContext . name ;
130
+ const mergedId = this . namePath . join ( '_' ) ;
131
+ return formName ? `${ formName } _${ mergedId } ` : mergedId ;
132
+ }
112
133
} ,
113
134
fieldValue ( ) {
114
135
const model = this . FormContext . model ;
115
- if ( ! model || ! this . prop ) {
136
+ if ( ! model || ! this . fieldName ) {
116
137
return ;
117
138
}
118
- let path = this . prop ;
119
- if ( path . indexOf ( ':' ) !== - 1 ) {
120
- path = path . replace ( / : / g, '.' ) ;
121
- }
122
- return getPropByPath ( model , path , true ) . v ;
139
+ return getPropByPath ( model , this . namePath , true ) . v ;
123
140
} ,
124
141
isRequired ( ) {
125
142
let rules = this . getRules ( ) ;
@@ -145,7 +162,7 @@ export default {
145
162
provide ( 'isFormItemChildren' , true ) ;
146
163
} ,
147
164
mounted ( ) {
148
- if ( this . prop ) {
165
+ if ( this . fieldName ) {
149
166
const { addField } = this . FormContext ;
150
167
addField && addField ( this ) ;
151
168
this . initialValue = cloneDeep ( this . fieldValue ) ;
@@ -157,10 +174,10 @@ export default {
157
174
} ,
158
175
methods : {
159
176
getNamePath ( ) {
160
- const { prop } = this . $props ;
177
+ const { fieldName } = this ;
161
178
const { prefixName = [ ] } = this . FormContext ;
162
179
163
- return prop !== undefined ? [ ...prefixName , ...getNamePath ( prop ) ] : [ ] ;
180
+ return fieldName !== undefined ? [ ...prefixName , ...this . namePath ] : [ ] ;
164
181
} ,
165
182
validateRules ( options ) {
166
183
const { validateFirst = false , messageVariables } = this . $props ;
@@ -170,15 +187,17 @@ export default {
170
187
let filteredRules = this . getRules ( ) ;
171
188
if ( triggerName ) {
172
189
filteredRules = filteredRules . filter ( rule => {
173
- const { validateTrigger } = rule ;
174
- if ( ! validateTrigger ) {
190
+ const { trigger } = rule ;
191
+ if ( ! trigger ) {
175
192
return true ;
176
193
}
177
- const triggerList = toArray ( validateTrigger ) ;
194
+ const triggerList = toArray ( trigger ) ;
178
195
return triggerList . includes ( triggerName ) ;
179
196
} ) ;
180
197
}
181
-
198
+ if ( ! filteredRules . length ) {
199
+ return Promise . resolve ( ) ;
200
+ }
182
201
const promise = validateRules (
183
202
namePath ,
184
203
this . fieldValue ,
@@ -207,8 +226,8 @@ export default {
207
226
const selfRules = this . rules ;
208
227
const requiredRule =
209
228
this . required !== undefined ? { required : ! ! this . required , trigger : 'change' } : [ ] ;
210
- const prop = getPropByPath ( formRules , this . prop || '' ) ;
211
- formRules = formRules ? prop . o [ this . prop || '' ] || prop . v : [ ] ;
229
+ const prop = getPropByPath ( formRules , this . namePath ) ;
230
+ formRules = formRules ? prop . o [ prop . k ] || prop . v : [ ] ;
212
231
return [ ] . concat ( selfRules || formRules || [ ] ) . concat ( requiredRule ) ;
213
232
} ,
214
233
getFilteredRule ( trigger ) {
@@ -242,13 +261,9 @@ export default {
242
261
resetField ( ) {
243
262
this . validateState = '' ;
244
263
this . validateMessage = '' ;
245
- let model = this . FormContext . model || { } ;
246
- let value = this . fieldValue ;
247
- let path = this . prop ;
248
- if ( path . indexOf ( ':' ) !== - 1 ) {
249
- path = path . replace ( / : / , '.' ) ;
250
- }
251
- let prop = getPropByPath ( model , path , true ) ;
264
+ const model = this . FormContext . model || { } ;
265
+ const value = this . fieldValue ;
266
+ const prop = getPropByPath ( model , this . namePath , true ) ;
252
267
this . validateDisabled = true ;
253
268
if ( Array . isArray ( value ) ) {
254
269
prop . o [ prop . k ] = [ ] . concat ( this . initialValue ) ;
@@ -456,7 +471,7 @@ export default {
456
471
const { autoLink } = getOptionProps ( this ) ;
457
472
const children = getSlot ( this ) ;
458
473
let firstChildren = children [ 0 ] ;
459
- if ( this . prop && autoLink && isValidElement ( firstChildren ) ) {
474
+ if ( this . fieldName && autoLink && isValidElement ( firstChildren ) ) {
460
475
const originalEvents = getEvents ( firstChildren ) ;
461
476
const originalBlur = originalEvents . onBlur ;
462
477
const originalChange = originalEvents . onChange ;
0 commit comments