Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 523e861

Browse files
committed
Use a subject for showContent
Change detection is broken for closing cards now that on push detection is enabled. This is because when a card opens, other cards that are closing have no way of knowing they need to update. By switching showContent to an observable, the async pipe will take care of this for us.
1 parent 3822dc6 commit 523e861

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

source/components/cardContainer/card/card.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</div>
1313
</div>
1414

15-
<div [hidden]="!showContent">
15+
<div [hidden]="!(showContent$ | async)">
1616
<div class="body"
1717
*ngIf="cardContainer.cardContent"
1818
[class.active]="cardContainer.clickableCards"
@@ -21,10 +21,10 @@
2121
<div class="clearfix"></div>
2222
</div>
2323
</div>
24-
<div [hidden]="!cardContainer.cardFooter || !(showContent || permanentFooter)">
24+
<div [hidden]="!cardContainer.cardFooter || !((showContent$ | async) || permanentFooter)">
2525
<div class="footer" *ngIf="cardContainer.cardFooter">
2626
<template [ngTemplateOutlet]="cardContainer.cardFooter.template" [ngOutletContext]="{ $implicit: item }"></template>
2727
<div class="clearfix"></div>
2828
</div>
2929
</div>
30-
</div>
30+
</div>

source/components/cardContainer/card/card.tests.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,15 @@ describe('CardComponent', () => {
6262

6363
describe('toggle', (): void => {
6464
it('should toggle the card content', (): void => {
65-
66-
expect(card.showContent).to.be.false;
65+
expect(card.showContent$.getValue()).to.be.false;
6766

6867
card.toggleContent();
6968

70-
expect(card.showContent).to.be.true;
69+
expect(card.showContent$.getValue()).to.be.true;
7170

7271
card.toggleContent();
7372

74-
expect(card.showContent).to.be.false;
73+
expect(card.showContent$.getValue()).to.be.false;
7574
});
7675
});
7776

@@ -88,12 +87,12 @@ describe('CardComponent', () => {
8887

8988
describe('close', (): void => {
9089
it('should close the card content if the submit is successful', (): void => {
91-
card.showContent = true;
90+
card.showContent$.next(true);
9291
card.submit = sinon.spy(() => true);
9392

9493
const closed = card.close();
9594

96-
expect(card.showContent).to.be.false;
95+
expect(card.showContent$.getValue()).to.be.false;
9796
expect(closed).to.be.true;
9897
});
9998

@@ -107,12 +106,12 @@ describe('CardComponent', () => {
107106
});
108107

109108
it('should not close the card if submit fails', (): void => {
110-
card.showContent = true;
109+
card.showContent$.next(true);
111110
card.submit = sinon.spy(() => false);
112111

113112
const closed = card.close();
114113

115-
expect(card.showContent).to.be.true;
114+
expect(card.showContent$.getValue()).to.be.true;
116115
expect(closed).to.be.false;
117116
});
118117
});

source/components/cardContainer/card/card.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Inject, forwardRef, Optional, SkipSelf, ChangeDetectionStrategy } from '@angular/core';
2-
import { Subject } from 'rxjs';
2+
import { Subject, BehaviorSubject } from 'rxjs';
33
import { isFunction, assign } from 'lodash';
44

55
import { services } from 'typescript-angular-utilities';
@@ -41,7 +41,7 @@ export class CardComponent<T> extends FormComponent {
4141
initCard: { (): void } = () => null;
4242
clickCard: { (): void } = () => null;
4343

44-
showContent: boolean = false;
44+
showContent$: BehaviorSubject<boolean>;
4545

4646
cardContainer: CardContainerComponent<T>;
4747

@@ -52,10 +52,11 @@ export class CardComponent<T> extends FormComponent {
5252
, @Inject(forwardRef(() => CardContainerComponent)) cardContainer: CardContainerComponent<T>) {
5353
super(notification, asyncHelper, formService, parentForm);
5454
this.cardContainer = cardContainer;
55+
this.showContent$ = new BehaviorSubject(false);
5556
}
5657

5758
toggleContent(): void {
58-
if (this.showContent) {
59+
if (this.showContent$.getValue()) {
5960
this.close();
6061
} else {
6162
this.open();
@@ -68,19 +69,19 @@ export class CardComponent<T> extends FormComponent {
6869
}
6970

7071
if (this.cardContainer.openCard()) {
71-
this.showContent = true;
72+
this.showContent$.next(true);
7273
}
7374
}
7475

7576
close(): boolean {
76-
if (!this.showContent) {
77+
if (!this.showContent$.getValue()) {
7778
return true;
7879
}
7980

8081
const canClose: boolean = !!this.submit();
8182

8283
if (canClose) {
83-
this.showContent = false;
84+
this.showContent$.next(false);
8485
}
8586

8687
return canClose;

0 commit comments

Comments
 (0)