-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9a24cc
commit f32bac2
Showing
7 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# @angular-kit/cdk/template | ||
|
||
A package providing some useful Angular ehancements for templates. | ||
|
||
## 🔋 Included | ||
* `*rxIfList` - `*ngIf` for lists. | ||
|
||
### `rxIfList` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/rx-if-list/rx-if-list.directive'; |
50 changes: 50 additions & 0 deletions
50
libs/cdk/template/src/lib/rx-if-list/rx-if-list.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {RxIfListDirective, RxIfListDirectiveModule} from './rx-if-list.directive'; | ||
import {TestBed} from '@angular/core/testing'; | ||
import {Component} from '@angular/core'; | ||
|
||
describe('RxIfListDirective', () => { | ||
it('should create an instance of test host', async () => { | ||
const { component } = await setup(`<div *rxIfList="[]"></div>`); | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should not render template when array is empty', async () => { | ||
const { fixture } = await setup(`<div *rxIfList="[]"></div>`); | ||
expect(fixture.nativeElement.innerHTML).not.toContain('<div></div>'); | ||
}); | ||
|
||
it('should not render template when array is null', async () => { | ||
const { fixture } = await setup(`<div *rxIfList="null"></div>`); | ||
expect(fixture.nativeElement.innerHTML).not.toContain('<div></div>'); | ||
}); | ||
|
||
it('should not render template when array is undefined', async () => { | ||
const { fixture } = await setup(`<div *rxIfList="undefined"></div>`); | ||
expect(fixture.nativeElement.innerHTML).not.toContain('<div></div>'); | ||
}); | ||
|
||
it('should render template when array is not empty', async () => { | ||
const { fixture } = await setup(`<div *rxIfList="['a']"></div>`); | ||
expect(fixture.nativeElement.innerHTML).toContain('<div></div>'); | ||
}); | ||
}); | ||
|
||
async function setup(template: string) { | ||
@Component({ | ||
template, | ||
}) | ||
class TestHostComponent {} | ||
|
||
await TestBed.configureTestingModule({ | ||
declarations: [TestHostComponent], | ||
imports: [RxIfListDirectiveModule], | ||
//providers: [RxIfListDirective] | ||
}).compileComponents(); | ||
|
||
//const rxIfListDirective = TestBed.inject(RxIfListDirective); | ||
const fixture = TestBed.createComponent(TestHostComponent); | ||
const component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
|
||
return { fixture, component }; | ||
} |
25 changes: 25 additions & 0 deletions
25
libs/cdk/template/src/lib/rx-if-list/rx-if-list.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Directive, Input, NgModule, TemplateRef, ViewContainerRef} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
|
||
@Directive({ | ||
selector: '[rxIfList]', | ||
}) | ||
export class RxIfListDirective { | ||
@Input() set rxIfList(value: ArrayLike<unknown> | null | undefined) { | ||
this.vcr.clear(); | ||
if ((value?.length ?? []) > 0) { | ||
this.vcr.createEmbeddedView(this.tpl); | ||
} | ||
} | ||
constructor( | ||
private readonly vcr: ViewContainerRef, | ||
private readonly tpl: TemplateRef<unknown> | ||
) {} | ||
} | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
declarations: [RxIfListDirective], | ||
exports: [RxIfListDirective], | ||
}) | ||
export class RxIfListDirectiveModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters