@@ -8,6 +8,7 @@ import type {
8
8
iSelectOption ,
9
9
tFormAutocomplete ,
10
10
tFormIcon ,
11
+ tFormInputDefault ,
11
12
} from "@open-xamu-co/ui-common-types" ;
12
13
import {
13
14
eFormType ,
@@ -78,38 +79,36 @@ function isChoiceType(type: eFormTypeBase | eFormTypeSimple | eFormTypeComplex):
78
79
return types . includes ( type ) ;
79
80
}
80
81
81
- export class FormInputDefault <
82
- T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple ,
83
- > implements iFormInputDefault < T >
82
+ export abstract class FormInputDefault <
83
+ T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple . TEXT ,
84
+ > implements tFormInputDefault < T >
84
85
{
85
86
// public
86
- public type ! : T ;
87
+ public type : T ;
87
88
// public readonly
88
- public readonly required ! : boolean ;
89
- public readonly placeholder ! : string ;
90
- public readonly icon ! : tFormIcon ;
91
- public readonly autocomplete ! : tFormAutocomplete ;
89
+ public readonly required : boolean ;
90
+ public readonly placeholder : string ;
91
+ public readonly icon ? : tFormIcon ;
92
+ public readonly autocomplete ? : tFormAutocomplete ;
92
93
93
94
constructor (
94
95
formInput : iFormInputDefault < T > ,
95
96
private _rerender ?: ( fi ?: Partial < iFormInputDefault < T > > ) => void
96
97
) {
98
+ this . type = formInput . type || ( eFormTypeSimple . TEXT as T ) ;
97
99
this . required = formInput . required ?? false ;
100
+ this . placeholder = formInput . placeholder || "" ;
101
+ this . autocomplete = formInput . autocomplete ;
98
102
99
- if ( formInput . type ) this . type = formInput . type ;
100
- if ( formInput . placeholder ) this . placeholder = formInput . placeholder ;
101
103
if ( formInput . icon ) this . icon = getIcon ( formInput . icon , formInput . type ) ;
102
- if ( formInput . autocomplete ) this . autocomplete = formInput . autocomplete ;
103
104
}
104
105
105
106
/** Rerender component */
106
107
public rerender ( ) : void {
107
108
this . _rerender ?.( this ) ;
108
109
}
109
110
110
- /**
111
- * set rerender function
112
- */
111
+ /** set rerender function */
113
112
public setRerender ( rerender : ( fi ?: Partial < iFormInputDefault < T > > ) => void ) {
114
113
this . _rerender = rerender ;
115
114
@@ -122,15 +121,15 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
122
121
implements iFormInput < V , Vk >
123
122
{
124
123
// private
125
- private _options ! : iSelectOption [ ] ;
126
- private _values ! : Vk [ ] ;
124
+ private _options : iSelectOption [ ] ;
125
+ private _values : Vk [ ] ;
127
126
private _defaults ?: [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ] ;
128
127
// public readonly
129
- public readonly name ! : string ;
130
- public readonly title ! : string ;
131
- public readonly multiple ! : boolean ;
132
- public readonly min ! : number ;
133
- public readonly max ! : number ;
128
+ public readonly name : string ;
129
+ public readonly title ? : string ;
130
+ public readonly multiple : boolean ;
131
+ public readonly min : number ;
132
+ public readonly max : number ;
134
133
135
134
/**
136
135
* Form input constructor
@@ -144,7 +143,11 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
144
143
) {
145
144
super ( formInput , rerender ) ;
146
145
146
+ this . name = formInput . name ;
147
+ this . multiple = formInput . multiple ?? false ;
148
+ this . title = formInput . title ;
147
149
this . _options = formInput . options ?. map ( toOption ) ?? [ ] ;
150
+ this . _defaults = formInput . defaults ;
148
151
this . min = formInput . min ?? 1 ;
149
152
150
153
// max cannot be lower than min or more than options if they exist
@@ -167,12 +170,6 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
167
170
// use defaults
168
171
if ( this . _values . length < length ) this . _values = values ;
169
172
}
170
-
171
- this . name = formInput . name ;
172
- this . multiple = formInput . multiple ?? false ;
173
-
174
- if ( formInput . defaults ) this . _defaults = formInput . defaults ;
175
- if ( formInput . title ) this . title = formInput . title ;
176
173
}
177
174
178
175
get options ( ) : iSelectOption [ ] {
@@ -288,7 +285,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
288
285
* Get simple object
289
286
*/
290
287
public getObject < Vi extends iFormValue = iFormValue , Vik extends Vi | Vi [ ] = Vi > (
291
- input : FormInput < Vi , Vik >
288
+ input : iFormInput < Vi , Vik >
292
289
) : iFormInput < Vi , Vik > {
293
290
return {
294
291
required : input . required ,
@@ -311,17 +308,3 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
311
308
return isEqual ( this . getObject ( this ) , this . getObject ( other ) ) ;
312
309
}
313
310
}
314
-
315
- export interface iForm {
316
- /**
317
- * Optional form key
318
- */
319
- key ?: string ;
320
- title ?: string ;
321
- inputs : FormInput [ ] ;
322
- listen ?: boolean ;
323
- /** Make all inputs read only by disabling them */
324
- readonly ?: boolean ;
325
- /** Message when no valid inputs are rendered */
326
- emptyMessage ?: string ;
327
- }
0 commit comments