@@ -47,25 +47,29 @@ function getIcon(
4747/**
4848 * get form input default
4949 */
50- function getDefault < V extends iFormValue = iFormValue , Vk extends V | V [ ] = V > (
50+ function getDefault < V extends iFormValue | iFormValue [ ] > (
5151 type ?: eFormTypeBase | eFormTypeSimple | eFormTypeComplex ,
52- defaults ?: [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ]
53- ) : Vk {
52+ defaults ?: [
53+ iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > ,
54+ iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > ,
55+ ...iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > [ ] ,
56+ ]
57+ ) : V {
5458 switch ( type ) {
5559 case eFormType . LOCATION :
5660 // 3 values
57- return Array ( 3 ) . fill ( "" ) as Vk ;
61+ return Array ( 3 ) . fill ( "" ) as V ;
5862 case eFormType . ID :
5963 case eFormType . PHONE :
6064 case eFormType . CELLPHONE :
6165 case eFormType . NEW_PASSWORD :
6266 // 2 values
63- return Array ( 2 ) . fill ( "" ) as Vk ;
67+ return Array ( 2 ) . fill ( "" ) as V ;
6468 default :
6569 // 1 value
6670 if ( ! defaults ) return Array ( 1 ) . fill ( "" ) [ 0 ] ;
6771
68- return Array ( defaults . length ) . fill ( "" ) as Vk ;
72+ return Array ( defaults . length ) . fill ( "" ) as V ;
6973 }
7074}
7175
@@ -116,14 +120,24 @@ export abstract class FormInputDefault<
116120 }
117121}
118122
119- export class FormInput < V extends iFormValue = iFormValue , Vk extends V | V [ ] = V | V [ ] >
120- extends FormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex >
121- implements iFormInput < V , Vk >
123+ export class FormInput <
124+ V extends iFormValue | iFormValue [ ] = iFormValue | iFormValue [ ] ,
125+ T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex =
126+ | eFormTypeBase
127+ | eFormTypeSimple
128+ | eFormTypeComplex ,
129+ >
130+ extends FormInputDefault < T >
131+ implements iFormInput < V , T >
122132{
123133 // private
124134 private _options : iSelectOption [ ] ;
125- private _values : Vk [ ] ;
126- private _defaults ?: [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ] ;
135+ private _values : V [ ] ;
136+ private _defaults ?: [
137+ iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > ,
138+ iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > ,
139+ ...iFormInputDefault < eFormTypeBase | eFormTypeSimple | eFormTypeComplex > [ ] ,
140+ ] ;
127141 // public readonly
128142 public readonly name : string ;
129143 public readonly title ?: string ;
@@ -137,9 +151,9 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
137151 * @param _onUpdatedValues hook that is called when the values are updated
138152 */
139153 constructor (
140- formInput : iFormInput < V , Vk > ,
141- private _onUpdatedValues ?: ( updatedValues : Vk [ ] ) => Vk [ ] | undefined | void ,
142- rerender ?: ( fi ?: Partial < iFormInput < V , Vk > > ) => void
154+ formInput : iFormInput < V , T > ,
155+ private _onUpdatedValues ?: ( updatedValues : V [ ] ) => V [ ] | undefined | void ,
156+ rerender ?: ( fi ?: Partial < iFormInput < V , T > > ) => void
143157 ) {
144158 super ( formInput , rerender ) ;
145159
@@ -161,7 +175,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
161175 if ( this . required && ! this . _values . length ) {
162176 const values = this . options . map ( ( { value } ) => value ) ;
163177
164- this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) as Vk [ ] ;
178+ this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) as V [ ] ;
165179 }
166180 } else if ( this . type !== eFormType . FILE ) {
167181 const length = Math . max ( 1 , this . min ) ; // negative values fallback
@@ -181,7 +195,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
181195 if ( isChoiceType ( this . type ) ) {
182196 // autoset single value if required
183197 if ( this . required && ! this . _values . length ) {
184- const values = < Vk [ ] > this . options . map ( ( { value } ) => value ) ;
198+ const values = < V [ ] > this . options . map ( ( { value } ) => value ) ;
185199
186200 this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) ;
187201 }
@@ -190,18 +204,18 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
190204 this . rerender ( ) ;
191205 }
192206
193- get values ( ) : Vk [ ] {
207+ get values ( ) : V [ ] {
194208 return this . _values ;
195209 }
196- set values ( updatedValues : Vk [ ] | undefined ) {
210+ set values ( updatedValues : V [ ] | undefined ) {
197211 if ( updatedValues === undefined ) {
198212 // set defaults
199213 this . _values = [ ] ;
200214
201215 if ( isChoiceType ( this . type ) ) {
202216 // autoset single value if required
203217 if ( this . required && ! this . _values . length ) {
204- const values = < Vk [ ] > this . options . map ( ( { value } ) => value ) ;
218+ const values = < V [ ] > this . options . map ( ( { value } ) => value ) ;
205219
206220 this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) ;
207221 }
@@ -218,12 +232,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
218232 }
219233 }
220234
221- get defaults ( ) : [ iFormInputDefault , iFormInputDefault , ... iFormInputDefault [ ] ] | undefined {
235+ get defaults ( ) {
222236 return this . _defaults ;
223237 }
224- set defaults (
225- updatedDefaults : [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ] | undefined
226- ) {
238+ set defaults ( updatedDefaults ) {
227239 this . _defaults = updatedDefaults ;
228240 this . rerender ( ) ;
229241 }
@@ -233,7 +245,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
233245 *
234246 * @override
235247 */
236- public setRerender ( rerender : ( fi ?: Partial < iFormInput < V > > ) => void ) {
248+ public setRerender ( rerender : ( fi ?: Partial < iFormInput < V , T > > ) => void ) {
237249 super . setRerender ( rerender ) ;
238250
239251 return this ;
@@ -242,7 +254,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
242254 /**
243255 * add new model to the models
244256 */
245- public addValue ( newValue : Vk = getDefault ( this . type , this . defaults ) ) {
257+ public addValue ( newValue : V = getDefault ( this . type , this . defaults ) ) {
246258 if ( this . values . length < this . max ) {
247259 this . values = [ ...this . values , newValue ] ;
248260 }
@@ -264,10 +276,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
264276 * Clone this object
265277 */
266278 public clone (
267- overrides ?: Omit < iFormInput < V , Vk > , "name" > & { name ?: string } ,
268- onUpdatedValues ?: ( updatedValues : Vk [ ] ) => Vk [ ] | undefined | void
279+ overrides ?: Omit < iFormInput < V , T > , "name" > & { name ?: string } ,
280+ onUpdatedValues ?: ( updatedValues : V [ ] ) => V [ ] | undefined | void
269281 ) {
270- const oldFormInput : iFormInput < V , Vk > = {
282+ const oldFormInput : iFormInput < V , T > = {
271283 ...this ,
272284 options : this . options ,
273285 values : this . values ,
@@ -281,12 +293,17 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
281293 ) ;
282294 }
283295
296+ public isEqual ( other : FormInput ) : boolean {
297+ return isEqual ( FormInput . getObject ( this ) , FormInput . getObject ( other ) ) ;
298+ }
299+
284300 /**
285301 * Get simple object
286302 */
287- public getObject < Vi extends iFormValue = iFormValue , Vik extends Vi | Vi [ ] = Vi > (
288- input : iFormInput < Vi , Vik >
289- ) : iFormInput < Vi , Vik > {
303+ static getObject <
304+ Vi extends iFormValue | iFormValue [ ] = iFormValue | iFormValue [ ] ,
305+ Ti extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple ,
306+ > ( input : iFormInput < Vi , Ti > ) : iFormInput < Vi , Ti > {
290307 return {
291308 required : input . required ,
292309 type : input . type ,
@@ -303,8 +320,4 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
303320 multiple : input . multiple ,
304321 } ;
305322 }
306-
307- public isEqual ( other : FormInput ) : boolean {
308- return isEqual ( this . getObject ( this ) , this . getObject ( other ) ) ;
309- }
310323}
0 commit comments