Code Monkey home page Code Monkey logo

demo_ngx-typed-reactive-form's Introduction

NgxTypedReactiveForm

What is it

It is an attempt to bring types to Angular's Reactive Forms using generics. For the sake of intellisense and better developer experience.

Online demo: strongly typed reactive form on Stackblitz

Features

  • No overrides.
  • No API to learn.
  • Types for Valuechanges, value, etc.
  • Four generic types:
    • TypedFormBuilderConfig
    • TypedFormControl
    • TypedFormGroup
    • TypedFormArray

Usage

  • Start with a model:
export interface UserDto {
    name: string;
    about: string;
    registered: string;
  }
  • Generate a strongly typed TypedFormBuilderConfig based on the model, to use with Reactive Forms FormBuilder:
export const USER_FORM_CONFIG: TypedFormBuilderConfig<UserDto> = {
    /** NOTE: value is strongly typed */
  name: ['John Doe', Validators.required],
  about: ['This is a generic description', Validators.required],
  registered: ['1992-12-12', Validators.required],
};
  • Create a typed reactive form:
export class DemoComponent implements OnInit {
  public userFormGroup: TypedFormGroup<UserDto>;

  constructor(private fb: FormBuilder) {}

  ngOnInit(): void {
    this.userFormGroup = this.fb.group(USER_FORM_CONFIG) as TypedFormGroup<UserDto>;
  }
}
  • Enjoy intellisense in template and form's methods:
<input [formControl]="userFormGroup.controls.name" />

Don't forget to star in on github if you like it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.