All Projects → amcdnl → ngrx-form

amcdnl / ngrx-form

Licence: MIT license
Reactive Forms bindings for NGRX and Angular

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ngrx-form

fullstack-graphql-angular
Simple Fullstack GraphQL Application with Angular CLI + Redux. API built with Typescript + Express + GraphQL + Sequelize (supports MySQL, Postgres, Sqlite and MSSQL). WebApp built with Angular CLI + Redux + Async Middleware to access the API.
Stars: ✭ 67 (+45.65%)
Mutual labels:  ngrx, angular2
ng-math
Math Flashcard Progressive Web App built with Angular 2
Stars: ✭ 17 (-63.04%)
Mutual labels:  ngrx, angular2
angular2-instagram
🔥Instagram like photo filter playground built with Angular2 (Web | Desktop)
Stars: ✭ 91 (+97.83%)
Mutual labels:  ngrx, angular2
ngx-redux-ui-management-recipes
Recipes for managing the UI layout of an application using Redux in Angular
Stars: ✭ 39 (-15.22%)
Mutual labels:  ngrx, angular2
Angular Redux Ngrx Examples
Sample projects with Angular (4.x) + Angular CLI + ngrx (Redux) + Firebase
Stars: ✭ 73 (+58.7%)
Mutual labels:  ngrx, angular2
angular2-webpack-advance-starter
An advanced Angular2 Webpack Starter project with support for ngrx/store, ngrx/effects, ng2-translate, angulartics2, lodash, NativeScript (*native* mobile), Electron (Mac, Windows and Linux desktop) and more.
Stars: ✭ 49 (+6.52%)
Mutual labels:  ngrx, angular2
Aspnetcore Angular Universal
ASP.NET Core & Angular Universal advanced starter - PWA w/ server-side rendering for SEO, Bootstrap, i18n internationalization, TypeScript, unit testing, WebAPI REST setup, SignalR, Swagger docs, and more! By @TrilonIO
Stars: ✭ 1,455 (+3063.04%)
Mutual labels:  ngrx, angular2
ionic2.0-angularfire
this a basic application for Ionic 2.0.rc5 with AngularFire2 with ngrx/store & ngrx/effects to manage state
Stars: ✭ 71 (+54.35%)
Mutual labels:  ngrx, angular2
spring-websocket-angular6
Example for using Spring Websocket and Angular with Stomp Messaging
Stars: ✭ 18 (-60.87%)
Mutual labels:  angular2
todo-app-ngrx
TodoMV* - Angular + Redux (ngrx)
Stars: ✭ 42 (-8.7%)
Mutual labels:  ngrx
framework7.angular2
Framework7 angular 2 integration
Stars: ✭ 14 (-69.57%)
Mutual labels:  angular2
cloud-cardboard-viewer
Build a Node.js & Angular 2 Web App using Google Cloud Platform
Stars: ✭ 23 (-50%)
Mutual labels:  angular2
ngrx-slice
Moved to https://github.com/nartc/nartc-workspace
Stars: ✭ 50 (+8.7%)
Mutual labels:  ngrx
ng2-tooltip-directive
The tooltip is a pop-up tip that appears when you hover over an item or click on it.
Stars: ✭ 101 (+119.57%)
Mutual labels:  angular2
odata-v4-ng
OData service for Angular
Stars: ✭ 27 (-41.3%)
Mutual labels:  angular2
angular-for-beginners-starter
A beginner friendly playground for Getting Started with Angular, by the Angular University
Stars: ✭ 52 (+13.04%)
Mutual labels:  angular2
angular2-yandex-maps
Angular 2 components Yandex Maps.
Stars: ✭ 26 (-43.48%)
Mutual labels:  angular2
BlazorHealthApp
Example application ported from Angular 2 to Blazor
Stars: ✭ 37 (-19.57%)
Mutual labels:  angular2
meditation-plus-angular
DEVELOPMENT MOVED TO
Stars: ✭ 15 (-67.39%)
Mutual labels:  ngrx
jw-bootstrap-switch-ng2
Bootstrap Switch for Angular 2+
Stars: ✭ 45 (-2.17%)
Mutual labels:  angular2

ngrx-form npm version

Reactive Form bindings for NGRX.

See changelog for latest changes.

Whats this do?

Often when building Reactive Forms in Angular, you need to bind values from the store to form and vice versus. The values from the store are observable and the reactive form accepts raw objects, as a result we end up monkey patching this back and forth. For more info on this, checkout Reactive Forms with NGRX blog post.

Binding the values is not the only thing we commonly do, its not un-typical to translate form dirty status or form errors. Typical workflows might include reading the errors from the form to show in various decoupled components or for use in our effects or using the form dirty status to prevent users from leaving a page without saving but without binding a variable we have no way to reset the status of the form after a successful save from an effect.

In addition to these issues we encounter, there are workflows where you want to fill out a form and leave and then come back and resume your current status. This is an excellent use case for stores and we can conquer that case with this utility.

In a nutshell, this tool helps bridge the gaps between ngrx-store and reactive forms with a set of utilities and bindings to keep your forms and state in sync.

Usage

To get started, install the package:

npm i ngrx-form -s

Configuration

In the root module of your application, import NgrxFormModule and include it in the imports. Also in this file, lets import the form meta-reducer and bind it to our store.

import { form, NgrxFormModule } from 'ngrx-form';

@NgModule({
    StoreModule.forRoot(reducers, {
        metaReducers: [form]
    }),
    NgrxFormModule
})
export class AppModule {}

Reducer Setup

Define your state interface by extending the FormState interface:

import { FormState } from 'ngrx-form';

export interface PizzaForm extends FormState<Pizza> {}

the FormState interface is a interface you will map your form<=>model to. It looks like:

export interface FormState<T> {
  /**
   * The model to bind to the form
   */
  model: T;

  /**
   * Id of the model. Seperate because
   * this is typically not bound in
   * a form
   */
  modelId?: string;

  /**
   * Key value pair of errors
   */
  errors?: { [k: string]: string };

  /**
   * Whether the form is dirty or not.
   */
  dirty?: boolean;

  /**
   * The status of the form
   */
  status?: string;
}

then in the reducer populate the model object, this will vary based on your implementation but it should look something like this:

const pizzaDefaultState = { model: undefined };
export function pizzaReducer(state = pizzaDefaultState, action) {
    switch(action.type) {
        switch 'LOAD_PIZZA':
            return { ...state, model: action.payload };

        return state;
    }
}

Form Setup

In your component, you would implement the a reactive form and decorate the form with the ngrxForm directive with the path of your state object. We are passing the string path to ngrxForm. Our directive uses this path to connect itself to the store and setup bindings.

@Component({
    template: `
        <form [formGroup]="pizzaForm" novalidate (ngSubmit)="onSubmit()" ngrxForm="pizza">
            <input type="text" formControlName="toppings" />
            <button type="submit">Order</button>
        </form>
    `
})
export class PizzaComponent {
    pizzaForm = this.formBuilder.group({
        toppings: ['']
    });
}

Now anytime your form updates, your state will also reflect the new state.

The directive also has 2 inputs you can utilize as well:

  • ngrxFormDebounce: number - Debounce the value changes to the form. Default value: 100.
  • ngrxFormClearOnDestroy: boolean - Clear the state on destroy of the form.

Actions

In addition to it automatically keeping track of the form, you can also manually dispatch actions for things like resetting the form state. For example:

this.store.dispatch(
    new UpdateFormDirty({
        dirty: false, path: 'pizza'
    })
);

The actions that it comes with out of the box are:

  • UpdateFormStatus({ status, path }) - Update the form status
  • UpdateFormValue({ value, path }) - Update the form value
  • UpdateFormDirty({ dirty, path }) - Update the form dirty status
  • SetFormDisabled(path) - Set the form to disabled
  • SetFormEnabled(path) - Set the form to enabled
  • SetFormDirty(path) - Set the form to dirty (shortcut for UpdateFormDirty)
  • SetFormPristine(path) - Set the form to prestine (shortcut for UpdateFormDirty)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].