Last active
June 10, 2022 13:33
-
-
Save NyaGarcia/da0ed888f32cf2e58b2950247fb51ba4 to your computer and use it in GitHub Desktop.
Creating the pokemon form dialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Inject, OnInit } from '@angular/core'; | |
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; | |
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; | |
import { Pokemon } from '../../interfaces/pokemon.interface'; | |
@Component({ | |
selector: 'app-form', | |
templateUrl: './form.component.html', | |
styleUrls: ['./form.component.scss'], | |
}) | |
export class FormComponent implements OnInit { | |
form: FormGroup; | |
constructor( | |
private readonly formBuilder: FormBuilder, | |
public readonly dialogRef: MatDialogRef<FormComponent>, | |
@Inject(MAT_DIALOG_DATA) private readonly pokemon: Pokemon | |
) {} | |
ngOnInit() { | |
this.setForm(); | |
} | |
setForm() { | |
this.form = this.formBuilder.group({ | |
name: [this.pokemon.name, [Validators.required]], | |
type: [this.pokemon.type, [Validators.required]], | |
description: [this.pokemon.description, [Validators.required]], | |
height: [this.pokemon.height, [Validators.required]], | |
weight: [this.pokemon.weight, [Validators.required]], | |
imgUrl: [this.pokemon.imgUrl, [Validators.required]], | |
}); | |
} | |
submit() { | |
this.dialogRef.close({ ...this.pokemon, ...this.form.value }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment