Skip to content

Commit 274420a

Browse files
committed
fix(rewrite): custom change detection with OnPush
#93
1 parent ada8453 commit 274420a

File tree

15 files changed

+40
-33
lines changed

15 files changed

+40
-33
lines changed

projects/ngx-sub-form/src/lib/ngx-sub-form.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ɵmarkDirty as markDirty } from '@angular/core';
12
import { FormControl } from '@angular/forms';
23
import isEqual from 'fast-deep-equal';
34
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
@@ -15,13 +16,6 @@ import {
1516
tap,
1617
withLatestFrom,
1718
} from 'rxjs/operators';
18-
import {
19-
ArrayPropertyValue,
20-
isNullOrUndefined,
21-
OneOfControlsTypes,
22-
TypedAbstractControl,
23-
TypedFormControl,
24-
} from './shared/ngx-sub-form-utils';
2519
import {
2620
createFormDataFromOptions,
2721
getControlValueAccessorBindings,
@@ -32,7 +26,6 @@ import {
3226
import {
3327
ComponentHooks,
3428
ControlValueAccessorComponentInstance,
35-
CreateFormArrayControlMethod,
3629
FormBindings,
3730
FormType,
3831
NgxFormOptions,
@@ -42,6 +35,7 @@ import {
4235
NgxSubFormArrayOptions,
4336
NgxSubFormOptions,
4437
} from './ngx-sub-form.types';
38+
import { isNullOrUndefined } from './shared/ngx-sub-form-utils';
4539

4640
const optionsHaveInstructionsToCreateArrays = <ControlInterface, FormInterface>(
4741
options: NgxFormOptions<ControlInterface, FormInterface> & Partial<NgxSubFormArrayOptions<FormInterface>>,
@@ -212,17 +206,16 @@ export function createForm<ControlInterface, FormInterface>(
212206
handleFormArrays<FormInterface>(formArrays, value, createFormArrayControl);
213207

214208
formGroup.reset(value, { emitEvent: false });
215-
216-
// commenting out the following for now as it seems that calling
217-
// `markDirty` on a component when an input hasn't been set
218-
// (in this case on a root form) then it throws an error
219-
// Cannot read property 'nodeIndex' of null
220-
// so we'll see later on if this is really needed or if it can
221-
// be removed
209+
}),
210+
),
211+
supportChangeDetectionStrategyOnPush: controlValue$.pipe(
212+
delay(0),
213+
tap(() =>
222214
// support `changeDetection: ChangeDetectionStrategy.OnPush`
223215
// on the component hosting a form
224-
// markDirty(componentInstance);
225-
}),
216+
// fixes https://github.com/cloudnc/ngx-sub-form/issues/93
217+
markDirty(componentInstance),
218+
),
226219
),
227220
setDisabledState$: setDisabledState$.pipe(
228221
tap((shouldDisable: boolean) => {

src/app/main-rewrite/listing/listing-form/droid-listing/assassin-droid/assassin-droid.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { AssassinDroid, AssassinDroidWeapon, DroidType } from 'src/app/interfaces/droid.interface';
@@ -15,6 +15,7 @@ export const ASSASSIN_DROID_WEAPON_TEXT: { [K in AssassinDroidWeapon]: string }
1515
templateUrl: './assassin-droid.component.html',
1616
styleUrls: ['./assassin-droid.component.scss'],
1717
providers: subformComponentProviders(AssassinDroidComponent),
18+
changeDetection: ChangeDetectionStrategy.OnPush,
1819
})
1920
export class AssassinDroidComponent {
2021
public AssassinDroidWeapon = AssassinDroidWeapon;

src/app/main-rewrite/listing/listing-form/droid-listing/astromech-droid/astromech-droid.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { AstromechDroid, AstromechDroidShape, DroidType } from '../../../../../interfaces/droid.interface';
@@ -8,6 +8,7 @@ import { AstromechDroid, AstromechDroidShape, DroidType } from '../../../../../i
88
templateUrl: './astromech-droid.component.html',
99
styleUrls: ['./astromech-droid.component.scss'],
1010
providers: subformComponentProviders(AstromechDroidComponent),
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
})
1213
export class AstromechDroidComponent {
1314
public AstromechDroidShape = AstromechDroidShape;

src/app/main-rewrite/listing/listing-form/droid-listing/droid-product.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import {
@@ -24,6 +24,7 @@ interface OneDroidForm {
2424
templateUrl: './droid-product.component.html',
2525
styleUrls: ['./droid-product.component.scss'],
2626
providers: subformComponentProviders(DroidProductComponent),
27+
changeDetection: ChangeDetectionStrategy.OnPush,
2728
})
2829
export class DroidProductComponent {
2930
public DroidType = DroidType;

src/app/main-rewrite/listing/listing-form/droid-listing/medical-droid/medical-droid.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { DroidType, MedicalDroid } from 'src/app/interfaces/droid.interface';
@@ -8,6 +8,7 @@ import { DroidType, MedicalDroid } from 'src/app/interfaces/droid.interface';
88
templateUrl: './medical-droid.component.html',
99
styleUrls: ['./medical-droid.component.scss'],
1010
providers: subformComponentProviders(MedicalDroidComponent),
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
})
1213
export class MedicalDroidComponent {
1314
public form = createForm<MedicalDroid>(this, {

src/app/main-rewrite/listing/listing-form/droid-listing/protocol-droid/protocol-droid.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
44
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
@@ -9,6 +9,7 @@ import { DroidType, Languages, ProtocolDroid } from '../../../../../interfaces/d
99
templateUrl: './protocol-droid.component.html',
1010
styleUrls: ['./protocol-droid.component.scss'],
1111
providers: subformComponentProviders(ProtocolDroidComponent),
12+
changeDetection: ChangeDetectionStrategy.OnPush,
1213
})
1314
export class ProtocolDroidComponent {
1415
public Languages = Languages;

src/app/main-rewrite/listing/listing-form/listing-form.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, Output } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Input, Output } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType } from 'ngx-sub-form';
44
import { Subject } from 'rxjs';
@@ -21,6 +21,7 @@ interface OneListingForm {
2121
selector: 'app-listing-form',
2222
templateUrl: './listing-form.component.html',
2323
styleUrls: ['./listing-form.component.scss'],
24+
changeDetection: ChangeDetectionStrategy.OnPush,
2425
})
2526
export class ListingFormComponent {
2627
public ListingType: typeof ListingType = ListingType;

src/app/main-rewrite/listing/listing-form/vehicle-listing/crew-members/crew-member/crew-member.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { CrewMember } from '../../../../../../interfaces/crew-member.interface';
@@ -8,6 +8,7 @@ import { CrewMember } from '../../../../../../interfaces/crew-member.interface';
88
templateUrl: './crew-member.component.html',
99
styleUrls: ['./crew-member.component.scss'],
1010
providers: subformComponentProviders(CrewMemberComponent),
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
})
1213
export class CrewMemberComponent {
1314
public form = createForm<CrewMember>(this, {

src/app/main-rewrite/listing/listing-form/vehicle-listing/crew-members/crew-members.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormArray, FormControl, Validators } from '@angular/forms';
3-
import { ArrayPropertyKey, ArrayPropertyValue, createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
3+
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { CrewMember } from '../../../../../interfaces/crew-member.interface';
55

66
interface CrewMembersForm {
@@ -12,6 +12,7 @@ interface CrewMembersForm {
1212
templateUrl: './crew-members.component.html',
1313
styleUrls: ['./crew-members.component.scss'],
1414
providers: subformComponentProviders(CrewMembersComponent),
15+
changeDetection: ChangeDetectionStrategy.OnPush,
1516
})
1617
export class CrewMembersComponent {
1718
public form = createForm<CrewMember[], CrewMembersForm>(this, {

src/app/main-rewrite/listing/listing-form/vehicle-listing/spaceship/spaceship.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { FormControl, Validators } from '@angular/forms';
33
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
44
import { Spaceship, VehicleType } from 'src/app/interfaces/vehicle.interface';
@@ -8,6 +8,7 @@ import { Spaceship, VehicleType } from 'src/app/interfaces/vehicle.interface';
88
templateUrl: './spaceship.component.html',
99
styleUrls: ['./spaceship.component.scss'],
1010
providers: subformComponentProviders(SpaceshipComponent),
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
})
1213
export class SpaceshipComponent {
1314
public form = createForm<Spaceship>(this, {

0 commit comments

Comments
 (0)