@@ -47,25 +47,29 @@ function getIcon(
47
47
/**
48
48
* get form input default
49
49
*/
50
- function getDefault < V extends iFormValue = iFormValue , Vk extends V | V [ ] = V > (
50
+ function getDefault < V extends iFormValue | iFormValue [ ] > (
51
51
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 {
54
58
switch ( type ) {
55
59
case eFormType . LOCATION :
56
60
// 3 values
57
- return Array ( 3 ) . fill ( "" ) as Vk ;
61
+ return Array ( 3 ) . fill ( "" ) as V ;
58
62
case eFormType . ID :
59
63
case eFormType . PHONE :
60
64
case eFormType . CELLPHONE :
61
65
case eFormType . NEW_PASSWORD :
62
66
// 2 values
63
- return Array ( 2 ) . fill ( "" ) as Vk ;
67
+ return Array ( 2 ) . fill ( "" ) as V ;
64
68
default :
65
69
// 1 value
66
70
if ( ! defaults ) return Array ( 1 ) . fill ( "" ) [ 0 ] ;
67
71
68
- return Array ( defaults . length ) . fill ( "" ) as Vk ;
72
+ return Array ( defaults . length ) . fill ( "" ) as V ;
69
73
}
70
74
}
71
75
@@ -116,14 +120,24 @@ export abstract class FormInputDefault<
116
120
}
117
121
}
118
122
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 >
122
132
{
123
133
// private
124
134
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
+ ] ;
127
141
// public readonly
128
142
public readonly name : string ;
129
143
public readonly title ?: string ;
@@ -137,9 +151,9 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
137
151
* @param _onUpdatedValues hook that is called when the values are updated
138
152
*/
139
153
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
143
157
) {
144
158
super ( formInput , rerender ) ;
145
159
@@ -161,7 +175,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
161
175
if ( this . required && ! this . _values . length ) {
162
176
const values = this . options . map ( ( { value } ) => value ) ;
163
177
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 [ ] ;
165
179
}
166
180
} else if ( this . type !== eFormType . FILE ) {
167
181
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
181
195
if ( isChoiceType ( this . type ) ) {
182
196
// autoset single value if required
183
197
if ( this . required && ! this . _values . length ) {
184
- const values = < Vk [ ] > this . options . map ( ( { value } ) => value ) ;
198
+ const values = < V [ ] > this . options . map ( ( { value } ) => value ) ;
185
199
186
200
this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) ;
187
201
}
@@ -190,18 +204,18 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
190
204
this . rerender ( ) ;
191
205
}
192
206
193
- get values ( ) : Vk [ ] {
207
+ get values ( ) : V [ ] {
194
208
return this . _values ;
195
209
}
196
- set values ( updatedValues : Vk [ ] | undefined ) {
210
+ set values ( updatedValues : V [ ] | undefined ) {
197
211
if ( updatedValues === undefined ) {
198
212
// set defaults
199
213
this . _values = [ ] ;
200
214
201
215
if ( isChoiceType ( this . type ) ) {
202
216
// autoset single value if required
203
217
if ( this . required && ! this . _values . length ) {
204
- const values = < Vk [ ] > this . options . map ( ( { value } ) => value ) ;
218
+ const values = < V [ ] > this . options . map ( ( { value } ) => value ) ;
205
219
206
220
this . _values = values . slice ( 0 , Math . max ( 1 , this . min ) ) ;
207
221
}
@@ -218,12 +232,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
218
232
}
219
233
}
220
234
221
- get defaults ( ) : [ iFormInputDefault , iFormInputDefault , ... iFormInputDefault [ ] ] | undefined {
235
+ get defaults ( ) {
222
236
return this . _defaults ;
223
237
}
224
- set defaults (
225
- updatedDefaults : [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ] | undefined
226
- ) {
238
+ set defaults ( updatedDefaults ) {
227
239
this . _defaults = updatedDefaults ;
228
240
this . rerender ( ) ;
229
241
}
@@ -233,7 +245,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
233
245
*
234
246
* @override
235
247
*/
236
- public setRerender ( rerender : ( fi ?: Partial < iFormInput < V > > ) => void ) {
248
+ public setRerender ( rerender : ( fi ?: Partial < iFormInput < V , T > > ) => void ) {
237
249
super . setRerender ( rerender ) ;
238
250
239
251
return this ;
@@ -242,7 +254,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
242
254
/**
243
255
* add new model to the models
244
256
*/
245
- public addValue ( newValue : Vk = getDefault ( this . type , this . defaults ) ) {
257
+ public addValue ( newValue : V = getDefault ( this . type , this . defaults ) ) {
246
258
if ( this . values . length < this . max ) {
247
259
this . values = [ ...this . values , newValue ] ;
248
260
}
@@ -264,10 +276,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
264
276
* Clone this object
265
277
*/
266
278
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
269
281
) {
270
- const oldFormInput : iFormInput < V , Vk > = {
282
+ const oldFormInput : iFormInput < V , T > = {
271
283
...this ,
272
284
options : this . options ,
273
285
values : this . values ,
@@ -281,12 +293,17 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
281
293
) ;
282
294
}
283
295
296
+ public isEqual ( other : FormInput ) : boolean {
297
+ return isEqual ( FormInput . getObject ( this ) , FormInput . getObject ( other ) ) ;
298
+ }
299
+
284
300
/**
285
301
* Get simple object
286
302
*/
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 > {
290
307
return {
291
308
required : input . required ,
292
309
type : input . type ,
@@ -303,8 +320,4 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
303
320
multiple : input . multiple ,
304
321
} ;
305
322
}
306
-
307
- public isEqual ( other : FormInput ) : boolean {
308
- return isEqual ( this . getObject ( this ) , this . getObject ( other ) ) ;
309
- }
310
323
}
0 commit comments