Skip to content

Commit

Permalink
feat(cdk:forms): formArray supports clearControls (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
danranVm authored Mar 6, 2023
1 parent 1f1484f commit 1cd4dcc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/cdk/forms/__tests__/formArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ describe('formArray.ts', () => {
expect(array.blurred.value).toEqual(false)
})

test('clearControls work', async () => {
expect(array.length.value).toEqual(1)
expect(array.getValue()).toEqual([basicValue])

array.clearControls()

expect(array.length.value).toEqual(0)
expect(array.getValue()).toEqual([])
})

test('setControl work', async () => {
expect(array.getValue()).toEqual([basicValue])
const group = new FormGroup<BasicGroup>({
Expand Down
4 changes: 4 additions & 0 deletions packages/cdk/forms/docs/Api.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ export class FormArray<T = any> extends AbstractControl<T[]> {
* @param index 删除控件的下标
*/
removeAt(index: number): void;
/**
* 清空数组中的所有子空间
*/
clearControls(): void;
/**
* 替换数组中给定 `index` 处现有的子控件
*
Expand Down
6 changes: 6 additions & 0 deletions packages/cdk/forms/src/models/formArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export class FormArray<T = any> extends AbstractControl<T[]> {
controls.splice(index, 1)
this._controls.value = controls
}
/**
* Empties out the controls.
*/
clearControls(): void {
this._controls.value = []
}

/**
* Replace an existing control.
Expand Down

0 comments on commit 1cd4dcc

Please sign in to comment.