Skip to content

Commit 92675f7

Browse files
committed
feat(cdk): rxIfList does now accept also Observables as input
1 parent e53e0a1 commit 92675f7

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

libs/cdk/template/src/lib/rx-if-list/rx-if-list.directive.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import {
33
EmbeddedViewRef,
44
Input,
55
NgModule,
6+
OnDestroy,
67
TemplateRef,
78
ViewContainerRef,
89
ɵstringify as stringify
910
} from '@angular/core';
1011
import {CommonModule} from '@angular/common';
12+
import {distinctUntilChanged, mergeAll, Observable, ReplaySubject, Subscription} from "rxjs";
13+
import {coerceObservable} from "@angular-kit/cdk/coercing";
1114

1215
@Directive({
1316
// eslint-disable-next-line @angular-eslint/directive-selector
1417
selector: '[rxIfList]',
1518
})
16-
export class RxIfListDirective {
19+
export class RxIfListDirective implements OnDestroy{
20+
private readonly sub = new Subscription();
21+
private readonly valueSource$$ = new ReplaySubject<Observable<ArrayLike<unknown> | null | undefined> >(1);
1722
private _context: RxIfListContext = new RxIfListContext();
1823
private _thenTemplateRef: TemplateRef<RxIfListContext>|null = null;
1924
private _elseTemplateRef: TemplateRef<RxIfListContext>|null = null;
@@ -45,15 +50,27 @@ export class RxIfListDirective {
4550

4651
constructor(private _viewContainer: ViewContainerRef, templateRef: TemplateRef<RxIfListContext>) {
4752
this._thenTemplateRef = templateRef;
53+
this.sub.add(
54+
this.valueSource$$.asObservable().pipe(
55+
distinctUntilChanged(),
56+
mergeAll(),
57+
distinctUntilChanged()
58+
).subscribe(value => {
59+
this._context.$implicit = this._context.rxIfList = value;
60+
this._updateView(this._context);
61+
})
62+
)
63+
}
64+
ngOnDestroy() {
65+
this.sub.unsubscribe();
4866
}
4967

5068
/**
5169
* The Boolean expression to evaluate as the condition for showing a template.
5270
*/
5371
@Input()
54-
set rxIfList(value: ArrayLike<unknown> | null | undefined) {
55-
this._context.$implicit = this._context.rxIfList = value;
56-
this._updateView();
72+
set rxIfList(value: ArrayLike<unknown> | Observable<ArrayLike<unknown>> | null | undefined) {
73+
this.valueSource$$.next(coerceObservable(value));
5774
}
5875

5976
/**
@@ -64,7 +81,7 @@ export class RxIfListDirective {
6481
assertTemplate('rxIfListThen', templateRef);
6582
this._thenTemplateRef = templateRef;
6683
this._thenViewRef = null; // clear previous view if any.
67-
this._updateView();
84+
this._updateView(this._context);
6885
}
6986

7087
/**
@@ -75,17 +92,17 @@ export class RxIfListDirective {
7592
assertTemplate('rxIfListElse', templateRef);
7693
this._elseTemplateRef = templateRef;
7794
this._elseViewRef = null; // clear previous view if any.
78-
this._updateView();
95+
this._updateView(this._context);
7996
}
8097

81-
private _updateView() {
82-
if (this._context.$implicit && (((this._context.$implicit as ArrayLike<any>)?.length ?? []) > 0)){
98+
private _updateView(ctx: RxIfListContext) {
99+
if (ctx.$implicit && (((ctx.$implicit as ArrayLike<any>)?.length ?? []) > 0)){
83100
if (!this._thenViewRef) {
84101
this._viewContainer.clear();
85102
this._elseViewRef = null;
86103
if (this._thenTemplateRef) {
87104
this._thenViewRef =
88-
this._viewContainer.createEmbeddedView(this._thenTemplateRef, this._context);
105+
this._viewContainer.createEmbeddedView(this._thenTemplateRef, ctx);
89106
}
90107
}
91108
} else {
@@ -94,7 +111,7 @@ export class RxIfListDirective {
94111
this._thenViewRef = null;
95112
if (this._elseTemplateRef) {
96113
this._elseViewRef =
97-
this._viewContainer.createEmbeddedView(this._elseTemplateRef, this._context);
114+
this._viewContainer.createEmbeddedView(this._elseTemplateRef, ctx);
98115
}
99116
}
100117
}

0 commit comments

Comments
 (0)