@@ -63,48 +63,39 @@ function getDefault<V extends iFormValue = iFormValue>(
63
63
}
64
64
}
65
65
66
+ function isChoiceType ( type : eFormTypeSimple | eFormTypeComplex ) : boolean {
67
+ const types : ( eFormTypeSimple | eFormTypeComplex ) [ ] = [
68
+ eFormType . CHOICE ,
69
+ eFormType . SELECT ,
70
+ eFormType . SELECT_FILTER ,
71
+ ] ;
72
+
73
+ return types . includes ( type ) ;
74
+ }
75
+
66
76
export class FormInputDefault < T extends eFormTypeSimple | eFormTypeComplex = eFormTypeSimple >
67
77
implements iFormInputDefault < T >
68
78
{
69
- // private
70
- private _options ! : iSelectOption [ ] ;
71
79
// public
72
80
public type ! : T ;
73
81
// public readonly
74
82
public readonly required ! : boolean ;
75
83
public readonly placeholder ! : string ;
76
84
public readonly icon ! : tFormIcon ;
77
85
public readonly autocomplete ! : tFormAutocomplete ;
78
- public min ! : number ;
79
- public max ! : number ;
80
86
81
87
constructor (
82
88
formInput : iFormInputDefault < T > ,
83
89
private _rerender ?: ( fi ?: Partial < iFormInputDefault < T > > ) => void
84
90
) {
85
91
this . required = formInput . required ?? false ;
86
- this . _options = formInput . options ?. map ( toOption ) ?? [ ] ;
87
- this . min = formInput . min ?? 1 ;
88
-
89
- // max cannot be lower than min or more than options if they exist
90
- const maxValue = this . options . length || formInput . max || 9e9 ;
91
-
92
- this . max = maxValue < this . min ? this . min : maxValue ;
93
92
94
93
if ( formInput . type ) this . type = formInput . type ;
95
94
if ( formInput . placeholder ) this . placeholder = formInput . placeholder ;
96
95
if ( formInput . icon ) this . icon = getIcon ( formInput . icon , formInput . type ) ;
97
96
if ( formInput . autocomplete ) this . autocomplete = formInput . autocomplete ;
98
97
}
99
98
100
- get options ( ) : iSelectOption [ ] {
101
- return this . _options ;
102
- }
103
- set options ( updatedOptions : iSelectOption [ ] | undefined ) {
104
- this . _options = updatedOptions || [ ] ;
105
- this . rerender ( ) ;
106
- }
107
-
108
99
/** Rerender component */
109
100
public rerender ( ) : void {
110
101
this . _rerender ?.( this ) ;
@@ -125,12 +116,15 @@ export class FormInput<V extends iFormValue = iFormValue>
125
116
implements iFormInput < V >
126
117
{
127
118
// private
119
+ private _options ! : iSelectOption [ ] ;
128
120
private _values ! : ( V | V [ ] ) [ ] ;
129
121
private _defaults ?: [ iFormInputDefault , iFormInputDefault , ...iFormInputDefault [ ] ] ;
130
122
// public readonly
131
123
public readonly name ! : string ;
132
124
public readonly title ! : string ;
133
125
public readonly multiple ! : boolean ;
126
+ public readonly min ! : number ;
127
+ public readonly max ! : number ;
134
128
135
129
/**
136
130
* Form input constructor
@@ -144,16 +138,51 @@ export class FormInput<V extends iFormValue = iFormValue>
144
138
) {
145
139
super ( formInput , rerender ) ;
146
140
141
+ this . _options = formInput . options ?. map ( toOption ) ?? [ ] ;
142
+ this . min = formInput . min ?? 1 ;
143
+
144
+ // max cannot be lower than min or more than options if they exist
145
+ const maxValue = this . _options . length || formInput . max || 9e9 ;
146
+
147
+ this . max = maxValue < this . min ? this . min : maxValue ;
148
+
147
149
const values = Array ( this . min ) . fill ( getDefault ( formInput . type , formInput . defaults ) ) ;
148
150
149
- this . _values = formInput . values ?? values ;
151
+ this . _values = formInput . values ?. length ? formInput . values : values ;
152
+
153
+ // autoset single value
154
+ if (
155
+ isChoiceType ( this . type ) &&
156
+ this . options . length === 1 &&
157
+ this . _values [ 0 ] !== this . options [ 0 ] . value
158
+ ) {
159
+ this . _values = [ this . options [ 0 ] . value as V ] ;
160
+ }
161
+
150
162
this . name = formInput . name ;
151
163
this . multiple = formInput . multiple ?? false ;
152
164
153
165
if ( formInput . defaults ) this . _defaults = formInput . defaults ;
154
166
if ( formInput . title ) this . title = formInput . title ;
155
167
}
156
168
169
+ get options ( ) : iSelectOption [ ] {
170
+ return this . _options ;
171
+ }
172
+ set options ( updatedOptions : iSelectOption [ ] | undefined ) {
173
+ this . _options = updatedOptions || [ ] ;
174
+
175
+ if (
176
+ isChoiceType ( this . type ) &&
177
+ this . options . length === 1 &&
178
+ this . values [ 0 ] !== this . options [ 0 ] . value
179
+ ) {
180
+ this . values = [ this . options [ 0 ] . value as V ] ;
181
+ }
182
+
183
+ this . rerender ( ) ;
184
+ }
185
+
157
186
get values ( ) : ( V | V [ ] ) [ ] {
158
187
return this . _values ;
159
188
}
@@ -257,6 +286,10 @@ export class FormInput<V extends iFormValue = iFormValue>
257
286
}
258
287
259
288
export interface iForm {
289
+ /**
290
+ * Optional form key
291
+ */
292
+ key ?: string | number ;
260
293
title ?: string ;
261
294
inputs : FormInput [ ] ;
262
295
listen ?: boolean ;
0 commit comments