Skip to content

Commit d70af22

Browse files
committed
feat: simplify multiselect and make reusable
1 parent cd4139c commit d70af22

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/app/multiselect/multiselect.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
</summary>
55
@if (!disabled()) {
66
<ul>
7-
@for (topic of allTopics; track $index) {
7+
@for (option of selectOptions(); track $index) {
88
<li>
99
<label>
1010
<input
1111
type="checkbox"
12-
[name]="topic"
13-
[checked]="value().includes(topic)"
14-
(input)="changeInput(topic, $event)"
12+
[name]="option"
13+
[checked]="value().includes(option)"
14+
(input)="changeInput(option, $event)"
1515
/>
16-
{{ topic }}
16+
{{ option }}
1717
</label>
1818
</li>
1919
}

src/app/multiselect/multiselect.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import { FormValueControl, ValidationError, WithOptionalField } from '@angular/f
88
styleUrl: './multiselect.scss',
99
})
1010
export class Multiselect implements FormValueControl<string[]> {
11-
readonly allTopics = ['Angular', 'Vue', 'React'];
1211
readonly value = model<string[]>([]);
13-
readonly label = input.required<string>();
1412
readonly errors = input<readonly WithOptionalField<ValidationError>[]>([]);
1513
readonly disabled = input<boolean>(false);
1614

17-
changeInput(topic: string, e: Event) {
15+
readonly label = input.required<string>();
16+
readonly selectOptions = input.required<string[]>();
17+
18+
changeInput(option: string, e: Event) {
1819
const checked = (e.target as HTMLInputElement).checked;
19-
const isInModel = this.value().includes(topic) && checked;
20-
if (!isInModel && checked) {
21-
this.value.update((current) => [...current, topic]);
22-
return;
23-
}
24-
if (!checked) {
25-
this.value.update((current) => current.filter((t) => t !== topic));
20+
if (checked) {
21+
// option checked, add to list
22+
this.value.update((current) => [...current, option]);
23+
} else {
24+
// option unchecked, remove from list
25+
this.value.update((current) => current.filter((o) => o !== option));
2626
}
2727
}
2828

src/app/registration-form-3/registration-form-3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ <h1>Version 3: Child Forms and Custom UI Controls</h1>
8888
</label>
8989
<app-multiselect
9090
[field]="registrationForm.newsletterTopics"
91+
[selectOptions]="['Angular', 'React', 'Vue', 'Svelte']"
9192
label="Topics (multiple possible):"
9293
/>
9394
<app-form-error [fieldRef]="registrationForm.newsletterTopics" />

src/app/registration-form-3/registration-form-3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const initialState: RegisterFormData = {
2525
age: 18,
2626
password: { pw1: '', pw2: '' },
2727
email: [''],
28-
newsletter: false,
28+
newsletter: true,
2929
newsletterTopics: ['Angular'],
3030
agreeToTermsAndConditions: false,
3131
};

0 commit comments

Comments
 (0)