-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use TestBed.inject instead of TestBed.get - Remove angular-validation-support dependency - executeAfterContentInit no longer works properly in Angular 9. Overriding ngAfterContentInit doesn't work properly anymore. On second thought executeAfterContentInit and ObservableContainer seem overly complex for what we want to achieve, thus I'm removing those usages. - ControlContainer used to be initialised and usable before ngOnInit was called. This is no longer the case, thus added code to handle this.
- Loading branch information
1 parent
cd6ea1f
commit 0d9dcaf
Showing
15 changed files
with
12,913 additions
and
7,399 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,68 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { FormBuilder } from '@angular/forms'; | ||
|
||
import { getControlPath } from './get-control-path'; | ||
|
||
describe('getControlPath', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
FormBuilder | ||
] | ||
}); | ||
}); | ||
|
||
it(`emits paths for form groups`, inject([FormBuilder], (fb: FormBuilder) => { | ||
const firstName = fb.control(''); | ||
fb.group({ | ||
name: fb.group({ | ||
firstName: firstName | ||
}) | ||
}); | ||
|
||
expect(getControlPath(firstName)).toEqual('name.firstName'); | ||
})); | ||
|
||
it(`emits numeric paths for form arrays`, inject([FormBuilder], (fb: FormBuilder) => { | ||
const firstName = fb.control(''); | ||
const firstName2 = fb.control(''); | ||
|
||
fb.group({ | ||
persons: fb.array([ | ||
fb.group({ | ||
firstName: firstName | ||
}), | ||
fb.group({ | ||
firstName: firstName2 | ||
}) | ||
]) | ||
}); | ||
|
||
expect(getControlPath(firstName)).toEqual('persons.0.firstName'); | ||
expect(getControlPath(firstName2)).toEqual('persons.1.firstName'); | ||
})); | ||
|
||
it(`emits an empty string for a control without parents`, inject([FormBuilder], (fb: FormBuilder) => { | ||
const control = fb.control(''); | ||
|
||
expect(getControlPath(control)).toEqual(''); | ||
})); | ||
|
||
it(`emits an index string for a control with only a form array as parent`, inject([FormBuilder], (fb: FormBuilder) => { | ||
const control = fb.control(''); | ||
|
||
fb.array([control]); | ||
|
||
expect(getControlPath(control)).toEqual('0'); | ||
})); | ||
|
||
it(`emits a single identifier for a control with only a single form group as parent`, inject([FormBuilder], (fb: FormBuilder) => { | ||
const control = fb.control(''); | ||
|
||
fb.group({ | ||
control: control | ||
}); | ||
|
||
expect(getControlPath(control)).toEqual('control'); | ||
})); | ||
}); |
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,27 @@ | ||
import { AbstractControl } from '@angular/forms'; | ||
|
||
/** | ||
* Given a control, returns a string representation of the property path to | ||
* this control. Thus, for a FormControl 'firstName', that is part of a | ||
* FormGroup references to as 'name', this function will return: 'name.firstName'. | ||
* | ||
* Note that FormArray indexes are also put in the path, e.g.: 'person.0.name.firstName'. | ||
*/ | ||
export function getControlPath(control: AbstractControl): string { | ||
if (control.parent) { | ||
let path = getControlPath(control.parent); | ||
if (path) { | ||
path += '.'; | ||
} | ||
return path + Object.keys(control.parent.controls).find(key => { | ||
const controls = control.parent.controls; | ||
if (Array.isArray(controls)) { | ||
return controls[Number(key)] === control; | ||
} else { | ||
return controls[key] === control; | ||
} | ||
}); | ||
} | ||
|
||
return ''; | ||
} |
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
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
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
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
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
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
Oops, something went wrong.