Skip to content

Commit cb171eb

Browse files
committed
docs: update user guide for new ngrx 8 functions
1 parent da2f1bc commit cb171eb

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

docs/user-guide/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ export function appReducer(state = initialState, action: Action): AppState {
7878
}
7979
```
8080

81+
If you are using ngrx version 8 or above you can alternatively use `onNgrxForms` with `createReducer`:
82+
83+
```ts
84+
import { createReducer } from '@ngrx/store';
85+
import { onNgrxForms } from 'ngrx-forms';
86+
87+
export const appReducer = createReducer(
88+
initialState,
89+
onNgrxForms(),
90+
// your other reducers...
91+
);
92+
```
93+
8194
Expose the form state inside your component:
8295

8396
```typescript

docs/user-guide/updating-the-state.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
All form states are internally updated by **ngrx-forms** through dispatching actions from the directives. While this is of course also possible for you there exist a set of update functions that can be used to update form states. This is mainly useful to change the state as a result of a different action in your reducer. Note that **ngrx-forms** is coded in such a way that no state references will change if nothing inside the state changes. It is therefore perfectly safe to repeatedly call any of the functions below and the state will be updated exactly once or not at all if nothing changed. Each function can be imported from `'ngrx-forms'` (e.g. `import { setValue } from 'ngrx-forms';`).
22

3+
Usually you will update your form states in your reducers. The following example shows how this can be done.
4+
5+
```ts
6+
import { Action } from '@ngrx/store';
7+
import { createFormGroupState, updateGroup, validate } from 'ngrx-forms';
8+
9+
export interface LoginFormValue {
10+
username: string;
11+
password: string;
12+
stayLoggedIn: boolean;
13+
}
14+
15+
export const initialLoginFormValue: LoginFormValue = {
16+
username: '',
17+
password: '',
18+
stayLoggedIn: false,
19+
};
20+
21+
// this function updates a form group state based on the passed
22+
// update functions (see the sections below for more details on
23+
// all available update functions as well as on updating groups)
24+
export const validateLoginForm = updateGroup<LoginFormValue>({
25+
username: validate(value => !value ? { missing: true } : {}),
26+
// other updates...
27+
});
28+
29+
const initialState = {
30+
loginForm: createFormGroupState('loginForm', initialLoginFormValue),
31+
// your other properties...
32+
};
33+
34+
export function reducer(state = initialState, action: Action) {
35+
const loginForm = validateLoginForm(formGroupReducer(state.loginForm, action));
36+
if (loginForm !== state.loginForm) {
37+
state = { ...state, loginForm };
38+
}
39+
40+
switch (action.type) {
41+
case 'some action type':
42+
// modify state
43+
return state;
44+
45+
default: {
46+
return state;
47+
}
48+
}
49+
}
50+
```
51+
52+
If you are using ngrx version 8 or above you can alternatively use `onNgrxForms` and `wrapReducerWithFormStateUpdate` with `createReducer`:
53+
54+
```ts
55+
import { createReducer } from '@ngrx/store';
56+
import { onNgrxForms, wrapReducerWithFormStateUpdate } from 'ngrx-forms';
57+
58+
const rawReducer = createReducer(
59+
initialState,
60+
onNgrxForms(),
61+
// your other reducers...
62+
);
63+
64+
// wrapReducerWithFormStateUpdate calls the update function
65+
// after the given reducer; you can wrap this reducer again
66+
// if you have multiple forms in your state
67+
export const reducer = wrapReducerWithFormStateUpdate(
68+
rawReducer,
69+
// point to the form state to update
70+
s => s.loginForm,
71+
// this function is always called after the reducer
72+
validateLoginForm,
73+
);
74+
```
75+
376
Below you will find a complete list of all update functions provided by **ngrx-forms**. Each section also shows how to use actions directly instead of the update functions (the examples directly call the `formStateReducer` but you can of course dispatch these actions from anywhere in your code). To view all available actions the best place is [the code itself](https://github.com/MrWolfZ/ngrx-forms/blob/master/src/actions.ts).
477

578
#### Setting the value
@@ -284,7 +357,7 @@ export interface MyFormValue {
284357
someNumbers: number[];
285358
}
286359

287-
const updateMyFormGroup = updateGroup<MyFormValue>({
360+
const validateMyForm = updateGroup<MyFormValue>({
288361
someTextInput: validate(required),
289362
nested: updateGroup<MyFormValue['nested']>({
290363
someNumber: validate(required, greaterThanOrEqualTo(2)),
@@ -293,7 +366,7 @@ const updateMyFormGroup = updateGroup<MyFormValue>({
293366
});
294367
```
295368

296-
The `updateMyFormGroup` function has a signature of `FormGroupState<MyFormValue> -> FormGroupState<MyFormValue>`. It takes a state, runs all validations, updates the errors, and returns the resulting state.
369+
The `validateMyForm` function has a signature of `FormGroupState<MyFormValue> -> FormGroupState<MyFormValue>`. It takes a state, runs all validations, updates the errors, and returns the resulting state.
297370

298371
In addition, the `updateGroup` function allows specifying as many update function objects as you want and applies all of them after another. This is useful if you have dependencies between your update functions where one function's result may affect the result of another function. The following (contrived) example shows how to set the value of `someNumber` based on the `errors` of `someTextInput`.
299372

docs/user-guide/validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface MyFormValue {
3434
nested: NestedValue;
3535
}
3636

37-
const updateMyFormGroup = updateGroup<MyFormValue>({
37+
const validateMyForm = updateGroup<MyFormValue>({
3838
someTextInput: validate(required),
3939
nested: updateGroup<NestedValue>({
4040
someNumber: validate(required, greaterThanOrEqualTo(2), lessThan(10)),

0 commit comments

Comments
 (0)