-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dba517
commit c78e5dc
Showing
45 changed files
with
1,737 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {AbstractControl, FormArray} from '@angular/forms'; | ||
import {ControlFactory} from './utils'; | ||
|
||
/** Extends FormArray so it contains definition of items for further creation */ | ||
export class FormArrayExtended extends FormArray { | ||
constructor(public createControl: ControlFactory, controls: AbstractControl[], ...rest: any[]) { | ||
super(controls, ...rest); | ||
} | ||
|
||
override setValue(value: any[], options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void { | ||
this.setSize(value.length); | ||
super.setValue(value, options); | ||
} | ||
|
||
/** | ||
* Sets specified number of controls in the array | ||
* @param size of the array | ||
*/ | ||
setSize(size: number) { | ||
while (size < this.controls.length) this.removeAt(0); | ||
while (size > this.controls.length) this.push(this.createControl()); | ||
} | ||
} |
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,28 @@ | ||
import {AbstractControl, FormGroup} from '@angular/forms'; | ||
import {ControlFactory} from './utils'; | ||
|
||
/** Extends FormGroup so it contains definition of map items for further creation */ | ||
export class FormMap extends FormGroup { | ||
constructor(public createControl: ControlFactory, controls: Record<string, AbstractControl>, ...rest: any[]) { | ||
super(controls, ...rest); | ||
} | ||
|
||
override setValue(value: Record<string, any>, options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void { | ||
this.setShape(Object.keys(value)); | ||
super.setValue(value, options); | ||
} | ||
|
||
/** | ||
* Sets child controls for a specified list of keys | ||
* @param keys list of keys new form group should contain | ||
*/ | ||
setShape(keys: string[]) { | ||
const allKeys = new Set([...keys, ...Object.keys(this.controls)]); | ||
allKeys.forEach(key => { | ||
// add control for a new one | ||
if (!(key in this.controls)) this.addControl(key, this.createControl()); | ||
// remove control if missing | ||
else if (!keys.includes(key)) this.removeControl(key); | ||
}); | ||
} | ||
} |
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,52 @@ | ||
import {AbstractControl, FormArray, FormControl, FormGroup} from '@angular/forms'; | ||
|
||
import {FormArrayExtended} from './formArrayExtended'; | ||
import {FormMap} from './formMap'; | ||
|
||
export type ControlFactory = () => AbstractControl; | ||
|
||
/** | ||
* Recursively sets values of nested controls so nested object === null/undefined | ||
* does not cause failure as in case of AbstractControl.patchValue | ||
* @param control target FormControl, FormArray or FormGroup | ||
* @param value source data | ||
*/ | ||
export function safeSetValue(control: AbstractControl, value: any) { | ||
if (control instanceof FormControl) { | ||
control.setValue(value, {emitEvent: false}); | ||
return; | ||
} | ||
|
||
if (control instanceof FormArrayExtended) { | ||
if (nullOrUndef(value)) value = []; | ||
if (!Array.isArray(value)) throw new TypeError(`Cannot set value '${value}' on FormArrayExtended`); | ||
|
||
control.setSize(value.length); | ||
control.controls.forEach((c, idx) => safeSetValue(c, value[idx])); | ||
} else if (control instanceof FormMap) { | ||
if (nullOrUndef(value)) value = {}; | ||
if (typeof value !== 'object' || Array.isArray(value)) { | ||
throw new TypeError(`Cannot set value '${value}' on FormMap`); | ||
} | ||
|
||
control.setShape(Object.keys(value)); | ||
Object.entries(control.controls).forEach(([name, c]) => safeSetValue(c, value[name])); | ||
} else if (control instanceof FormArray) { | ||
control.controls.forEach((child, idx) => | ||
safeSetValue(child, getValue(value, idx))); | ||
} else if (control instanceof FormGroup) { | ||
Object.keys(control.controls).forEach(name => { | ||
safeSetValue(control.controls[name], getValue(value, name)); | ||
}); | ||
} | ||
} | ||
|
||
function nullOrUndef(input: any) { | ||
return input === undefined || input === null; | ||
} | ||
|
||
function getValue(input: any, attribute: string | number) { | ||
return nullOrUndef(input) || typeof input !== 'object' ? | ||
undefined : | ||
input[attribute]; | ||
} |
Oops, something went wrong.