Skip to content

Commit 5642a60

Browse files
committed
feat: move child form initial satte into child form
1 parent 508a9da commit 5642a60

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

src/app/identity-form/identity-form.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export interface GenderIdentity {
88
pronoun: string; // e. g. "they/them"
99
}
1010

11+
export const initialGenderIdentityState: GenderIdentity = {
12+
gender: '',
13+
salutation: '',
14+
pronoun: '',
15+
};
16+
1117
export const identitySchema = schema<GenderIdentity>((path) => {
1218
hidden(path.salutation, (ctx) => {
1319
return !ctx.valueOf(path.gender) || ctx.valueOf(path.gender) !== 'diverse';

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ export interface RegisterFormData {
1414
agreeToTermsAndConditions: boolean;
1515
}
1616

17+
const initialState: RegisterFormData = {
18+
username: '',
19+
age: 18,
20+
email: [''],
21+
newsletter: false,
22+
agreeToTermsAndConditions: false,
23+
};
24+
1725
export const formSchema = schema<RegisterFormData>((fieldPath) => {
1826
// Username validation
1927
required(fieldPath.username, { message: 'Username is required' });
@@ -29,14 +37,6 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
2937
});
3038
});
3139

32-
const initialState: RegisterFormData = {
33-
username: '',
34-
age: 18,
35-
email: [''],
36-
newsletter: false,
37-
agreeToTermsAndConditions: false,
38-
};
39-
4040
@Component({
4141
selector: 'app-registration-form-1',
4242
imports: [BackButton, Control, DebugOutput, FormError],

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export interface RegisterFormData {
1616
agreeToTermsAndConditions: boolean;
1717
}
1818

19+
const initialState: RegisterFormData = {
20+
username: '',
21+
age: 18,
22+
password: { pw1: '', pw2: '' },
23+
email: [''],
24+
newsletter: false,
25+
newsletterTopics: '',
26+
agreeToTermsAndConditions: false,
27+
};
28+
1929
export const formSchema = schema<RegisterFormData>((fieldPath) => {
2030
// Username validation
2131
required(fieldPath.username, { message: 'Username is required' });
@@ -104,21 +114,11 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
104114
);
105115
}
106116
);
107-
117+
108118
// Disable newsletter topics when newsletter is unchecked
109119
disabled(fieldPath.newsletterTopics, (ctx) => !ctx.valueOf(fieldPath.newsletter));
110120
});
111121

112-
const initialState: RegisterFormData = {
113-
username: '',
114-
age: 18,
115-
password: { pw1: '', pw2: '' },
116-
email: [''],
117-
newsletter: false,
118-
newsletterTopics: '',
119-
agreeToTermsAndConditions: false,
120-
};
121-
122122
@Component({
123123
selector: 'app-registration-form-2',
124124
imports: [BackButton, Control, DebugOutput, FormError],

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { apply, applyEach, applyWhen, Control, customError, CustomValidationErro
44
import { BackButton } from '../back-button/back-button';
55
import { DebugOutput } from '../debug-output/debug-output';
66
import { FormError } from '../form-error/form-error';
7-
import { GenderIdentity, IdentityForm, identitySchema } from '../identity-form/identity-form';
7+
import { GenderIdentity, IdentityForm, identitySchema, initialGenderIdentityState } from '../identity-form/identity-form';
88
import { Multiselect } from '../multiselect/multiselect';
99
import { RegistrationService } from '../registration-service';
1010

@@ -19,6 +19,17 @@ export interface RegisterFormData {
1919
agreeToTermsAndConditions: boolean;
2020
}
2121

22+
const initialState: RegisterFormData = {
23+
username: '',
24+
identity: initialGenderIdentityState,
25+
age: 18,
26+
password: { pw1: '', pw2: '' },
27+
email: [''],
28+
newsletter: false,
29+
newsletterTopics: ['Angular'],
30+
agreeToTermsAndConditions: false,
31+
};
32+
2233
export const formSchema = schema<RegisterFormData>((fieldPath) => {
2334
// Username validation
2435
required(fieldPath.username, { message: 'Username is required' });
@@ -115,21 +126,6 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
115126
apply(fieldPath.identity, identitySchema);
116127
});
117128

118-
const initialState: RegisterFormData = {
119-
username: '',
120-
identity: {
121-
gender: '',
122-
salutation: '',
123-
pronoun: '',
124-
},
125-
age: 18,
126-
password: { pw1: '', pw2: '' },
127-
email: [''],
128-
newsletter: false,
129-
newsletterTopics: ['Angular'],
130-
agreeToTermsAndConditions: false,
131-
};
132-
133129
@Component({
134130
selector: 'app-registration-form-3',
135131
imports: [BackButton, Control, DebugOutput, FormError, IdentityForm, Multiselect],

0 commit comments

Comments
 (0)