@@ -49,9 +49,10 @@ type FlagTypes =
49
49
| 'badInput'
50
50
| 'valid' ;
51
51
52
+ // Acceptable as an internal interface/type, BUT if exposed externally this should be turned into a public class in a separate file.
52
53
interface Validator {
53
54
flagKey : FlagTypes ;
54
- getMessage : ( ) => String ;
55
+ getMessageMethod : ( ) => String ;
55
56
checkMethod : ( ) => boolean ;
56
57
}
57
58
@@ -212,19 +213,28 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
212
213
* );
213
214
* @method hasValue
214
215
* @param {FlagTypes } flagKey the type of validation.
215
- * @param {method } getMessage method to retrieve relevant message. Is executed every time the validator is re-executed.
216
+ * @param {method } getMessageMethod method to retrieve relevant message. Is executed every time the validator is re-executed.
216
217
* @param {method } checkMethod method to determine if this validator should invalidate this form control. Return true if this should prevent submission.
217
218
*/
218
219
protected addValidator (
219
220
flagKey : FlagTypes ,
220
221
getMessageMethod : ( ) => String ,
221
222
checkMethod : ( ) => boolean
222
- ) {
223
- this . _validators . push ( {
223
+ ) : Validator {
224
+ const obj = {
224
225
flagKey : flagKey ,
225
- getMessage : getMessageMethod ,
226
+ getMessageMethod : getMessageMethod ,
226
227
checkMethod : checkMethod ,
227
- } ) ;
228
+ } ;
229
+ this . _validators . push ( obj ) ;
230
+ return obj ;
231
+ }
232
+
233
+ protected removeValidator ( validator : Validator ) {
234
+ const index = this . _validators . indexOf ( validator ) ;
235
+ if ( index !== - 1 ) {
236
+ this . _validators . splice ( index , 1 ) ;
237
+ }
228
238
}
229
239
230
240
/**
@@ -236,26 +246,28 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
236
246
this . _formCtrlElements . push ( element ) ;
237
247
}
238
248
249
+ private _customValidityObject ?: Validator ;
250
+
239
251
/**
240
252
* @method setCustomValidity
241
253
* @description Set custom validity state, set to empty string to remove the custom message.
242
254
* @param message {string} - The message to be shown
243
255
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity|HTMLObjectElement:setCustomValidity }
244
256
*/
245
- protected setCustomValidity ( message : string ) {
246
- if ( message === '' ) {
247
- this . _internals . setValidity ( { } ) ;
248
- this . dispatchEvent ( new UUIFormControlEvent ( UUIFormControlEvent . VALID ) ) ;
249
- return ;
257
+ protected setCustomValidity ( message : string | null ) {
258
+ if ( this . _customValidityObject ) {
259
+ this . removeValidator ( this . _customValidityObject ) ;
250
260
}
251
261
252
- this . _internals . setValidity (
253
- { customError : true } ,
254
- message ,
255
- this . getFormElement ( )
256
- ) ;
262
+ if ( message != null && message !== '' ) {
263
+ this . _customValidityObject = this . addValidator (
264
+ 'customError' ,
265
+ ( ) : string => message ,
266
+ ( ) => true
267
+ ) ;
268
+ }
257
269
258
- this . dispatchEvent ( new UUIFormControlEvent ( UUIFormControlEvent . INVALID ) ) ;
270
+ this . _runValidators ( ) ;
259
271
}
260
272
261
273
private _runValidators ( ) {
@@ -281,7 +293,7 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
281
293
this . _validityState [ validator . flagKey ] = true ;
282
294
this . _internals . setValidity (
283
295
this . _validityState ,
284
- validator . getMessage ( ) ,
296
+ validator . getMessageMethod ( ) ,
285
297
this . getFormElement ( )
286
298
) ;
287
299
}
0 commit comments