Skip to content

Commit db91578

Browse files
committed
added checkbox based on items on input
1 parent cf7dc42 commit db91578

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

client/src/app/core/modules/input/input.component.html

+34-15
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,38 @@
7272

7373
<!-- Checkboxes -->
7474
<ng-container *ngIf="type === 'checkbox'">
75-
<label class="container-box">
76-
<input
77-
(ngModelChange)="value = $event; onChange()"
78-
class="w-input__checkbox"
79-
[disabled]="disabled"
80-
[class.error]="error"
81-
[ngModel]="value"
82-
[ngClass]="wClass"
83-
type="checkbox"
84-
[name]="name"
85-
#inputEl
86-
/>
87-
<span class="checkmark"></span>
88-
<span class="check-text" *ngIf="label">{{ label }}</span>
89-
</label>
75+
<ng-container *ngIf="items?.length">
76+
<label class="check-label" *ngFor="let item of items; index as i">
77+
<span class="form-label">{{ item }}</span>
78+
<input
79+
(ngModelChange)="setCheckboxValue($event, i); onChange()"
80+
class="w-input__checkbox"
81+
[disabled]="disabled"
82+
[class.error]="error"
83+
ngModel
84+
[ngClass]="wClass"
85+
type="checkbox"
86+
[name]="name+i"
87+
#inputEl
88+
/>
89+
<span class="checkbox _radio"></span>
90+
</label>
91+
</ng-container>
92+
<ng-container *ngIf="!items?.length">
93+
<label class="container-box">
94+
<input
95+
(ngModelChange)="value = $event; onChange()"
96+
class="w-input__checkbox"
97+
[disabled]="disabled"
98+
[class.error]="error"
99+
[ngModel]="value"
100+
[ngClass]="wClass"
101+
type="checkbox"
102+
[name]="name"
103+
#inputEl
104+
/>
105+
<span class="checkmark"></span>
106+
<span class="check-text" *ngIf="label">{{ label }}</span>
107+
</label>
108+
</ng-container>
90109
</ng-container>

client/src/app/core/modules/input/input.component.ts

+29-13
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,35 @@ import {
1111
} from '@angular/core';
1212
import { CoreService } from 'wacom';
1313

14+
export type Value = string | number | boolean | string[] | number[] | boolean[];
15+
1416
/**
1517
* InputComponent is a customizable input component that supports various types of inputs,
1618
* including text, radio buttons, checkboxes, and textareas. It also provides validation,
1719
* custom value replacement, and event handling for changes, submissions, and blur events.
1820
*/
1921
@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
2426
})
2527
export class InputComponent implements OnInit, OnChanges {
2628
/**
2729
* The value of the input field.
2830
*/
29-
@Input() value: string | number | boolean = '';
31+
@Input() value: Value = '';
3032

3133
/**
3234
* A function to replace the input value before emitting changes.
3335
* This allows custom transformations of the input value.
3436
*/
35-
@Input() replace: (
36-
value: string | number | boolean
37-
) => string | number | boolean;
37+
@Input() replace: (value: Value) => Value;
3838

3939
/**
4040
* A function to validate the input value. The default implementation checks for a truthy value.
4141
*/
42-
@Input() valid: (value: string | number | boolean) => boolean = (
43-
value: string | number | boolean
44-
) => !!value;
42+
@Input() valid: (value: Value) => boolean = (value: Value) => !!value;
4543

4644
/**
4745
* A list of items used for radio buttons or other list-based inputs.
@@ -111,12 +109,12 @@ export class InputComponent implements OnInit, OnChanges {
111109
/**
112110
* Event emitted when the input value changes.
113111
*/
114-
@Output() wChange = new EventEmitter<string | number | boolean>();
112+
@Output() wChange = new EventEmitter<Value>();
115113

116114
/**
117115
* Event emitted when the form is submitted.
118116
*/
119-
@Output() wSubmit = new EventEmitter<string | number | boolean>();
117+
@Output() wSubmit = new EventEmitter<Value>();
120118

121119
/**
122120
* Event emitted when the input field loses focus.
@@ -203,4 +201,22 @@ export class InputComponent implements OnInit, OnChanges {
203201
setDisabled(disabled: boolean): void {
204202
this.disabled = disabled;
205203
}
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+
}
206222
}

0 commit comments

Comments
 (0)