Skip to content

Commit 55a3849

Browse files
committed
docs(readme): update
1 parent 924f337 commit 55a3849

File tree

2 files changed

+130
-36
lines changed

2 files changed

+130
-36
lines changed

README.md

+130-36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
<img width="50%" height="50%" src="./logo.png">
55
</p>
66

7+
> The Foundation for Proper Form Management
8+
9+
## 🔮 Features
10+
11+
✅ Auto persists the form's state upon user navigation.<br>
12+
✅ Provides an API to reactively querying any form, from anywhere. <br>
13+
✅ Persist the form's state to local storage.
14+
15+
<br>
16+
717
[![Build Status](https://img.shields.io/travis/datorama/akita.svg?style=flat-square)](https://travis-ci.org/ngneat/transloco)
818
[![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]()
919
[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)]()
@@ -63,52 +73,138 @@ export class OnboardingComponent {
6373
}
6474

6575
ngOnDestroy() {
66-
this.formsManager.unsubscribe();
76+
this.formsManager.unsubscribe('onboarding');
6777
}
6878
}
6979
```
7080

7181
As you can see, we’re still working with the existing API in order to create a form in Angular. We’re injecting the `NgFormsManager` and calling the `upsert` method, giving it the form name and an `AbstractForm`.
72-
From that point on, `NgFormsManager` will track the form value changes and update the store accordingly.
82+
From that point on, `NgFormsManager` will track the `form` value changes, and update the store accordingly.
7383

7484
With this setup, you’ll have an extensive API to query the store and update the form from anywhere in your application:
7585

86+
## API
87+
88+
- `selectValid()` - Whether the control is valid
89+
7690
```ts
91+
const isFormValid$ = formsManager.selectValid('onboarding');
92+
const isNameValid$ = formsManager.selectValid('onboarding', 'name');
93+
```
7794

78-
@Component({
79-
selector: 'my-comp',
80-
templateUrl: './app.component.html',
81-
})
82-
export class MyCompComponent {
83-
constructor(private formsManager: NgFormsManager) {}
95+
- `selectDirty()` - Whether the control is dirty
8496

85-
ngOnInit() {
86-
this.formsManager.selectForm(formName).subscribe(form => {});
87-
this.formsManager.getForm(formName);
88-
this.formsManager.hasForm(formName);
97+
```ts
98+
const isFormDirty$ = formsManager.selectDirty('onboarding');
99+
const isNameDirty$ = formsManager.selectDirty('onboarding', 'name');
100+
```
89101

90-
this.formsManager.selectControl(formName, 'email').subscribe(emailControl => {});
91-
this.formsManager.getControl(formName, path?);
102+
- `selectDisabled()` - Whether the control is disabled
92103

93-
this.formsManager.selectErrors(formName, path?).subscribe(errors => {});
94-
this.formsManager.selectValue(formName, path?).subscribe(value => {});
95-
this.formsManager.selectDisabled(formName, path?).subscribe(disabled => {});
96-
this.formsManager.selectDirty(formName, path?).subscribe(dirty => {});
97-
this.formsManager.selectValid(formName, path?).subscribe(valid => {});
104+
```ts
105+
const isFormDisabled$ = formsManager.selectDisabled('onboarding');
106+
const isNameDisabled$ = formsManager.selectDisabled('onboarding', 'name');
107+
```
98108

99-
this.formsManager.patchValue(formName, value, options);
100-
this.formsManager.setValue(formName, value, options);
101-
}
109+
- `selectValue()` - Observe the control's value
102110

103-
ngOnDestroy() {
104-
formsManager.unsubscribe(formName?);
105-
}
111+
```ts
112+
const value$ = formsManager.selectValue('onboarding');
113+
const nameValue$ = formsManager.selectValue('onboarding', 'name');
114+
```
115+
116+
- `selectErrors()` - Observe the control's errors
117+
118+
```ts
119+
const errors$ = formsManager.selectErrors('onboarding');
120+
const nameErros$ = formsManager.selectErrors('onboarding', 'name');
121+
```
122+
123+
- `selectControl()` - Observe the control state
124+
125+
```ts
126+
const control$ = formsManager.selectControl('onboarding');
127+
const nameControl$ = formsManager.selectControl('onboarding', 'name');
128+
```
129+
130+
- `getControl()` - Get the control state
131+
132+
```ts
133+
const control = formsManager.getControl('onboarding');
134+
const nameControl = formsManager.getControl('onboarding', 'name');
135+
```
136+
137+
It returns the following state:
138+
139+
```ts
140+
{
141+
value: any,
142+
rawValue: object,
143+
errors: object,
144+
valid: boolean,
145+
dirty: boolean,
146+
invalid: boolean,
147+
disabled: boolean,
148+
touched: boolean,
149+
pristine: boolean,
150+
pending: boolean,
106151
}
107152
```
108153

154+
- `selectForm()` - Observe the form state
155+
156+
```ts
157+
const form$ = formsManager.selectForm('onboarding');
158+
```
159+
160+
- `getForm()` - Get the form state
161+
162+
```ts
163+
const form = formsManager.getForm('onboarding');
164+
```
165+
166+
- `hasForm()` - Whether the form exists
167+
168+
```ts
169+
const hasForm = formsManager.hasForm('onboarding');
170+
```
171+
172+
- `patchValue()` - A facade to the original `patchValue` method
173+
174+
```ts
175+
formsManager.patchValue('onboarding', value, options);
176+
```
177+
178+
- `setValue()` - A facade to the original `setValue` method
179+
180+
```ts
181+
formsManager.setValue('onboarding', value, options);
182+
```
183+
184+
- `unsubscribe()` - Unsubscribe from the form's `valueChanges` observable (always call it on `ngOnDestroy`)
185+
186+
```ts
187+
formsManager.unsubscribe('onboarding');
188+
formsManager.unsubscribe();
189+
```
190+
191+
- `clear()` - Deletes the form from the store
192+
193+
```ts
194+
formsManager.clear('onboarding');
195+
formsManager.clear();
196+
```
197+
198+
- `destroy()` - Destroy the form (Internally calls `clear` and `unsubscribe`)
199+
200+
```ts
201+
formsManager.destroy('onboarding');
202+
formsManager.destroy();
203+
```
204+
109205
## Persist to Local Storage
110206

111-
When passing the form to the `upsert` method, pass the `persistState` flag:
207+
In the `upsert` method, pass the `persistState` flag:
112208

113209
```ts
114210
formsManager.upsert(formName, abstractContorl, {
@@ -142,8 +238,8 @@ export class HomeComponent{
142238
});
143239

144240
/*
145-
* Check the `minPrice` value in the settings form
146-
* and update the price's control validators
241+
* Observe the `minPrice` value in the `settings` form
242+
* and update the price `control` validators
147243
*/
148244
this.formsManager.selectValue<number>('settings', 'minPrice')
149245
.subscribe(minPrice => setValidators(this.form.get('price'), Validators.min(minPrice));
@@ -153,7 +249,7 @@ export class HomeComponent{
153249
154250
## Using FormArray Controls
155251
156-
When working with a `FormArray`, it's required to pass a factory function that tells how to create the controls inside the array. For example:
252+
When working with a `FormArray`, it's required to pass a `factory` function that instruct how to create the `controls` inside the `FormArray`. For example:
157253
158254
```ts
159255
import { NgFormsManager } from '@ngneat/forms-manager';
@@ -165,19 +261,17 @@ export class HomeComponent {
165261
constructor(private formsManager: NgFormsManager<FormsState>) {}
166262

167263
ngOnInit() {
168-
const createControl = value => new FormControl(value);
169-
170-
this.skills = new FormArray([createControl('JS')]);
264+
this.skills = new FormArray([]);
171265

172-
/** Or inside form group */
266+
/** Or inside a FormGroup */
173267
this.config = new FormGroup({
174-
skills: new FormArray([createControl('JS')]),
268+
skills: new FormArray([]),
175269
});
176270

177271
this.formsManager
178-
.upsert('skills', this.skills, { arrControlFactory: createControl })
272+
.upsert('skills', this.skills, { arrControlFactory: value => new FormControl(value) })
179273
.upsert('config', this.config, {
180-
arrControlFactory: { skills: createControl },
274+
arrControlFactory: { skills: value => new FormControl(value) },
181275
});
182276
}
183277

logo.png

27.4 KB
Loading

0 commit comments

Comments
 (0)