Skip to content

Commit 0a15799

Browse files
authored
Merge pull request #16 from manudss/feature/#15-getinitialvalue
2 parents 2ff97e4 + 9a267ee commit 0a15799

File tree

4 files changed

+106
-14
lines changed

4 files changed

+106
-14
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
"code",
5050
"doc"
5151
]
52+
},
53+
{
54+
"login": "manudss",
55+
"name": "Emmanuel De Saint Steban",
56+
"avatar_url": "https://avatars3.githubusercontent.com/u/1046806?v=4",
57+
"profile": "https://github.com/manudss",
58+
"contributions": [
59+
"code",
60+
"doc"
61+
]
5262
}
5363
],
5464
"contributorsPerLine": 7

README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,23 @@ import { NgFormsManager } from '@ngneat/forms-manager';
5151

5252
@Component({
5353
template: `
54-
<form [formGroup]="onboardingForm">
55-
<input formControlName="name">
56-
<input formControlName="age">
57-
<input formControlName="city">
58-
</form>
59-
`
54+
<form [formGroup]="onboardingForm">
55+
<input formControlName="name" />
56+
<input formControlName="age" />
57+
<input formControlName="city" />
58+
</form>
59+
`,
6060
})
6161
export class OnboardingComponent {
62-
6362
onboardingForm: FormGroup;
6463

65-
constructor(
66-
private formsManager: NgFormsManager,
67-
private builder: FormBuilder
68-
) {}
64+
constructor(private formsManager: NgFormsManager, private builder: FormBuilder) {}
6965

7066
ngOnInit() {
7167
this.onboardingForm = this.builder.group({
7268
name: [null, Validators.required],
73-
age: [null, Validators.required],
74-
city: [null, Validators.required]
69+
age: [null, Validators.required],
70+
city: [null, Validators.required],
7571
});
7672

7773
this.formsManager.upsert('onboarding', this.onboardingForm);
@@ -353,6 +349,18 @@ export class SettingsComponent {
353349
}
354350
```
355351
352+
### `setInitialValue(name, value)` - Set the initial form's value
353+
354+
```ts
355+
formsManager.setInitialValue('form', initialValue);
356+
```
357+
358+
### `getInitialValue(name)` - Get the initial value or `undefined` if not exist.
359+
360+
```ts
361+
formsManager.getInitialValue('form');
362+
```
363+
356364
## NgFormsManager Config
357365
358366
You can override the default config by passing the `NG_FORMS_MANAGER_CONFIG` provider:
@@ -392,6 +400,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
392400
<td align="center"><a href="https://github.com/Coly010"><img src="https://avatars2.githubusercontent.com/u/12140467?v=4" width="100px;" alt=""/><br /><sub><b>Colum Ferry</b></sub></a><br /><a href="https://github.com/ngneat/forms-manager/commits?author=Coly010" title="Code">💻</a> <a href="https://github.com/ngneat/forms-manager/commits?author=Coly010" title="Documentation">📖</a></td>
393401
<td align="center"><a href="https://github.com/mehmet-erim"><img src="https://avatars0.githubusercontent.com/u/34455572?v=4" width="100px;" alt=""/><br /><sub><b>Mehmet Erim</b></sub></a><br /><a href="https://github.com/ngneat/forms-manager/commits?author=mehmet-erim" title="Documentation">📖</a></td>
394402
<td align="center"><a href="https://github.com/dspeirs7"><img src="https://avatars2.githubusercontent.com/u/739058?v=4" width="100px;" alt=""/><br /><sub><b>David Speirs</b></sub></a><br /><a href="https://github.com/ngneat/forms-manager/commits?author=dspeirs7" title="Code">💻</a> <a href="https://github.com/ngneat/forms-manager/commits?author=dspeirs7" title="Documentation">📖</a></td>
403+
<td align="center"><a href="https://github.com/manudss"><img src="https://avatars3.githubusercontent.com/u/1046806?v=4" width="100px;" alt=""/><br /><sub><b>Emmanuel De Saint Steban</b></sub></a><br /><a href="https://github.com/ngneat/forms-manager/commits?author=manudss" title="Code">💻</a> <a href="https://github.com/ngneat/forms-manager/commits?author=manudss" title="Documentation">📖</a></td>
395404
</tr>
396405
</table>
397406

projects/ngneat/forms-manager/src/lib/forms-manager.spec.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('FormsManager', () => {
4242
formsManager = null;
4343
});
4444

45-
fit('should update the store with forms value', fakeAsync(() => {
45+
it('should update the store with forms value', fakeAsync(() => {
4646
expect(getSnapshot(formsManager)).toEqual({
4747
config: {
4848
value: '',
@@ -1184,4 +1184,60 @@ describe('FormsManager', () => {
11841184
expect(spy).toHaveBeenCalledTimes(2);
11851185
expect(spy).toHaveBeenCalledWith(['One', 'Two']);
11861186
}));
1187+
1188+
describe('Initial Value', () => {
1189+
const fakeData = {
1190+
name: 'Chuck Norris',
1191+
1192+
date: '1940-03-10',
1193+
phone: {
1194+
number: '123456789007',
1195+
prefix: '+00',
1196+
},
1197+
};
1198+
1199+
const userForm = new FormGroup({
1200+
name: new FormControl('Chuck Norris'),
1201+
email: new FormControl('[email protected]'),
1202+
date: new FormControl('1940-03-10'),
1203+
phone: new FormGroup({
1204+
number: new FormControl('123456789007'),
1205+
prefix: new FormControl('+00'),
1206+
}),
1207+
});
1208+
1209+
it('should get and set initial Value for a control', () => {
1210+
formsManager.setInitialValue('config', 'initial');
1211+
1212+
expect(formsManager.getInitialValue('config')).toEqual('initial');
1213+
});
1214+
1215+
it('should get and set initial Value for a group of control', () => {
1216+
formsManager.setInitialValue('group', fakeData);
1217+
1218+
expect(formsManager.getInitialValue('group')).toEqual(fakeData);
1219+
});
1220+
1221+
it('should get initial Value for a group of control if withInitialValue option has been set in upsert', () => {
1222+
formsManager.upsert('user', userForm, { withInitialValue: true });
1223+
1224+
expect(formsManager.getInitialValue('user')).toEqual(fakeData);
1225+
});
1226+
1227+
it('should get initial Value for a group of control if withInitialValue option has been set in upsert', () => {
1228+
formsManager.upsert('user', userForm, { withInitialValue: true });
1229+
1230+
expect(formsManager.getInitialValue('user')).toEqual(fakeData);
1231+
});
1232+
1233+
it('should get undefined if withInitialValue option has been set in upsert', () => {
1234+
formsManager.upsert('user', userForm);
1235+
1236+
expect(formsManager.getInitialValue('user')).toBeUndefined();
1237+
});
1238+
1239+
it('should get undefined if no initial Value set', () => {
1240+
expect(formsManager.getInitialValue('other')).toBeUndefined();
1241+
});
1242+
});
11871243
});

projects/ngneat/forms-manager/src/lib/forms-manager.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ export class NgFormsManager<FormsState = any> {
189189
return null;
190190
}
191191

192+
/**
193+
*
194+
* Get the initial value for a control
195+
*
196+
* Will return undefined, if no initial value was returned.
197+
*
198+
* @example
199+
*
200+
* manager.getInitialValue('login');
201+
*
202+
*/
203+
getInitialValue<State = any>(name: keyof FormsState): State | undefined;
204+
getInitialValue<T extends keyof FormsState>(name: keyof FormsState): FormsState[T] | undefined;
205+
getInitialValue(name: keyof FormsState): any | undefined {
206+
return this.initialValues$$.get(name);
207+
}
208+
192209
/**
193210
*
194211
* @example

0 commit comments

Comments
 (0)