Skip to content

Commit

Permalink
fix(selectvalue): should be typed
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Dec 12, 2019
1 parent 8ec2495 commit a196e96
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,34 @@ export class HomeComponent {
}
```
## NgFormsManager Generic Type
`NgFormsManager` can take a generic type where you can define the forms shape. For example:
```ts
export interface AppForms = {
onboarding: {
name: string;
age: number;
city: string;
}
}
```
This will make sure that the queries are typed, and you don't make any mistakes in the form name.
```ts
export class OnboardingComponent {
constructor(private formsManager: NgFormsManager<AppForms>, private builder: FormBuilder) {}

ngOnInit() {
this.formsManager.selectValue('onboarding').subscribe(value => {
// value now typed as AppForms['onboarding']
});
}
}
```
## NgFormsManager Config
You can override the default config by passing the `NG_FORMS_MANAGER_CONFIG` provider:
Expand Down
8 changes: 5 additions & 3 deletions projects/ngneat/forms-manager/src/lib/forms-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export class NgFormsManager<FormsState = any> {
return this.selectControl(formName, path).pipe(map(control => control.disabled));
}

selectValue<T = any>(formName: keyof FormsState, path?: string): Observable<T> {
selectValue<T extends keyof FormsState>(formName: T, path?: string): Observable<FormsState[T]> {
return this.selectControl(formName, path).pipe(map(control => control.value));
}

selectErrors(formName: keyof FormsState, path?: string): Observable<any> {
return this.selectControl(formName, path).pipe(map(control => control.errors));
selectErrors<T = any>(formName: keyof FormsState, path?: string): Observable<T> {
return this.selectControl(formName, path).pipe(map(control => control.errors as T));
}

/**
Expand All @@ -55,6 +55,7 @@ export class NgFormsManager<FormsState = any> {
);
}

// TODO: _AbstractControl should take a generic that should type the `value` property
getControl(formName: keyof FormsState, path?: string): _AbstractControl {
if (!path) {
return this.getForm(formName);
Expand All @@ -68,6 +69,7 @@ export class NgFormsManager<FormsState = any> {
return null;
}

// TODO: _AbstractControl should take a generic that should type the `value` property
selectForm(
formName: keyof FormsState,
options: { filterNil: true } = { filterNil: true }
Expand Down

0 comments on commit a196e96

Please sign in to comment.