Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.

Commit 5899220

Browse files
committed
feat(io-form): added IoFormGroup with a safer patchValue than FormGroup
1 parent 3bedb70 commit 5899220

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

projects/ngx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nll/ngx",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"private": false,
55
"peerDependencies": {
66
"@nll/dux": "^2.1.0",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './io-form.module';
22
export * from './io-form.pipe';
33
export * from './ioToForm';
4+
export * from './io-form-group.class';
5+
export * from './io-form-array.class';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { FormGroup } from '@angular/forms';
2+
3+
export class IoFormGroup extends FormGroup {
4+
// Extend patchValue to safe things up
5+
patchValue(
6+
value: unknown,
7+
options: { onlySelf?: boolean; emitEvent?: boolean } = {}
8+
): void {
9+
if (typeof value === 'object' && value !== null) {
10+
super.patchValue(value, options);
11+
}
12+
}
13+
}

projects/ngx/src/lib/io-form/ioToForm.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { FormControl, FormGroup } from '@angular/forms';
1+
import { FormControl } from '@angular/forms';
22
import { DateFromDatelikeType, EnumType, OptionFromOptionalType } from '@nll/utils-ts/lib/io';
33
import * as t from 'io-ts';
44
import { DateFromISOStringType, OptionFromNullableType } from 'io-ts-types';
55

66
import { IoFormArray } from './io-form-array.class';
7+
import { IoFormGroup } from './io-form-group.class';
78

89
export type Control =
910
| t.StringType
@@ -29,7 +30,7 @@ export type Container = Group | Array;
2930
const UNKNOWN_TYPE_WARNING =
3031
'IoFormService encountered an unknown type codec, defaulting to FormControl for type';
3132

32-
export const ioToForm = (io: IO): FormGroup | IoFormArray | FormControl => {
33+
export const ioToForm = (io: IO): IoFormGroup | IoFormArray | FormControl => {
3334
switch (io._tag) {
3435
// FormControls
3536
case 'BooleanType':
@@ -40,13 +41,13 @@ export const ioToForm = (io: IO): FormGroup | IoFormArray | FormControl => {
4041
case 'DateFromDatelikeType':
4142
return new FormControl();
4243

43-
// FormGroups
44+
// IoFormGroups
4445
case 'InterfaceType':
4546
let group = {};
4647
for (let k in io.props) {
4748
group[k] = ioToForm(io.props[k]);
4849
}
49-
return new FormGroup(group);
50+
return new IoFormGroup(group);
5051

5152
// FormArrays
5253
case 'ArrayType':

0 commit comments

Comments
 (0)