Skip to content

Commit

Permalink
Revert "feat(forms): add ability to clear a FormRecord (#50750)" (#58315
Browse files Browse the repository at this point in the history
)

This reverts commit 3e7d724.

PR Close #58315
  • Loading branch information
AndrewKushnir committed Oct 22, 2024
1 parent a5b0394 commit 79d9be3
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 70 deletions.
3 changes: 0 additions & 3 deletions goldens/public-api/forms/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,6 @@ export interface FormRecord<TControl> {
addControl(name: string, control: TControl, options?: {
emitEvent?: boolean;
}): void;
clear(options?: {
emitEvent?: boolean;
}): void;
contains(controlName: string): boolean;
getRawValue(): {
[key: string]: ɵRawValue<TControl>;
Expand Down
33 changes: 1 addition & 32 deletions packages/forms/src/model/form_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,25 +724,7 @@ export const isFormGroup = (control: unknown): control is FormGroup => control i
*/
export class FormRecord<TControl extends AbstractControl = AbstractControl> extends FormGroup<{
[key: string]: TControl;
}> {
/**
* Clear all controls from this record.
*
* This method also updates the value and validity of the control.
*
* @param options Specifies whether this FormGroup instance should emit events after a
* control is removed.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges` observables emit events with the latest status and value when the controls are
* removed. When false, no events are emitted.
*/
clear(options: {emitEvent?: boolean} = {}): void {
this._forEachChild((child) => child._registerOnCollectionChange(() => {}));
this.controls = {};
this.updateValueAndValidity({emitEvent: options.emitEvent});
this._onCollectionChange();
}
}
}> {}

export interface FormRecord<TControl> {
/**
Expand All @@ -766,19 +748,6 @@ export interface FormRecord<TControl> {
*/
removeControl(name: string, options?: {emitEvent?: boolean}): void;

/**
* Clear all controls from this record.
*
* This method also updates the value and validity of the control.
*
* @param options Specifies whether this FormGroup instance should emit events after a
* control is removed.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges` observables emit events with the latest status and value when the controls are
* removed. When false, no events are emitted.
*/
clear(options?: {emitEvent?: boolean}): void;

/**
* Replace an existing control.
*
Expand Down
35 changes: 0 additions & 35 deletions packages/forms/test/form_group_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
FormArray,
FormControl,
FormGroup,
FormRecord,
ValidationErrors,
Validators,
ValueChangeEvent,
Expand Down Expand Up @@ -2587,39 +2586,5 @@ import {StatusChangeEvent} from '../src/model/abstract_model';
});
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
});

describe('clear()', () => {
let c1: FormControl;
let c2: FormControl;
let r: FormRecord;

beforeEach(() => {
c1 = new FormControl('one');
c2 = new FormControl('two');
r = new FormRecord({one: c1, two: c2});
});

it('should remove control if new control is null', () => {
r.clear();
expect(r.controls['one']).not.toBeDefined();
expect(r.controls['two']).not.toBeDefined();
expect(r.value).toEqual({});
});

it('should only emit value change event once', () => {
const logger: string[] = [];
r.valueChanges.subscribe(() => logger.push('change!'));
r.clear();
expect(logger).toEqual(['change!']);
});

it('should not emit event when `FormGroup.clearControls` called with `emitEvent: false`', () => {
const logger: string[] = [];
r.valueChanges.subscribe(() => logger.push('value change'));
r.statusChanges.subscribe(() => logger.push('status change'));
r.clear({emitEvent: false});
expect(logger).toEqual([]);
});
});
});
})();

0 comments on commit 79d9be3

Please sign in to comment.