-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion-2-version-3.diff
More file actions
315 lines (304 loc) · 9.87 KB
/
version-2-version-3.diff
File metadata and controls
315 lines (304 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
diff --git a/tmp/version-2/identity-form.html b/tmp/version-3/identity-form.html
index e69de29..70fa474 100644
--- a/tmp/version-2/identity-form.html
+++ b/tmp/version-3/identity-form.html
@@ -0,0 +1,30 @@
+<!-- TODO: remove @if, necessary due to a bug with <select> -->
+@if (identity()) {
+ <label
+ >Gender
+ <select [formField]="identity().gender"
+ (change)="updateSalutationAndPronoun()"
+ >
+ <option value="">Please select</option>
+ <option value="male">Male</option>
+ <option value="female">Female</option>
+ <option value="diverse">Diverse</option>
+ </select>
+ </label>
+
+ <div class="group-with-gap">
+ @if (!identity().salutation().hidden()) {
+ <label
+ >Salutation
+ <input type="text" placeholder="e. g. Mx." [formField]="identity().salutation" />
+ <app-form-error [fieldRef]="identity().salutation" />
+ </label>
+ } @if (!identity().pronoun().hidden()) {
+ <label
+ >Pronoun
+ <input type="text" placeholder="e. g. they/them" [formField]="identity().pronoun" />
+ <app-form-error [fieldRef]="identity().pronoun" />
+ </label>
+ }
+ </div>
+}
diff --git a/tmp/version-2/identity-form.ts b/tmp/version-3/identity-form.ts
index e69de29..ac83145 100644
--- a/tmp/version-2/identity-form.ts
+++ b/tmp/version-3/identity-form.ts
@@ -0,0 +1,51 @@
+import { Component, input } from '@angular/core';
+import { FormField, FieldTree, hidden, required, schema } from '@angular/forms/signals';
+
+import { FormError } from '../form-error/form-error';
+
+export interface GenderIdentity {
+ gender: '' | 'male' | 'female' | 'diverse';
+ salutation: string; // e. g. "Mx.", "Dr.", etc.
+ pronoun: string; // e. g. "they/them"
+}
+
+export const initialGenderIdentityState: GenderIdentity = {
+ gender: '',
+ salutation: '',
+ pronoun: '',
+};
+
+export const identitySchema = schema<GenderIdentity>((path) => {
+ hidden(path.salutation, (ctx) => {
+ return !ctx.valueOf(path.gender) || ctx.valueOf(path.gender) !== 'diverse';
+ });
+ hidden(path.pronoun, (ctx) => {
+ return !ctx.valueOf(path.gender) || ctx.valueOf(path.gender) !== 'diverse';
+ });
+
+ required(path.salutation, {
+ when: (ctx) => ctx.valueOf(path.gender) === 'diverse',
+ message: 'Please choose a salutation, when diverse gender selected.',
+ });
+ required(path.pronoun, {
+ when: (ctx) => ctx.valueOf(path.gender) === 'diverse',
+ message: 'Please choose a pronoun, when diverse gender selected.',
+ });
+});
+
+@Component({
+ selector: 'app-identity-form',
+ imports: [FormField, FormError],
+ templateUrl: './identity-form.html',
+ styleUrl: './identity-form.scss',
+})
+export class IdentityForm {
+ readonly identity = input.required<FieldTree<GenderIdentity>>();
+
+ protected updateSalutationAndPronoun() {
+ this.identity().salutation().value.set('');
+ this.identity().salutation().reset();
+ this.identity().pronoun().value.set('');
+ this.identity().pronoun().reset();
+ }
+}
diff --git a/tmp/version-2/multiselect.html b/tmp/version-3/multiselect.html
index e69de29..25af118 100644
--- a/tmp/version-2/multiselect.html
+++ b/tmp/version-3/multiselect.html
@@ -0,0 +1,22 @@
+<details class="dropdown" [aria-disabled]="disabled()" [aria-describedby]="ariaDescribedBy()">
+ <summary>
+ {{ label() }}
+ </summary>
+ @if (!disabled()) {
+ <ul>
+ @for (option of selectOptions(); track $index) {
+ <li>
+ <label>
+ <input
+ type="checkbox"
+ [name]="option"
+ [checked]="value().includes(option)"
+ (input)="changeInput(option, $event)"
+ />
+ {{ option }}
+ </label>
+ </li>
+ }
+ </ul>
+ }
+</details>
diff --git a/tmp/version-2/multiselect.ts b/tmp/version-3/multiselect.ts
index e69de29..42bc6e6 100644
--- a/tmp/version-2/multiselect.ts
+++ b/tmp/version-3/multiselect.ts
@@ -0,0 +1,37 @@
+import { Component, effect, input, model } from '@angular/core';
+import { FormValueControl, ValidationError } from '@angular/forms/signals';
+
+@Component({
+ selector: 'app-multiselect',
+ imports: [],
+ templateUrl: './multiselect.html',
+ styleUrl: './multiselect.scss',
+})
+export class Multiselect implements FormValueControl<string[]> {
+ readonly value = model<string[]>([]);
+ readonly errors = input<readonly ValidationError[]>([]);
+ readonly disabled = input<boolean>(false);
+ readonly ariaDescribedBy = input<string>();
+
+ readonly label = input.required<string>();
+ readonly selectOptions = input.required<string[]>();
+
+ changeInput(option: string, e: Event) {
+ const checked = (e.target as HTMLInputElement).checked;
+ if (checked) {
+ // option checked, add to list
+ this.value.update((current) => [...current, option]);
+ } else {
+ // option unchecked, remove from list
+ this.value.update((current) => current.filter((o) => o !== option));
+ }
+ }
+
+ constructor() {
+ effect(() => {
+ if (this.disabled()) {
+ this.value.set([]);
+ }
+ });
+ }
+}
diff --git a/tmp/version-2/registration-form.html b/tmp/version-3/registration-form.html
index 85cc7c7..12b201d 100644
--- a/tmp/version-2/registration-form.html
+++ b/tmp/version-3/registration-form.html
@@ -12,6 +12,9 @@
<app-form-error [fieldRef]="registrationForm.username" />
</label>
+ <!-- A whole child form with own model -->
+ <app-identity-form [identity]="registrationForm.identity" />
+
<!-- native HTML inputs bound with the [formField] directive -->
<div>
<label
@@ -75,18 +78,12 @@
>Subscribe to Newsletter?
<input type="checkbox" [formField]="registrationForm.newsletter" />
</label>
-
- <label>
- Topics (multiple possible):
- <select [formField]="registrationForm.newsletterTopics">
- <option value=""></option>
- <option value="Angular">Angular</option>
- <option value="Vue">Vue</option>
- <option value="React">React</option>
- </select>
- <app-form-error [fieldRef]="registrationForm.newsletterTopics" />
- </label>
-
+ <app-multiselect
+ [formField]="registrationForm.newsletterTopics"
+ [selectOptions]="['Angular', 'React', 'Vue', 'Svelte']"
+ label="Topics (multiple possible):"
+ />
+ <app-form-error [fieldRef]="registrationForm.newsletterTopics" />
<label
>I agree to the terms and conditions
<input
diff --git a/tmp/version-2/registration-form.ts b/tmp/version-3/registration-form.ts
index 09ec664..4e34e02 100644
--- a/tmp/version-2/registration-form.ts
+++ b/tmp/version-3/registration-form.ts
@@ -1,10 +1,12 @@
import { Component, inject, resource, signal } from '@angular/core';
import {
+ apply,
applyEach,
applyWhen,
- FormField,
+ debounce,
disabled,
email,
+ FormField,
FieldTree,
form,
maxLength,
@@ -23,25 +25,34 @@ import {
import { DebugOutput } from '../debug-output/debug-output';
import { FormError } from '../form-error/form-error';
+import {
+ GenderIdentity,
+ IdentityForm,
+ identitySchema,
+ initialGenderIdentityState,
+} from '../identity-form/identity-form';
+import { Multiselect } from '../multiselect/multiselect';
import { RegistrationService } from '../registration-service';
export interface RegisterFormData {
username: string;
+ identity: GenderIdentity;
age: number;
password: { pw1: string; pw2: string };
email: string[];
newsletter: boolean;
- newsletterTopics: string;
+ newsletterTopics: string[];
agreeToTermsAndConditions: boolean;
}
const initialState: RegisterFormData = {
username: '',
+ identity: initialGenderIdentityState,
age: 18,
password: { pw1: '', pw2: '' },
email: [''],
- newsletter: false,
- newsletterTopics: '',
+ newsletter: true,
+ newsletterTopics: ['Angular'],
agreeToTermsAndConditions: false,
};
@@ -50,6 +61,7 @@ export const formSchema = schema<RegisterFormData>((path) => {
required(path.username, { message: 'Username is required.' });
minLength(path.username, 3, { message: 'A username must be at least 3 characters long.' });
maxLength(path.username, 12, { message: 'A username can be max. 12 characters long.' });
+ debounce(path.username, 500);
validateAsync(path.username, {
// Reactive params
params: (ctx) => ctx.value(),
@@ -137,13 +149,20 @@ export const formSchema = schema<RegisterFormData>((path) => {
// Disable newsletter topics when newsletter is unchecked
disabled(path.newsletterTopics, (ctx) => !ctx.valueOf(path.newsletter));
+
+ // apply child schema for identity checks
+ apply(path.identity, identitySchema);
});
@Component({
selector: 'app-registration-form',
- imports: [FormField, DebugOutput, FormError, FormRoot],
+ imports: [FormField, DebugOutput, FormError, FormRoot, IdentityForm, Multiselect],
templateUrl: './registration-form.html',
styleUrl: './registration-form.scss',
+ // Also possible: set SignalFormsConfig only for local component:
+ // providers: [
+ // provideSignalFormsConfig(signalFormsConfig)
+ // ]
})
export class RegistrationForm {
readonly #registrationService = inject(RegistrationService);
@@ -159,6 +178,7 @@ export class RegistrationForm {
try {
await this.#registrationService.registerUser(form().value);
+ setTimeout(() => this.resetForm(), 3000);
} catch (e) {
errors.push({
fieldTree: form,
@@ -167,7 +187,6 @@ export class RegistrationForm {
});
}
- setTimeout(() => this.resetForm(), 3000);
return errors;
},
},
@@ -175,13 +194,6 @@ export class RegistrationForm {
);
protected ariaInvalidState(field: FieldTree<unknown>): boolean | undefined {
- if (field().value() === 'validuser') {
- console.log('###### FIELD:', {
- touched: field().touched(),
- pending: field().pending(),
- errors: field().errors(),
- });
- }
return field().touched() && !field().pending() ? field().errors().length > 0 : undefined;
}