GithubHelp home page GithubHelp logo

jaregu / ngxs-forms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrwolfz/ngrx-forms

2.0 2.0 1.0 16.98 MB

Proper integration of forms in Angular applications using ngxs (forked from ngrx-forms)

License: Other

TypeScript 99.70% JavaScript 0.30%

ngxs-forms's Introduction

ngxs-forms

npm version Build Status codecov license

This is fork of the best forms implementation for redux style stores. Original ngrx-forms uses @ngrx/store, this fork is updated to be used with @ngxs/store store.

Installation

npm install ngxs-forms --save

# or if you are using yarn
yarn add ngxs-forms

This library has a peer dependency on @angular/core, @angular/common, @angular/forms, and @ngxs/store, so make sure appropriate versions of those packages are installed.

Quick start

Add NgxsFormsModule.forRoot() to root app module

import { NgxsFormsModule } from 'ngxs-forms';
// ...

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    // ...
    NgxsFormsModule.forRoot()
  ],
  // ...
  bootstrap: [AppComponent]
})
export class AppModule { }

Define NGXS state

import { FormGroupState, createFormGroupState } from 'ngxs-forms';

// ...

interface FormModel {
  id: number;
  name: string;
  
  // ...
}

interface StateModel {
  loading: boolean;
  specialForm: FormGroupState<FormModel>;
  
  //...
}

const initialModelState: FormModel = {
  id: null,
  name: '',
  
  // ...
}

const SOME_FORM_ID = '[Some scope] Special form';
const initialSpecialFormState = createFormGroupState<FormModel>(SOME_FORM_ID, initialModelState);

const initialState: StateModel = {
  loading: false,
  specialForm: initialSpecialFormState
};

@State<StateModel>({
  name: 'someSpecialState',
  defaults: initialState
})
@Injectable()
export class SpecialState {
  
  // ...
}

Add NgxsFormsModule in feature module and use state in component (no special reducers needed)

// ...
@NgModule({
  declarations: [
    SomeComponent
  ],
  imports: [
    // ...
    NgxsFormsModule
  ],
})
export class SomeFeatureModule { }

// ...
@Component({
  selector: 'app-special',
  template: `
  <form novalidate [ngrxFormState]="(specialState$ | async).specialForm">
    <input type="text" [ngrxFormControlState]="(specialState$ | async).specialForm.controls.name"></input>
  </form>`,
})
export class SomeComponent implements OnInit {

  @Select(SpecialState) specialState$: Observable<StateModel>;

Documentation

Learn more about ngrx-forms at github or official ngrx-forms documentation or visit the example application.

Learn more about @ngxs/store store at github or visit homepage.

License

Everything in this repository is licensed under the MIT License unless otherwise specified.

Original work Copyright (c) 2017-present Jonathan Ziller

Modified work Copyright 2018-present Mārcis Meijers

ngxs-forms's People

Contributors

mrwolfz avatar mme-private avatar lucax88x avatar ymekesser avatar solnat avatar tbroadley avatar scott-wyatt avatar icepeng avatar dzonatan avatar wbhob avatar mr-eluzive avatar tashoecraft avatar sloff avatar philjones88 avatar tomvandezande avatar dependabot[bot] avatar mucaho avatar r-khr avatar

Stargazers

 avatar Vladislav Sarunov avatar

Watchers

James Cloos avatar  avatar

Forkers

misty000

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.