Skip to content

Commit d53f02a

Browse files
committed
chore: restructured README and added getting started section
1 parent a609ed4 commit d53f02a

File tree

1 file changed

+168
-20
lines changed

1 file changed

+168
-20
lines changed

README.md

Lines changed: 168 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,185 @@
11
# ngrx-forms
22

3+
[![Build Status](https://travis-ci.org/MrWolfZ/ngrx-forms.svg?branch=master)](https://travis-ci.org/MrWolfZ/ngrx-forms)
4+
35
Proper integration of forms in Angular 4 applications using ngrx.
46

57
Disclaimer: This library is not affiliated with the official ngrx library. I have created it mainly for my own use in one of my projects, but I thought others could profit as well.
68

7-
Please note that this library is still very much work-in-progress and therefore there are many changes still being done. The library is also not yet available via npm. If you want to beta test it you can download the `ngrx-forms-[version].tgz` file from the example app and use that to install the library to your project via:
9+
There is an [example app](https://ngrx-forms-example-app.herokuapp.com/) that showcases most of the functionality of this library.
10+
11+
### Content
12+
[1 Installation](#1)
13+
[2 Design Principles](#2)
14+
[3 User Guide](#3)
15+
[4 Open Points](#4)
16+
[5 Contributing](#5)
817

18+
## <a name="1"></a>1 Installation
919
```Shell
10-
npm install [path]ngrx-forms-[version].tgz --save
20+
npm install ngrx-forms --save
21+
```
22+
23+
This library depends on versions `^4.0.0` of `@angular/core`, `@angular/forms`, and `@ngrx/store`, and version `^5.0.0` of `rxjs`.
24+
25+
## <a name="2"></a>2 Design Principles
26+
This library is written to be as functional and as pure as possible. Most of the heavy lifting is done in pure reducer functions while the directives are only a thin layer to connect the form states to the DOM.
27+
28+
This library also tries to be as independent as possible from other libraries/modules. While there is of course a dependency on ngrx the touching points are small and it should be possible to adapt this library to any other redux library without too much effort. There is also a peer dependency on `@angular/forms` from which we re-use the `ControlValueAccessor` concept to allow easier integration with other libraries that provide custom form controls.
29+
30+
Conceptually this library borrows heavily from `@angular/forms`, specifically the concepts of form controls and form groups. Groups are simply a collection of named form controls. The state of a group is determined almost fully by its child controls (with the exception of errors which a group can have by itself).
31+
32+
## <a name="3"></a>3 User Guide
33+
34+
### Getting Started
35+
36+
Import the module:
37+
38+
```typescript
39+
import { NgrxFormsModule } from 'ngrx-forms';
40+
41+
@NgModule({
42+
declarations: [
43+
AppComponent,
44+
...,
45+
],
46+
imports: [
47+
...,
48+
NgrxFormsModule,
49+
StoreModule.forRoot(reducers),
50+
...,
51+
],
52+
providers: [],
53+
bootstrap: [AppComponent]
54+
})
55+
export class AppModule { }
1156
```
1257

13-
You can see the [example app](https://ngrx-forms-example-app.herokuapp.com/) online.
58+
Add a group state somewhere in your state tree via `createFormGroupState` and call the `formGroupReducer` inside your reducer:
59+
60+
```typescript
61+
import { Action } from '@ngrx/store';
62+
import { FormGroupState, createFormGroupState, formGroupReducer } from 'ngrx-forms';
63+
64+
export interface MyFormValue {
65+
someTextInput: string;
66+
someCheckbox: boolean;
67+
nested: {
68+
someNumber: number;
69+
};
70+
}
71+
72+
const FORM_ID = 'some globally unique string';
73+
74+
const initialFormState = createFormGroupState<MyFormValue>(FORM_ID, {
75+
someTextInput: '',
76+
someCheckbox: false,
77+
nested: {
78+
someNumber: 0,
79+
},
80+
});
81+
82+
export interface AppState {
83+
someOtherField: string;
84+
myForm: FormGroupState<MyFormValue>;
85+
}
86+
87+
const initialState: AppState = {
88+
someOtherField: '',
89+
myForm: initialFormState,
90+
};
91+
92+
export function appReducer(state = initialState, action: Action): AppState {
93+
const myForm = formGroupReducer(state.myForm, action);
94+
if (myForm !== state.myForm) {
95+
state = { ...state, myForm };
96+
}
97+
98+
switch (action.type) {
99+
case 'some action type':
100+
// modify state
101+
return state;
102+
103+
default: {
104+
return state;
105+
}
106+
}
107+
}
108+
```
109+
110+
Expose the form state inside your component:
111+
112+
```typescript
113+
import { Component } from '@angular/core';
114+
import { Store } from '@ngrx/store';
115+
import { FormGroupState } from 'ngrx-forms';
116+
import { Observable } from 'rxjs/Observable';
117+
118+
import { MyFormValue } from './reducer';
119+
120+
@Component({
121+
selector: 'my-component',
122+
templateUrl: './my-component.html',
123+
})
124+
export class MyComponent {
125+
formState$: Observable<FormGroupState<MyFormValue>>;
126+
127+
constructor(private store: Store<AppState>) {
128+
this.formState$ = store.select(s => s.myForm);
129+
}
130+
}
131+
```
132+
133+
Set the control states in your template:
134+
```html
135+
<form novalidate [ngrxFormState]="(formState$ | async)">
136+
<input type="text"
137+
[ngrxFormControlState]="(formState$ | async).controls.someTextInput">
138+
139+
<input type="checkbox"
140+
[ngrxFormControlState]="(formState$ | async).controls.someCheckbox">
141+
142+
<input type="number"
143+
[ngrxFormControlState]="(formState$ | async).controls.nested.controls.someNumber">
144+
</form>
145+
```
146+
147+
### Form Controls
148+
149+
- explain whole state concept
150+
- mention that state is sync'ed immediately
151+
152+
### Form Groups
153+
154+
- explain exactly how each property of a group is computed
155+
- form directive for submission status tracking (mention preventDefault())
156+
157+
### Updating the State
158+
159+
- explain all helper functions
160+
161+
### Custom Controls
14162

15-
Until proper documentation exists I recommend having a look at the example app code to see how the library can be used.
163+
- control value accessors
16164

17-
### Open Points
165+
## <a name="4"></a>4 Open Points
18166

19-
- more ergonomic helper functions to modify form states (e.g. to disable, enable, update values etc.), especially for deeply nested states
20-
- validation infrastructure (i.e. sync and async validators incl. validation status; although both are already achievable via reducer or effects)
21-
- directive cleanup (e.g. making certain status tracking like focus optional, having proper value conversion etc.)
22-
- lots of tests
23-
- proper documentation
167+
* providing option to choose when the view is sync'ed to the state (e.g. `change`, `blur` etc.)
168+
* proper value conversion
169+
* async validation (although already achievable via effects)
170+
* providing some global configuration options
171+
* some tests for directives
172+
* tests for example application
24173

25-
## Contributing
26-
* [1 Testing](#1)
27-
* [2 Building](#2)
28-
* [3 Documentation](#3)
174+
## <a name="5"></a>5 Contributing
29175

30-
## <a name="1"></a>1 Testing
31-
The following command run unit & integration tests that are in the `tests` folder, and unit tests that are in `src` folder:
176+
### Testing
177+
The following command runs all unit tests:
32178
```Shell
33179
npm test
34180
```
35181

36-
## <a name="2"></a>2 Building
182+
### Building and Packaging
37183
The following command:
38184
```Shell
39185
npm run build
@@ -42,21 +188,23 @@ npm run build
42188
- starts _AoT compilation_ using _ngc_ compiler
43189
- creates `dist` folder with all the files of distribution
44190

45-
To test locally the npm package:
191+
To test the npm package locally run:
46192
```Shell
47193
npm run pack-lib
48194
```
49-
Then you can install it in an app to test it:
195+
and install it in an app to test it with:
50196
```Shell
51197
npm install [path]ngrx-forms-[version].tgz
52198
```
53199

54-
## <a name="3"></a>3 Documentation
200+
<!--
201+
### Documentation
55202
To generate the documentation, this library uses [compodoc](https://github.com/compodoc/compodoc):
56203
```Shell
57204
npm run compodoc
58205
npm run compodoc-serve
59206
```
207+
-->
60208

61209
## License
62210
MIT

0 commit comments

Comments
 (0)