@@ -14,6 +14,7 @@ interface ValidateFnParam {
14
14
messageShowType : MessageShowType
15
15
dfcUID : string
16
16
popPosition : PopPosition | Array < BasePopPosition >
17
+ updateOn ?: UpdateOn
17
18
}
18
19
19
20
interface CustomValidatorRuleObject {
@@ -46,6 +47,7 @@ export interface ShowPopoverErrorMessageEventData {
46
47
message ?: string
47
48
uid ?: string ,
48
49
popPosition ?: PopPosition
50
+ [ prop : string ] : any
49
51
}
50
52
51
53
type MessageShowType = 'popover' | 'text' | 'none' | 'toast' ;
@@ -210,17 +212,31 @@ function handleErrorStrategyPass(el: HTMLElement): void {
210
212
el . setAttribute ( 'class' , classList . join ( ' ' ) ) ;
211
213
}
212
214
213
- function handleValidateError ( { el, tipEl, message, isFormTag, messageShowType, dfcUID, popPosition = 'right-bottom' } : Partial < ValidateFnParam > ) : void {
215
+ function getFormControlUID ( el : HTMLElement ) : string {
216
+ if ( el . tagName . toLocaleLowerCase ( ) === "body" ) return "" ;
217
+ let uid = ''
218
+ if ( el . parentElement . id . startsWith ( 'dfc-' ) ) {
219
+ return el . parentElement . id ;
220
+ } else {
221
+ uid = getFormControlUID ( el . parentElement ) ;
222
+ }
223
+ }
224
+
225
+ function handleValidateError ( { el, tipEl, message = "" , isFormTag, messageShowType, dfcUID, popPosition = 'right-bottom' , updateOn} : Partial < ValidateFnParam > ) : void {
214
226
// 如果该指令用在form标签上,这里做特殊处理
215
227
if ( isFormTag && messageShowType === MessageShowTypeEnum . toast ) {
216
228
// todo:待替换为toast
217
229
alert ( message ) ;
218
230
return ;
219
231
}
220
232
233
+ if ( ! dfcUID ) {
234
+ dfcUID = getFormControlUID ( el ) ;
235
+ }
236
+
221
237
// messageShowType为popover时,设置popover
222
238
if ( MessageShowTypeEnum . popover === messageShowType ) {
223
- EventBus . emit ( "showPopoverErrorMessage" , { showPopover : true , message, uid : dfcUID , popPosition} as ShowPopoverErrorMessageEventData ) ;
239
+ EventBus . emit ( "showPopoverErrorMessage" , { showPopover : true , message, uid : dfcUID , popPosition, updateOn } as ShowPopoverErrorMessageEventData ) ;
224
240
return ;
225
241
}
226
242
@@ -250,7 +266,7 @@ function getFormName(binding: DirectiveBinding): string {
250
266
}
251
267
252
268
// 校验处理函数
253
- function validateFn ( { validator, modelValue, el, tipEl, isFormTag, messageShowType, dfcUID, popPosition} : Partial < ValidateFnParam > ) {
269
+ function validateFn ( { validator, modelValue, el, tipEl, isFormTag, messageShowType, dfcUID, popPosition, updateOn } : Partial < ValidateFnParam > ) {
254
270
validator . validate ( { modelName : modelValue } ) . then ( ( ) => {
255
271
handleValidatePass ( el , tipEl ) ;
256
272
} ) . catch ( ( err ) => {
@@ -265,7 +281,7 @@ function validateFn({validator, modelValue, el, tipEl, isFormTag, messageShowTyp
265
281
msg = errors [ 0 ] . message ;
266
282
}
267
283
268
- handleValidateError ( { el, tipEl, message : msg , isFormTag, messageShowType, dfcUID, popPosition} ) ;
284
+ handleValidateError ( { el, tipEl, message : msg , isFormTag, messageShowType, dfcUID, popPosition, updateOn } ) ;
269
285
} )
270
286
}
271
287
@@ -394,14 +410,21 @@ export default {
394
410
const htmlEventValidateHandler = ( e ) => {
395
411
const modelValue = e . target . value ;
396
412
if ( messageShowType === MessageShowTypeEnum . popover ) {
397
- EventBus . emit ( "showPopoverErrorMessage" , { showPopover : false , message : "" , uid : dfcUID , popPosition} as ShowPopoverErrorMessageEventData ) ;
413
+ EventBus . emit ( "showPopoverErrorMessage" , { showPopover : false , message : "" , uid : dfcUID , popPosition, updateOn } as ShowPopoverErrorMessageEventData ) ;
398
414
}
399
- validateFn ( { validator, modelValue, el, tipEl, isFormTag : false , messageShowType, dfcUID, popPosition} ) ;
415
+ validateFn ( { validator, modelValue, el, tipEl, isFormTag : false , messageShowType, dfcUID, popPosition, updateOn } ) ;
400
416
}
401
417
402
418
// 监听事件验证
403
419
vnode . children [ 0 ] . el . addEventListener ( updateOn , htmlEventValidateHandler ) ;
404
420
421
+ // 如果校验时机为change,则在focus时关闭popover
422
+ if ( messageShowType === MessageShowTypeEnum . popover && updateOn === UpdateOnEnum . change ) {
423
+ vnode . children [ 0 ] . el . addEventListener ( 'focus' , ( ) => {
424
+ EventBus . emit ( "showPopoverErrorMessage" , { showPopover : false , uid : dfcUID , updateOn} as ShowPopoverErrorMessageEventData ) ;
425
+ } ) ;
426
+ }
427
+
405
428
// 设置errorStrategy
406
429
if ( errorStrategy === ErrorStrategyEnum . pristine ) {
407
430
handleErrorStrategy ( el ) ;
@@ -415,7 +438,7 @@ export default {
415
438
const modelValue = isFormTag ? '' : vnode . children [ 0 ] . el . value ;
416
439
417
440
// 进行提交验证
418
- validateFn ( { validator, modelValue, el, tipEl, isFormTag, messageShowType} ) ;
441
+ validateFn ( { validator, modelValue, el, tipEl, isFormTag, messageShowType, updateOn : 'submit' } ) ;
419
442
} ) ;
420
443
421
444
}
0 commit comments