Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk:forms): formArray supports clearControls #1490

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the code review
The code looks okay and should be able to provide the expected functionality. However, I would suggest adding more tests to ensure that the clearControls() method is working as expected. Additionally, you can add a try-catch block to handle any unexpected errors. Also, if possible, use linting tools for better readability and formatting of your code.

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` 处现有的子控件
*
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the review

  1. The code looks quite straightforward, and it seems to have added a new function to the FormArray class.
  2. The new method 'clearControls' looks like it does what it says it does, which is to clear out all of the objects from an array.
  3. However, I would suggest to add a check to make sure that the array is not empty before running the method, to avoid errors.
  4. Also, consider adding a comment block that explains what the method does and what arguments are accepted.

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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the code review

  1. The code looks syntactically correct, as it follows the standard Javascript syntax.
  2. The code is adding a new method ‘clearControls’ to the existing FormArray class which removes all the existing controls from the array.
  3. The method is properly documented using Javadoc style comments which is a good practice.
  4. The logic of the method is simple and easy to understand, so no issues in that regard.
  5. There are no obvious bugs in the code, but it would be better to add some unit tests to make sure it works as expected.
  6. The code should also include some input validation to make sure that the input is valid.
  7. It would also be good to add some logging statements to the method for debugging purposes.

Expand Down