1
1
import { IElementInternals } from 'element-internals-polyfill' ;
2
2
import { Constructor , FormControlInterface , FormValue , IControlHost , Validator } from './types' ;
3
3
4
- export function FormControlMixin < T extends Constructor < HTMLElement & IControlHost > > ( SuperClass : T ) {
4
+ export function FormControlMixin <
5
+ TBase extends Constructor < HTMLElement & IControlHost > & { observedAttributes : string [ ] }
6
+ > ( SuperClass : TBase ) {
5
7
class FormControl extends SuperClass {
6
8
/** Wires up control instances to be form associated */
7
9
static get formAssociated ( ) {
@@ -30,12 +32,11 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
30
32
static get observedAttributes ( ) : string [ ] {
31
33
const validatorAttributes = this . validators . map ( ( validator ) => validator . attribute ) ;
32
34
33
- /** @ts -ignore This exits */
34
35
const observedAttributes = super . observedAttributes || [ ] ;
35
36
36
37
/** Make sure there are no duplicates inside the attributes list */
37
38
const attributeSet = new Set ( [ ...observedAttributes , ...validatorAttributes ] ) ;
38
- return [ ...attributeSet ] ;
39
+ return [ ...attributeSet ] as string [ ] ;
39
40
}
40
41
41
42
/**
@@ -130,6 +131,7 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
130
131
return this . internals . validity ;
131
132
}
132
133
134
+ /* eslint-disable @typescript-eslint/no-explicit-any */
133
135
constructor ( ...args : any [ ] ) {
134
136
super ( ...args ) ;
135
137
this . addEventListener ( 'focus' , this . #onFocus) ;
@@ -159,7 +161,9 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
159
161
/** Initialize the form control and perform initial validation */
160
162
this . #initFormControl( ) ;
161
163
this . #validate( this . value ) ;
162
- this . validationMessageCallback ( '' ) ;
164
+ if ( this . validationMessageCallback ) {
165
+ this . validationMessageCallback ( '' ) ;
166
+ }
163
167
}
164
168
165
169
disconnectedCallback ( ) {
@@ -181,12 +185,6 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
181
185
/** Closed over variable to track value changes */
182
186
let value : FormValue = this . value || '' ;
183
187
184
- /** Value getter reference within the closure */
185
- let set : ( ( v : FormValue ) => void ) | undefined ;
186
-
187
- /** Value setter reference within the closure */
188
- let get : ( ( v : FormValue ) => void ) | undefined ;
189
-
190
188
/** Look to see if '`checked'` is on the control's prototype */
191
189
const hasChecked = this . #isCheckedElement;
192
190
@@ -210,15 +208,15 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
210
208
}
211
209
212
210
/** Make sure to defer to the parent */
213
- set = descriptor && descriptor . set ;
214
- get = descriptor && descriptor . get ;
211
+ const set = descriptor && descriptor . set ;
212
+ const get = descriptor && descriptor . get ;
215
213
216
214
/** Define the FormControl's value property */
217
215
Object . defineProperty ( this , 'value' , {
218
216
get ( ) {
219
217
/** If a getter already exists, make sure to call it */
220
218
if ( get ) {
221
- return get . call ( this , null ) ;
219
+ return get . call ( this ) ;
222
220
}
223
221
return value ;
224
222
} ,
@@ -312,7 +310,7 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
312
310
* in cases where `checked` is present upon initialization, this will be
313
311
* effectively `this.checked && this.value`.
314
312
*/
315
- valueChangedCallback ( value : FormValue ) : void { }
313
+ declare valueChangedCallback : ( value : FormValue ) => void ;
316
314
317
315
/**
318
316
* Resets a form control to its initial state
@@ -375,7 +373,9 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
375
373
this . #forceError = true ;
376
374
}
377
375
const showError = this . #shouldShowError( ) ;
378
- this . validationMessageCallback ( showError ? this . validationMessage : '' ) ;
376
+ if ( this . validationMessageCallback ) {
377
+ this . validationMessageCallback ( showError ? this . validationMessage : '' ) ;
378
+ }
379
379
} ;
380
380
381
381
/**
@@ -400,7 +400,9 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
400
400
}
401
401
this . #validate( value ) ;
402
402
const showError = this . #shouldShowError( ) ;
403
- this . validationMessageCallback ( showError ? this . validationMessage : '' ) ;
403
+ if ( this . validationMessageCallback ) {
404
+ this . validationMessageCallback ( showError ? this . validationMessage : '' ) ;
405
+ }
404
406
}
405
407
406
408
/**
@@ -443,7 +445,7 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
443
445
* if a control has a ValidityCallback, it can override the error
444
446
* message for a given validity key.
445
447
*/
446
- if ( this . validityCallback ( key ) ) {
448
+ if ( this . validityCallback && this . validityCallback ( key ) ) {
447
449
messageResult = this . validityCallback ( key ) as string ;
448
450
} else if ( message instanceof Function ) {
449
451
messageResult = message ( this , value ) ;
@@ -508,14 +510,14 @@ export function FormControlMixin<T extends Constructor<HTMLElement & IControlHos
508
510
* The returned value will be used as the validationMessage for the given key.
509
511
* @param validationKey {string} - The key that has returned invalid
510
512
*/
511
- validityCallback ( validationKey : string ) : string | void { }
513
+ declare validityCallback : ( validationKey : string ) => string | void ;
512
514
513
515
/**
514
516
* Called when the control's validationMessage should be changed
515
517
* @param message { string } - The new validation message
516
518
*/
517
- validationMessageCallback ( message : string ) : void { }
519
+ declare validationMessageCallback : ( message : string ) => void ;
518
520
}
519
521
520
- return FormControl as Constructor < FormControlInterface > & T ;
522
+ return FormControl as Constructor < FormControlInterface > & TBase ;
521
523
}
0 commit comments