Skip to content

Instantly share code, notes, and snippets.

@bobbyg603
Last active September 6, 2022 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbyg603/9619935d52288c0bba2bb6c86d18a2cc to your computer and use it in GitHub Desktop.
Save bobbyg603/9619935d52288c0bba2bb6c86d18a2cc to your computer and use it in GitHub Desktop.
How to Build a Strongly Typed Angular 14 Super Form
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());
}
}
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