I'm currently working with reactive forms in my Angular project and utilizing mat-country-select to display a list of countries in a select dropdown within my child FormComponent.
<mat-select-country class="full-width"
[formControl]="form.controls.country"
[required]="true"
appearance="outline"
[itemsLoadSize]="0"
[value]="DEFAULT_COUNTRY"
label="Choose country">
</mat-select-country>
I am attempting to create a modal dialog window for updating a Player in the parent PlayerComponent, defined by the structure below:
export interface Player extends BaseResponse {
lastName: string;
firstName: string;
dateOfBirth: Date;
country: string;
height: number;
weight: number;
position: string;
jerseyNumber: number;
teamName: string;
teamId?: string | null;
photoPath?: string | null;
}
Prior to opening the update dialog window in the PlayerComponent, I need to initialize and pass a form to my FormComponent.
export class PlayersComponent implements OnInit {
countries: Country[] = COUNTRIES;
playerActionsForm!: FormGroup<PlayerActionsForm>;
fillUpdatePlayerForm(player: Player) {
this.playerActionsForm.controls.id.setValue(player.id);
this.playerActionsForm.controls.firstName.setValue(player.firstName);
this.playerActionsForm.controls.lastName.setValue(player.lastName);
this.playerActionsForm.controls.birthDay.setValue(player.dateOfBirth);
const country = this.countries.find(
(country) => country.name === player.country,
);
this.playerActionsForm.controls.country.setValue(country ? country : null);
this.playerActionsForm.controls.height.setValue(player.height);
this.playerActionsForm.controls.weight.setValue(player.weight);
this.selectTeams$
.pipe(
map((teams) => teams?.find((team) => team.id === player.teamId)),
take(1),
untilDestroyed(this),
)
.subscribe((team) => {
this.playerActionsForm.controls.team.setValue(team ? team : null);
});
this.playerActionsForm.controls.jerseyNumber.setValue(player.jerseyNumber);
this.playerActionsForm.controls.position.setValue(player.position);
this.playerActionsForm.controls.experiences.setValue([]);
}
initializeActionsForm() {
this.playerActionsForm = new FormGroup<PlayerActionsForm>({
id: new FormControl(''),
firstName: new FormControl('', [
Validators.required,
Validators.pattern(NAME_PATTERN),
]),
lastName: new FormControl('', [
Validators.required,
Validators.pattern(NAME_PATTERN),
]),
birthDay: new FormControl(null, [
Validators.required,
dateValidator(moment().subtract(18, 'years').toDate(), 'max'),
]),
country: new FormControl(null, Validators.required),
height: new FormControl(1.01, [
Validators.required,
Validators.min(1.01),
]),
weight: new FormControl(50.01, [
Validators.required,
Validators.min(50.01),
]),
team: new FormControl(null),
position: new FormControl('', Validators.required),
jerseyNumber: new FormControl(0, [
Validators.required,
Validators.min(0),
]),
experiences: new FormArray<FormGroup<PlayerExperienceForm>>([]),
});
}
ngOnInit(): void {
this.initializeActionsForm();
}
Currently, I am trying to utilize ViewChild to access the Input property of mat-country-select [Countries], however, it is not functioning as expected. As of now, I am using a predefined array named COUNTRIES which was manually created. Ideally, I am seeking a solution that can easily retrieve a specific country by name without the need for manually creating constants.