refactor: form header as content projection, formgroup as input
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<form [formGroup]="form">
|
<form [formGroup]="form()" (ngSubmit)="handleSubmit(form().value)">
|
||||||
<legend>{{strings.addContact|upperfirst}}.<span>{{strings.allFieldRequired|upperfirst}}</span></legend>
|
<ng-content select="[slot='header']"></ng-content>
|
||||||
|
|
||||||
<div class="fields">
|
<div class="fields">
|
||||||
<app-form-field
|
<app-form-field
|
||||||
[errorsDictionary]="companyAndNameErrorsDictionary"
|
[errorsDictionary]="companyAndNameErrorsDictionary"
|
||||||
|
|||||||
@@ -1,20 +1,3 @@
|
|||||||
form {
|
|
||||||
legend {
|
|
||||||
display: inline-block;
|
|
||||||
font-family: var(--secondaryFont);
|
|
||||||
font-size: 2rem;
|
|
||||||
text-align: center;
|
|
||||||
|
|
||||||
span {
|
|
||||||
clear: both;
|
|
||||||
display: block;
|
|
||||||
font-size: 1rem;
|
|
||||||
margin-bottom: 2rem;
|
|
||||||
margin-top: 1rem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.footer {
|
.footer {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import { Component, inject } from '@angular/core';
|
import { Component, inject, input, output } from '@angular/core';
|
||||||
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
import { ReactiveFormsModule } from '@angular/forms';
|
||||||
import { FormField } from '../form-field/form-field';
|
import { FormField } from '../form-field/form-field';
|
||||||
import { STRINGS_INJECTOR } from '../../app.config';
|
import { STRINGS_INJECTOR } from '../../app.config';
|
||||||
import { SquaredBtn } from '../squared-btn/squared-btn';
|
import { SquaredBtn } from '../squared-btn/squared-btn';
|
||||||
import { NameAndCompanyFieldsErrorsDictionary } from '../../errors-dictionaries/name-and-company-field';
|
import { NameAndCompanyFieldsErrorsDictionary } from '../../errors-dictionaries/name-and-company-field';
|
||||||
import { PhoneFieldErroresDictionary } from '../../errors-dictionaries/phone-field';
|
import { PhoneFieldErroresDictionary } from '../../errors-dictionaries/phone-field';
|
||||||
import { UpperfirstPipe } from "../../pipes/upperfirst-pipe";
|
import { UpperfirstPipe } from "../../pipes/upperfirst-pipe";
|
||||||
|
import { ContactDTO } from '../../models/ContactDTO';
|
||||||
|
import { ContactFormValue } from '../../types/ContactFormValue.type';
|
||||||
|
import { FormGroupContact } from '../../utils/form-group-contact';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-contact-form',
|
selector: 'app-contact-form',
|
||||||
@@ -14,13 +17,16 @@ import { UpperfirstPipe } from "../../pipes/upperfirst-pipe";
|
|||||||
styleUrl: './contact-form.scss',
|
styleUrl: './contact-form.scss',
|
||||||
})
|
})
|
||||||
export class ContactForm {
|
export class ContactForm {
|
||||||
form = new FormGroup({
|
contact = output<ContactDTO>();
|
||||||
name: new FormControl(null, [Validators.required, Validators.maxLength(55)]),
|
form = input(new FormGroupContact());
|
||||||
company: new FormControl(null, [Validators.required, Validators.maxLength(55)]),
|
|
||||||
phone: new FormControl(null, [Validators.required, Validators.pattern( /^(\+?[\d]){12,15}$/ )]),
|
|
||||||
});
|
|
||||||
protected strings = inject(STRINGS_INJECTOR);
|
protected strings = inject(STRINGS_INJECTOR);
|
||||||
protected companyAndNameErrorsDictionary = new NameAndCompanyFieldsErrorsDictionary().getDictionary();
|
protected companyAndNameErrorsDictionary = new NameAndCompanyFieldsErrorsDictionary().getDictionary();
|
||||||
protected phoneErrorsDictionary = new PhoneFieldErroresDictionary().getDictionary();
|
protected phoneErrorsDictionary = new PhoneFieldErroresDictionary().getDictionary();
|
||||||
|
|
||||||
|
handleSubmit(contactForm: ContactFormValue) {
|
||||||
|
if(contactForm.company === null || contactForm.name === null || contactForm.phone === null) return;
|
||||||
|
const contact = new ContactDTO(undefined, contactForm.name, contactForm.company, contactForm.phone);
|
||||||
|
this.contact.emit(contact);
|
||||||
|
this.form().reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
src/app/types/ContactFormValue.type.ts
Normal file
1
src/app/types/ContactFormValue.type.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export type ContactFormValue = Partial<{name: string|null, company: string|null, phone: string|null}>;
|
||||||
Reference in New Issue
Block a user