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

ngx-form-resolver

v1.0.4

Published

A simple library for mapping objects to Angular FormGroups with reusable resolver functions.

Downloads

4

Readme


Links

Table of Contents

Installation

npm install ngx-form-resolver --save

Quick Start

1.) Import the FormResolverModule:

@NgModule({
    ...
    imports: 
        ...
        FormResolverModule.forRoot(),
    ]
    ...
})

2.) Inject the FormResolverBuilder (and FormBuilder):

@Component({...})
export class CarFormComponent {
    constructor(private FormResolverBuilder: FormResolverBuilder, private formBuilder: FormBuilder ...) {...}
}

3.) A FormResolver is designed to map a defined class/object to a certain FormGroup. A basic Car class and FormGroup will be used in this example.

export class Car {
    make: string = '';
    model: string = '';
    year: number = null;
    packages = {
        sport: false
    }
}
this.carFormGroup = formBuilder.group({
    'make': [''],
    'model': [''],
    'year': [null],
    'sportPackageCheckbox': [false]
});

4.) Build a FormResolver<Car> with the class and FormGroup:

this.carFormResolver = this.formResolverBuilder
    .setFactory(() => new Car())
    .setFormGroup(this.carFormGroup)
    .setResolvers({
        'make': FormControlResolvers.simple,
        'model': FormControlResolvers.simple,
        'year': FormControlResolvers.simpleNumber,
        'sportPackageCheckbox': FormControlResolvers.nested('packages.sport')
    })
    .build();

5.) Subscribe to changes via observable:

this.carFormResolver.getFormState()
    .pipe(...)
    .subscribe((car: Car) => {...})

6.) Set the value of the form state on demand:

this.carFormResolver.setFormState({
        make: 'Ford';
        model: 'Escape';
        year: 2018;
    } as Car);

7.) Finally, complete OnDestroy:

ngOnDestroy() {
    this.carFormResolver.complete();
}

API

FormResolverBuilder

Service used to build a FormResolver.

  • setFactory(factory: Function)
  • setFormGroup(formGroup: FormGroup)
  • setResolvers(resolverConfig: { [key: string]: FormControlResolver<any> })
  • build()

FormResolver

| Method | Description | |--------|-------------| | setFormState(object: T) | pass an object to resolve to the form state | | getFormState(): Observable<T> | get an observable that returns the resolved form state as an object whenever changes occur | | getFormStateSnapshot(): T | get the current resolved form state object | set a new FormGroup to watch | | updateFactory(factory: Function) | set a new factory function | | updateResolverConfig(resolverConfig: { [key: string]: FormControlResolver<any> }) | pass in a completely new config of formControlNames and resolvers | | addControl(controlName: string, formControlResolver: FormControlResolver<any>) | adds a control to be watched and resolved | | updateControl(controlName: string, formControlResolver: FormControlResolver<any>) | alias to addControl | | removeControl(controlName: string) | removes a control from being watched and resolved | | complete() | completes all form resolver subjects |

FormControlResolver

FormControlResolver type definition:

class FormControlResolver<T> {
    inputResolver: InputResolver<T>;
    outputResolver: OutputResolver<T>;
    ...
}
type InputResolver<T> = (inputObject: T, controlName?: string) => any;
type OutputResolver<T> = (outputObject: T, formValues: any, controlName?: string) => T;

Included default FormControlResolvers:

| Resolver | Description | |----------|-------------| | FormControlResolvers.simple | Used for directly mapping the value of a FormControl to a prop of the same name | | FormControlResolvers.simpleNumber | The simple formControlResolver except the input & output values are cast as a number | | FormControlResolvers.simpleString | The simple formControlResolver except the input & output values are cast as a string | | FormControlResolvers.nested(propertyPath: string \| string[]) | Used for mapping the value of a FormControl to a nested prop using a . notated string or array of prop strings | | FormControlResolvers.nestedNumber(propertyPath: string \| string[]) | The nested formControlResolver except the input & output values are cast as a number | | FormControlResolvers.nestedString(propertyPath: string \| string[]) | The nested formControlResolver except the input & output values are cast as a string |

Form Resolver Concept

A FormResolver<T> maps values to a FormGroup from an instance of T and to an instance of T from a FormGroup. A FormResolver is a configuration of FormControlResolvers that define and abstract away control value mapping. Once configured, a FormResolver serves as an adapter between your class/object and FormGroup. A FormResolver is built from the three items below using the FormResolverBuilder service:

  • Factory: The base object a FormResolver will use to map form state to. Ultimately, the result of this factory is the base object a FormGroup's state will be resolved into.

  • FormGroup: The FormGroup a FormResolver will be mapping to and from.

  • ResolverConfig: A config used to map specific FormControls to FormControlResolvers using formControlNames (see below).

Each of these items can be changed at any time using the provided API methods. After building a FormResolver the state of the form can be accessed directly as a snapshot or over time as an observable. The state can also be set using an object or instance of the defined class.

TIPS:

  • When passed a null value, a FormResolver will reset the target mapped control
  • FormResolvers work with nested FormGroups - both the parent group or any of the child groups.
  • Setting properties that are not in your form in your factory's return object will ensure the output form state contains those values (useful when you need keep the values of props not in your form)

Form Control Resolver Concept

Input/Output Resolvers

A FormControlResolver is a reusable, easily testable pair of functions used to map one form control to and from the target object/class. An InputResolver function maps an object to a form and an OutputResolver function maps a form control value to an object.

An InputResolver is passed the object being mapped and the name of the control the resolver is trying to map it to. The return value of this function is the value that the target FormControl will be set to.

An OutputResolver is passed the object being mapped, the full list of values in the formGroup, and the name of the control that needs to be mapped to the object.

Default Form Control Resolvers

There are a few default simple FormControlResolvers that are exported under FormControlResolvers (see API). These FormControlResolvers let you easily map form control values directly to object properties of the same name.

TIPS:

  • It can sometimes be useful to create custom a FormControlResolver factory by wrapping it in a function that returns a FormControlResolver. See the example below:
const exampleControlResolverFactory = (value: any) => new FormControlResolver(
    (inputObject: any, controlName: string) => { ... },
    (outputObject: any, formValues: any, controlName: string) => { ... }
)