Giter Site home page Giter Site logo

baso53 / ngx-typesafe-forms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dirkluijk/ngx-typesafe-forms

0.0 0.0 0.0 1.01 MB

Because FormControl<T> is more awesome than FormControl!

License: MIT License

JavaScript 0.37% HTML 1.29% CSS 0.26% TypeScript 98.07%

ngx-typesafe-forms's Introduction

Type-safe Forms for Angular ๐Ÿ“

Because FormControl<T> is more awesome than FormControl!

NPM version NPM downloads Build status All Contributors

Overview

What? ๐Ÿค”

This is a small library to make Angular Forms more type-safe!

  • Type-safe versions of FormControl, FormGroup, FormArray and ControlValueAccessor
  • 100% compatible with @angular/forms and existing Angular libraries!
  • Easy to use!
  • Additional read-only properties value$, valid$, pristine$, errors$, enabled$ and more.
  • A default implementation for ControlValueAccessor
  • Type-safe validators

Why? ๐Ÿคทโ€โ™‚๏ธ

Angular Forms are not very type-safe. They accept any type of value. This library makes your code more type-safe. More type-safety means smaller risk for bugs!

Installation ๐ŸŒฉ

npm
npm install ngx-typesafe-forms
yarn
yarn add ngx-typesafe-forms

Usage ๐Ÿ•น

Basic example

Just import your FormControl, FormGroup, FormArray and ControlValueAccessor from ngx-typesafe-forms instead of @angular/forms and you are done!

import { FormControl, FormGroup } from 'ngx-typesafe-forms';

@Component({ /* ... */ })
class YourComponent {
  myControl = new FormControl<string>();
  
  myFormGroup = new FormGroup<Person>({
    name: new FormControl(),
    birthDate: new FormControl(new Date())
  });
  
  changeDate(): void {
    this.myControl.setValue(123); // error: this will not compile!
    this.myFormGroup.controls.birthDate.setValue('foo'); // error: this will not compile!
    
    this.myControl.setValue('Foo'); // yes, this will!
    this.myFormGroup.controls.birthDate.setValue(new Date()); // yes, this will!
  }
}

Additional reactive properties

Besides the type-safety, we also provide additional reactive properties.

const myControl = new FormControl<string>('bar');

// subscribe to all values, including the existing value!
myControl.value$.subscribe((value) => {
  /* ... */
});

// subscribe to validity changes, including the existing valid state!
myControl.valid$.subscribe((valid) => {
  /* ... */
});

The recommended properties are:

  • value$
  • errors$
  • enabled$
  • pristine$
  • valid$
  • status$

Additionally, we also provide their counterparts:

  • disabled$
  • dirty$
  • invalid$

NOTE: all of these streams also include the current (initial) values.

FAQ

How to deal with validation and null values?

If you want to assume that form values are always valid, just use FormControl<Foo>.

However, form values can also be null (if invalid). If you want to use this more strictly, you can use FormControl<Foo | null> or even FormGroup<Invalidated<Foo>> (which marks all properties as nullable).

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

Dirk Luijk
Dirk Luijk

๐Ÿ’ป ๐Ÿ“–
Daan Scheerens
Daan Scheerens

๐Ÿค”

This project follows the all-contributors specification. Contributions of any kind welcome!

ngx-typesafe-forms's People

Contributors

dirkluijk avatar donroyco avatar

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.