-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): added directive NgForTrackByKey
Example: *ngFor="let item of items trackByKey 'key'"
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
projects/platform/src/lib/directives/track-by-key.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,28 @@ | ||
import { NgForOf } from '@angular/common'; | ||
import { Directive, Host, Input, Optional } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[ngForTrackByKey]' | ||
}) | ||
export class NgForTrackByKeyDirective<T> { | ||
|
||
@Input() | ||
set ngForTrackByKey(key: keyof T) { | ||
if (key) { | ||
this.ngFor.ngForTrackBy = (index: number, item: T): T[keyof T] => item[key]; | ||
} else { | ||
this.ngFor.ngForTrackBy = undefined; | ||
} | ||
|
||
this.ngFor['_differ'] = null; | ||
this.ngFor['_ngForOfDirty'] = true; | ||
this.ngFor.ngDoCheck(); | ||
} | ||
|
||
constructor(@Host() @Optional() private ngFor: NgForOf<T>) { | ||
if (!ngFor) { | ||
throw new Error('TrackByKey should use with *ngFor!'); | ||
} | ||
} | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
projects/platform/src/test/directives/track-by-key.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,41 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { createHostComponentFactory, SpectatorWithHost } from '@netbasal/spectator'; | ||
import { NgForTrackByKeyDirective } from '../../lib/directives/track-by-key.directive'; | ||
|
||
describe('NgForTrackByKeyDirective', () => { | ||
let host: SpectatorWithHost<NgForTrackByKeyDirective<any>>; | ||
const create = createHostComponentFactory({ | ||
component: NgForTrackByKeyDirective, | ||
declarations: [ NgForTrackByKeyDirective ], | ||
imports: [ CommonModule ] | ||
}); | ||
|
||
it('should add generate trackBy fn by key', () => { | ||
host = create(` | ||
<ng-container *ngFor=" | ||
let item of [ | ||
{ animal: '🦊' }, | ||
{ animal: '🦄' } | ||
] trackByKey 'animal'"> | ||
{{ item.animal }} | ||
</ng-container> | ||
`); | ||
|
||
expect(host.element).toHaveText('🦊 🦄'); | ||
}); | ||
|
||
it('should throw exception', () => { | ||
expect(() => { | ||
host = create(` | ||
<ng-container *ngFor=" | ||
let item of [ | ||
{ animal: '🦊' }, | ||
null | ||
] trackByKey 'animal'"> | ||
{{ item?.animal }} | ||
</ng-container> | ||
`); | ||
}).toThrow(`Cannot read property 'animal' of null`); | ||
}); | ||
|
||
}); |