Last active
September 6, 2022 13:12
-
-
Save bobbyg603/9619935d52288c0bba2bb6c86d18a2cc to your computer and use it in GitHub Desktop.
How to Build a Strongly Typed Angular 14 Super Form
This file contains 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, EventEmitter, Output } from '@angular/core'; | |
import { FormControl, FormGroup } from '@angular/forms'; | |
import { Item } from '../cart/item'; | |
import { Shirt, Color, Size } from './shirt'; | |
@Component({ | |
selector: 'app-shirt', | |
templateUrl: './shirt.component.html', | |
styleUrls: ['./shirt.component.css'], | |
}) | |
export class ShirtComponent { | |
@Output() addShirtToCartRequested = new EventEmitter<Item<Shirt>>(); | |
formGroup = new FormGroup({ | |
quantity: new FormControl(1), | |
item: new FormGroup({ | |
color: new FormControl(Color.Green), | |
size: new FormControl(Size.medium), | |
text: new FormControl('hello world 🌎'), | |
}), | |
}); | |
onSubmit(): void { | |
this.addShirtToCartRequested.emit(this.formGroup.getRawValue()); | |
} | |
} |
This file contains 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
export interface Item<T> { | |
item: T; | |
quantity: number; | |
} | |
export interface Shirt { | |
text: string; | |
color: Color; | |
size: Size; | |
} | |
export enum Color { | |
Red = 'Red', | |
Blue = 'Blue', | |
Green = 'Green', | |
Yellow = 'Yellow', | |
Pink = 'Pink', | |
} | |
export enum Size { | |
xs = 'XS', | |
small = 'S', | |
medium = 'M', | |
large = 'L', | |
xl = 'XL', | |
xxl = 'XXL', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment