Skip to content

Commit 4061cc7

Browse files
author
VladimirAmiorkov
committed
fix(list-view): Add support for default item template
1 parent 726bdd6 commit 4061cc7

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

Diff for: nativescript-angular/directives/templated-items-comp.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ export abstract class TemplatedItemsComponent implements DoCheck, OnDestroy, Aft
155155

156156
viewRef = args.view[NG_VIEW];
157157

158-
// No ng-template is setup, continue with 'defaultTemplate'
159-
if (viewRef) {
160-
return;
161-
}
162-
163158
// Getting angular view from original element (in cases when ProxyViewContainer
164159
// is used NativeScript internally wraps it in a StackLayout)
165160
if (!viewRef && args.view instanceof LayoutBase && args.view.getChildrenCount() > 0) {
@@ -169,6 +164,11 @@ export abstract class TemplatedItemsComponent implements DoCheck, OnDestroy, Aft
169164
if (!viewRef && isLogEnabled()) {
170165
listViewError(`ViewReference not found for item ${index}. View recycling is not working`);
171166
}
167+
168+
// No ng-template is setup, continue with 'defaultTemplate'
169+
if (!viewRef) {
170+
return;
171+
}
172172
}
173173

174174
if (!viewRef) {

Diff for: tests/app/tests/list-view-tests.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { assert } from "./test-config";
2-
import { Component, Input } from "@angular/core";
2+
import { Component, Input, ViewChild } from "@angular/core";
33
import { ComponentFixture, async } from "@angular/core/testing";
44
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from "nativescript-angular/testing";
5+
import { ListViewComponent } from "nativescript-angular/directives";
56
// import trace = require("trace");
67
// trace.setCategories("ns-list-view, " + trace.categories.Navigation);
78
// trace.enable();
@@ -76,11 +77,34 @@ export class TestListViewSelectorComponent {
7677
constructor() { testTemplates = { first: 0, second: 0 }; }
7778
}
7879

80+
@Component({
81+
selector: "list-view-default-item-template",
82+
template: `
83+
<GridLayout>
84+
<ListView #listView [items]="myItems"></ListView>
85+
</GridLayout>
86+
`
87+
})
88+
export class TestDefaultItemTemplateComponent {
89+
public myItems: Array<DataItem>;
90+
constructor () {
91+
this.myItems = new Array<DataItem>();
92+
for (let i = 0; i < 100; i++) {
93+
this.myItems.push(new DataItem(i, "Name " + i));
94+
}
95+
}
96+
@ViewChild("listView") listViewElement: ListViewComponent;
97+
onScrollListViewTo() {
98+
this.listViewElement.nativeElement.scrollToIndex(100);
99+
}
100+
}
101+
79102
describe("ListView-tests", () => {
80103
beforeEach(nsTestBedBeforeEach([
81104
TestListViewComponent,
82105
TestListViewSelectorComponent,
83-
ItemTemplateComponent
106+
ItemTemplateComponent,
107+
TestDefaultItemTemplateComponent
84108
]));
85109
afterEach(nsTestBedAfterEach(false));
86110

@@ -98,4 +122,12 @@ describe("ListView-tests", () => {
98122
assert.deepEqual(testTemplates, { first: 2, second: 1 });
99123
});
100124
}));
125+
126+
it("'defaultTemplate' does not throw when list-view is scrolled", async(() => {
127+
nsTestBedRender(TestDefaultItemTemplateComponent)
128+
.then((fixture: ComponentFixture<TestDefaultItemTemplateComponent>) => {
129+
const component = fixture.componentRef.instance;
130+
assert.doesNotThrow(component.onScrollListViewTo.bind(component));
131+
});
132+
}));
101133
});

0 commit comments

Comments
 (0)