Skip to content

Commit

Permalink
feat(form-array): implement remove helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioArnt committed Aug 14, 2020
1 parent 419c8c2 commit 0011944
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions projects/ngneat/reactive-forms/src/lib/formArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,16 @@ export class FormArray<T = any, E extends object = any> extends NgFormArray {
}

remove(value: T) {
throw Error('NotImplemented');
this.removeWhen(v => v.value === value);
}

removeWhen(predicate: (element: AbstractControl<T>) => boolean) {
throw Error('NotImplemented');
const toRemove: number[] = [];
for (let i = 0; i < this.length; ++i) {
if (predicate(this.at(i))) {
toRemove.push(i);
}
}
toRemove.sort((idx1, idx2) => idx2 - idx1).forEach(idx => this.removeAt(idx));
}
}

0 comments on commit 0011944

Please sign in to comment.