@@ -11,37 +11,35 @@ import {
11
11
} from '@angular/core' ;
12
12
import { CoreService } from 'wacom' ;
13
13
14
+ export type Value = string | number | boolean | string [ ] | number [ ] | boolean [ ] ;
15
+
14
16
/**
15
17
* InputComponent is a customizable input component that supports various types of inputs,
16
18
* including text, radio buttons, checkboxes, and textareas. It also provides validation,
17
19
* custom value replacement, and event handling for changes, submissions, and blur events.
18
20
*/
19
21
@Component ( {
20
- selector : 'winput' ,
21
- templateUrl : './input.component.html' ,
22
- styleUrls : [ './input.component.scss' ] ,
23
- standalone : false
22
+ selector : 'winput' ,
23
+ templateUrl : './input.component.html' ,
24
+ styleUrls : [ './input.component.scss' ] ,
25
+ standalone : false
24
26
} )
25
27
export class InputComponent implements OnInit , OnChanges {
26
28
/**
27
29
* The value of the input field.
28
30
*/
29
- @Input ( ) value : string | number | boolean = '' ;
31
+ @Input ( ) value : Value = '' ;
30
32
31
33
/**
32
34
* A function to replace the input value before emitting changes.
33
35
* This allows custom transformations of the input value.
34
36
*/
35
- @Input ( ) replace : (
36
- value : string | number | boolean
37
- ) => string | number | boolean ;
37
+ @Input ( ) replace : ( value : Value ) => Value ;
38
38
39
39
/**
40
40
* A function to validate the input value. The default implementation checks for a truthy value.
41
41
*/
42
- @Input ( ) valid : ( value : string | number | boolean ) => boolean = (
43
- value : string | number | boolean
44
- ) => ! ! value ;
42
+ @Input ( ) valid : ( value : Value ) => boolean = ( value : Value ) => ! ! value ;
45
43
46
44
/**
47
45
* A list of items used for radio buttons or other list-based inputs.
@@ -111,12 +109,12 @@ export class InputComponent implements OnInit, OnChanges {
111
109
/**
112
110
* Event emitted when the input value changes.
113
111
*/
114
- @Output ( ) wChange = new EventEmitter < string | number | boolean > ( ) ;
112
+ @Output ( ) wChange = new EventEmitter < Value > ( ) ;
115
113
116
114
/**
117
115
* Event emitted when the form is submitted.
118
116
*/
119
- @Output ( ) wSubmit = new EventEmitter < string | number | boolean > ( ) ;
117
+ @Output ( ) wSubmit = new EventEmitter < Value > ( ) ;
120
118
121
119
/**
122
120
* Event emitted when the input field loses focus.
@@ -203,4 +201,22 @@ export class InputComponent implements OnInit, OnChanges {
203
201
setDisabled ( disabled : boolean ) : void {
204
202
this . disabled = disabled ;
205
203
}
204
+
205
+ setCheckboxValue ( add : boolean , i : number ) : void {
206
+ this . value = Array . isArray ( this . value ) ? this . value : [ ] ;
207
+
208
+ const index = (
209
+ this . value as Array < string | number | boolean >
210
+ ) . findIndex ( ( item ) => item === this . items [ i ] ) ;
211
+
212
+ if ( index !== - 1 ) {
213
+ ( this . value as Array < string | number | boolean > ) . splice ( index , 1 ) ;
214
+ }
215
+
216
+ if ( add ) {
217
+ ( this . value as Array < string | number | boolean > ) . push (
218
+ this . items [ i ]
219
+ ) ;
220
+ }
221
+ }
206
222
}
0 commit comments