-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now the form builder can be used to build CdFormGroups. Signed-off-by: Stephan Müller <[email protected]>
- Loading branch information
Stephan Müller
committed
Jul 3, 2018
1 parent
083865d
commit db9d15f
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-form-builder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Validators } from '@angular/forms'; | ||
import { CdFormBuilder } from './cd-form-builder'; | ||
import { CdFormGroup } from './cd-form-group'; | ||
|
||
describe('cd-form-builder', () => { | ||
let service: CdFormBuilder; | ||
|
||
beforeEach(() => { | ||
service = new CdFormBuilder(); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should create a nested CdFormGroup', () => { | ||
const form = service.group({ | ||
nested: service.group({ | ||
a: [null], | ||
b: ['sth'], | ||
c: [2, [Validators.min(3)]] | ||
}), | ||
d: [{ e: 3 }], | ||
f: [true] | ||
}); | ||
expect(form.constructor).toBe(CdFormGroup); | ||
expect(form instanceof CdFormGroup).toBeTruthy(); | ||
expect(form.getValue('b')).toBe('sth'); | ||
expect(form.getValue('d')).toEqual({ e: 3 }); | ||
expect(form.get('c').valid).toBeFalsy(); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-form-builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { FormBuilder } from '@angular/forms'; | ||
|
||
import { ServicesModule } from '../services/services.module'; | ||
import { CdFormGroup } from './cd-form-group'; | ||
|
||
/** | ||
* CdFormBuilder extends FormBuilder to create an CdFormGroup based form. | ||
*/ | ||
@Injectable({ | ||
providedIn: ServicesModule | ||
}) | ||
export class CdFormBuilder extends FormBuilder { | ||
group( | ||
controlsConfig: { [key: string]: any }, | ||
extra: { [key: string]: any } | null = null | ||
): CdFormGroup { | ||
const form = super.group(controlsConfig, extra); | ||
return new CdFormGroup(form.controls, form.validator, form.asyncValidator); | ||
} | ||
} |