npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@browsepedia/ng-forms

v1.2.1

Published

Better typed Angular 14 Forms + aggregating form data accross multiple pages/components.

Downloads

44

Readme

@browsepedia/ng-forms

Better typed Angular 14 Forms + aggregating form data accross multiple pages/components.

Installation

    npm install --save @browsepedia/ng-forms

Usage

This library has 2 funcionalities:

  1. Better typings for the already typed Angular 14 Reactive Forms.
  2. An @Injectable() service that lets you aggregate Reactive Forms data accross multiple components/pages.

1. Better Typings

Angular 14 shipped a new feature that every Angular developer craved: typing FormGroups. Unfortunatelly, these are very loosely typed, meaning you cannot pass an entity type to a FormGroup generic type, you must pass a Record<string, AbstractControl<T>> to the FormGroup generic.

const form = new FormGroup<{
    firstName: FormControl<string>(''),
    lastName: FormControl<string>('')
}>


// works fine
const firstName = form.controls.firstName.value
const lastName = form.controls.lastName.value

This is ok, but not ideal. We would very much like to have the option to pass an entity to the generic type of FormGroup. But this is not that easy to achieve, because we dont always know the type that the FormControl should manage, and any is out of the question.

Thats where @browsepedia/ng-forms comes to the rescue.

There are 5 FormGroup extending classes inside @browsepedia/ng-forms"

  1. SimpleFormGroup<T> - each key stores a FormControl with the coresponding type.
interface User {
  name: string;
  age: number;
}

SimpleFormGroup<User>()
// is the equivalent of
FormGroup<{
    name: FormControl<string>,
    age: FormControl<number>
}>
  1. BasicFormControl<T> - each key stores either a FormControl or a FormGroup.
interface User {
  name: string;
  age: number;
  history: {
    hiredDate: Date;
    position: string;
  };
}

BasicFormControl<T>
// is the equivalent of
FormGroup<{
    name: FormControl<string>,
    age: FormControl<number>,
    history: FormGroup<{
        hiredDate: FormControl<Date>,
        position: FormControl<string>
    }>
}>

Note that Date, null and array types are still converted to FormControl instances using BasicFormControl.

  1. FlatFormGroup<T> - each key stores the same AbstractControl type as a BasicFormControl<T> with the addition of FormArrays being used for Array<Record<string, any>> This means that a type of number[], boolean[], Date[] and string[] would still be converted to FormControl<number[]>, FormControl<boolean[]>,FormControl<Date[]> or FormControl<string[]>
interface Role {
  name: string;
}

interface User {
  name: string;
  age: number;
  history: {
    hiredDate: Date;
    position: string;
  };
  roles: Role[];
  jobTitles: string[];
}

FlatFormGroup<User>
// is the equivalent of
FormGroup<{
    name: FormControl<string>,
    age: FormControl<number>,
    history: FormGroup<{
        hiredDate: FormControl<Date>,
        position: FormControl<string>
    }>,
    roles: FormArray<FormGroup<{
        name: FormControl<string>
    }>>,
    jobTitles: FormControl<string> // primitive arrays are still FormControls
}>
  1. StrictFormGroup<T> each key stores the AbstractControl based on the type, primitive type arrays become FormArrays.
interface Role {
  name: string;
}

interface User {
  name: string;
  age: number;
  history: {
    hiredDate: Date;
    position: string;
  };
  roles: Role[];
  jobTitles: string[];
}

FlatFormGroup<User>
// is the equivalent of
FormGroup<{
    name: FormControl<string>,
    age: FormControl<number>,
    history: FormGroup<{
        hiredDate: FormControl<Date>,
        position: FormControl<string>
    }>,
    roles: FormArray<FormGroup<{
        name: FormControl<string>
    }>>,
    jobTitles: FormArray<FormControl<string>>
}>
  1. UnrestrictedFormGroup<T> - each key stores any AbstractControl class based on the type. Meaning I can have one primitive array as a FormArray, and another as a FormControl. In addition, I can store Record<string, any> types into FormControls Or FormGroups, its totally unrestricted.
interface User {
  roles: string[];
  jobTitles: string[];
  history: {
    hiredDate: Date;
    position: string;
  };
}

FlatFormGroup<User>
// is the equivalent of
FormGroup<{
    roles: FormControl<string[]>,
    jobTitles: FormArray<FormControl<string>>,
    history: FormControl<User['history']>
}>

// OR

FormGroup<{
    roles: FormControl<string[]>,
    jobTitles: FormArray<FormControl<string>>,
    history: SimpleFormGroup<User['history']>
}>

2. Form Aggregator

The Form Aggregator is basically an @Injectable() service named NgFormsService<T> where T is an entity - User for example.

Usage

NgFormService is set in the providers array of NgFormsModule, which means you can import it in your lazy loaded module to get a per module instance of the service.

Note that for this to work and not have data leak from one form to another this Service NEEDS to be singleton per feature or per component tree where you are using it.

Either import NgFormsModule in your lazy loaded module or set NgFormService<T> in the providers of your component from which you start your aggregated form.

Example:

// user.ts
interface User {
  name: string;
  age: number;
  history: {
    hiredDate: Date;
    position: string;
  };
  roles: Role[];
  jobTitles: string[];
}

interface Role {
  name: string;
}


/// user-form-root.component.ts
  @Component({
    selector: 'app-user-form-root```,
    providers: [NgFormService<User>]
  })
  export class UserFormRootComponent {
    constructor(private _formService: NgFormService<User>) {
      this.form = this._formService.getRootForm()
      // or you can get only the valueChanges.
      // NOTE that formChange will emit the getRawValue() call
      this._formService.formChange$.subscrible(console.log);
    }

    // will hold the entire aggregated form from all
    // the subcomponents where you registered slices of the form.
    protected form: UntypedFormGroup<T>;
  }

  // user-history.component.ts
    export class UserHistoryComponent {
    constructor(private _formService: NgFormService<User>) {
      this.form = new SimpleFormGroup<User['history']>({
        hiredDate: FormControl<Date>(
          new Date(), { nonNullable: true }
        ),
        position: FormControl<string>('',
          { nonNullable: true }
        )
      });

      // you will get an error if you pass a key
      // which doesn't exist on the User interface.
      this._formService.registerFormControl<User>(
        'history',
        this.form
      )
    }

    protected form: SimpleFormGroup<User['history']>;
  }

  // user-roles.component.ts
    export class UserRolesComponent {
    constructor(private _formService: NgFormService<User>) {
      this.form = new FormArray<SimpleFormGroup<Role>>([]);

      // you will get an error if you pass a key
      // which doesn't exist on the User interface.
      this._formService.registerFormControl<User>(
        'roles',
        this.form
      )
    }

    protected form: FormArray<SimpleFormGroup<Role>>;
  }

Simpler usage.

Angular 14 also introduced constructor time usage of the inject() function. This library also provides an alternative to constructor injecting of the NgFormService by using injectable functions. The above example becomes the following:

// user.ts
interface User {
  name: string;
  age: number;
  history: {
    hiredDate: Date;
    position: string;
  };
  roles: Role[];
  jobTitles: string[];
}

interface Role {
  name: string;
}


/// user-form-root.component.ts
  import {
    getRootFormGroupValueChange,
    getRootFormGroup,
    registerFormControl
  } from '@browsepedia/ng-forms';

  @Component({
    selector: 'app-user-form-root```,
    providers: [NgFormService<User>]
  })
  export class UserFormRootComponent {
    // will hold the entire aggregated form from all the subcomponents where you registered slices of the form.
    protected form = getRootFormGroup<User>();
    private _valueChange$ = getRootFormGroupValueChange<User>()
  }

  // user-history.component.ts
    export class UserHistoryComponent {
    protected form: SimpleFormGroup<User['history']> =
      registerFormControl(
        new SimpleFormGroup<User['history']>({
        hiredDate: FormControl<Date>(
          new Date(),
          { nonNullable: true }
        ),
        position: FormControl<string>('', { nonNullable: true })
      })
    );
  }

  // user-roles.component.ts
    export class UserRolesComponent {
    protected form: FormArray<SimpleFormGroup<Role>> =
      registerFormControl(
      new FormArray<SimpleFormGroup<Role>>([])
    );
  }