-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repeat): add Repeat directive (#19)
closes #14 This PR adds `Repeat` directive that extends `NgFor`. This directive allows consumers to iterate over a number of times instead of a list of items ```html <!-- before --> <p *ngFor="let i of [0, 1, 2]">{{ i }}</p> <!-- after --> <p *ngFor="let i; repeat 3">{{ i }}</p> ```
- Loading branch information
Showing
8 changed files
with
58 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
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,3 @@ | ||
# ngxtension/repeat | ||
|
||
Secondary entry point of `ngxtension`. It can be used by importing from `ngxtension/repeat`. |
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 './repeat'; |
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,26 @@ | ||
import { Component } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { Repeat } from './repeat'; | ||
|
||
describe(Repeat.name, () => { | ||
@Component({ | ||
standalone: true, | ||
template: ` | ||
<p *ngFor="let i; repeat: 3">{{ i }}</p> | ||
`, | ||
imports: [Repeat], | ||
}) | ||
class Dummy {} | ||
|
||
it('given 3, when render, then render 3 items', () => { | ||
const fixture = TestBed.createComponent(Dummy); | ||
fixture.detectChanges(); | ||
|
||
const items = fixture.debugElement.queryAll(By.css('p')); | ||
expect(items).toHaveLength(3); | ||
items.forEach((item, i) => { | ||
expect(item.nativeElement.textContent).toContain(i.toString()); | ||
}); | ||
}); | ||
}); |
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,18 @@ | ||
import { NgFor } from '@angular/common'; | ||
import { Directive, Input } from '@angular/core'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
selector: '[ngFor][ngForRepeat]', | ||
}) | ||
export class Repeat extends NgFor<number> { | ||
@Input() set ngForRepeat(count: number) { | ||
if (Number.isNaN(count) || !Number.isInteger(count)) { | ||
throw new Error( | ||
`[Repeat] repeat requires an integer but ${count} is passed in` | ||
); | ||
} | ||
this.ngForOf = Array.from({ length: count }, (_, i) => i); | ||
this.ngForTrackBy = (i) => i; | ||
} | ||
} |
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