Skip to content

Commit 5a5cce1

Browse files
committed
feat: add comments and adjust structure
1 parent 45e4287 commit 5a5cce1

File tree

3 files changed

+107
-14
lines changed

3 files changed

+107
-14
lines changed

src/app/reactive/reactive.component.css

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ body :-ms-input-placeholder {
5757
top: 50%;
5858
left: 0;
5959
width: 100%;
60-
height: 400px;
60+
height: 500px;
6161
margin-top: -200px;
6262
overflow: hidden;
6363
}
@@ -69,7 +69,7 @@ body :-ms-input-placeholder {
6969
max-width: 600px;
7070
margin: 0 auto;
7171
padding: 80px 0;
72-
height: 400px;
72+
height: 500px;
7373
text-align: center;
7474
}
7575
.container h1 {
@@ -241,3 +241,30 @@ form button:hover {
241241
transform: translateY(-700px) rotate(600deg);
242242
}
243243
}
244+
245+
.fill-button.password {
246+
margin-top: 10px;
247+
width: 145px;
248+
position: absolute;
249+
right: 150px;
250+
}
251+
252+
.fill-button.mail {
253+
margin-top: 10px;
254+
width: 145px;
255+
position: absolute;
256+
left: 150px;
257+
}
258+
259+
.button:hover {
260+
color: plum;
261+
}
262+
263+
.button:active {
264+
color: orange;
265+
background-color: aqua !important;
266+
}
267+
268+
.delete {
269+
margin-top: 10px;
270+
}

src/app/reactive/reactive.component.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@
44
<p>REACTIVE</p>
55
</div>
66
<h1>Welcome</h1>
7+
<!-- using formGroup to link form with component -->
78
<form class="form" [formGroup]="loginForm" (ngSubmit)="submit()">
8-
<input type="text" placeholder="userName" formControlName="userName" />
9+
<!-- using formControlName to link form with component's attribute -->
10+
<input type="email" placeholder="mail" formControlName="mail" />
911
<input type="password" placeholder="Password" formControlName="password" />
10-
<button type="submit" id="login-button" [disabled]="!loginForm.valid">Login</button>
12+
<button class="button" type="submit" id="login-button" [disabled]="!loginForm.valid">Login</button>
13+
<br />
14+
<button class="button fill-button mail" type="button" id="fill-mail-button" [disabled]="!loginForm.valid" (click)="fillMail()">
15+
Fill Mail
16+
</button>
17+
<button
18+
class="button fill-button password"
19+
type="button"
20+
id="fill-password-button"
21+
[disabled]="!loginForm.valid"
22+
(click)="fillPassword()"
23+
>
24+
Fill Password
25+
</button>
26+
<br />
27+
<br />
28+
<br />
29+
<button class="button delete" type="button" id="delete-button" (click)="deleteValue()">DELETE</button>
1130
</form>
1231
</div>
1332

src/app/reactive/reactive.component.ts

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,68 @@ import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
99
export class ReactiveComponent implements OnInit {
1010
constructor(private builder: FormBuilder) {}
1111

12-
loginForm = this.builder.group({
13-
userName: [''],
12+
// Using FormBuilder to link and initialize form values
13+
public loginForm = this.builder.group({
14+
mail: [''],
1415
password: [''],
1516
});
1617

17-
// this also possible though it could be troublesome (using instance)
18-
/* protected loginForm = new FormGroup({
19-
userName: new FormControl(''),
20-
password: new FormControl(''),
21-
});*/
22-
23-
ngOnInit(): void {}
18+
/**
19+
* This also possible though it could be troublesome (using instance..)
20+
*/
21+
// public loginForm = new FormGroup({
22+
// mail: new FormControl(''),
23+
// password: new FormControl(''),
24+
// });
2425

26+
/**
27+
* Submit form values
28+
*/
2529
submit() {
2630
console.log('submit');
27-
console.log(this.loginForm.value);
31+
window.alert(JSON.stringify(this.loginForm.value));
32+
}
33+
34+
/**
35+
* Fill emailAddress with specific email
36+
* L shows example for filling email type
37+
* L Overwrites existing value
38+
*/
39+
fillMail() {
40+
// when using FormBuilder, you can set values from method, patchValue()
41+
this.loginForm.patchValue({
42+
mail: 'sample@example.com',
43+
});
44+
window.alert(JSON.stringify(this.loginForm.value));
45+
}
46+
47+
/**
48+
* Fill password with specific password
49+
* L Shows example for filling password type
50+
* L Overwrites existing value
51+
*/
52+
fillPassword() {
53+
// when using FormBuilder, you can set values from method, patchValue()
54+
this.loginForm.patchValue({
55+
password: 'pass1word',
56+
});
57+
window.alert(JSON.stringify(this.loginForm.value));
58+
}
59+
60+
/**
61+
* Delete form values
62+
*/
63+
deleteValue() {
64+
this.loginForm.patchValue({
65+
mail: '',
66+
password: '',
67+
});
68+
}
69+
70+
/**
71+
* Initialize form values
72+
*/
73+
ngOnInit(): void {
74+
this.deleteValue();
2875
}
2976
}

0 commit comments

Comments
 (0)