Skip to content

Commit 5011662

Browse files
authored
fix: vue3 childs not passed appContext
1 parent db412a4 commit 5011662

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/collectionview/vue3/component.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
import { CollectionView as NSCollectionView } from '..';
23
import { ItemEventData, Observable, ObservableArray } from '@nativescript/core';
3-
import { ELEMENT_REF, NSVElement, NSVRoot, PropType, VNode, computed, defineComponent, getCurrentInstance, h, ref, render, toRaw, warn, watch } from 'nativescript-vue';
4+
import { NSVRoot, PropType, VNode, defineComponent, getCurrentInstance, AppContext, h, ref, render, toRaw, watch } from 'nativescript-vue';
45

56
interface ListItem {
67
[key: string]: any;
@@ -27,6 +28,15 @@ function getItemCtx(item: any, index: number, itemAlias: string, indexAlias: str
2728
};
2829
}
2930

31+
function propagateAppContext(vnode: VNode, appContext?: AppContext) {
32+
if (!vnode || !appContext) return;
33+
34+
vnode.appContext = appContext;
35+
if (Array.isArray(vnode.children)) {
36+
vnode.children.forEach((child) => propagateAppContext(child as VNode, appContext));
37+
}
38+
}
39+
3040
export const CollectionView = defineComponent({
3141
props: {
3242
items: {
@@ -74,7 +84,7 @@ export const CollectionView = defineComponent({
7484
function onItemLoading(event: any & ItemEventData) {
7585
const index = event.index;
7686
const id = event.view?.[LIST_CELL_ID] ?? `${cellId++}`;
77-
const item = defineComponent(event.bindingContext, vm);
87+
const item = defineComponent(event.bindingContext);
7888

7989
const itemCtx = getItemCtx(item, index, props.alias, props.itemIdGenerator);
8090
// const itemCtx: ListItem = getItemCtx(props.items instanceof ObservableArray ? props.items.getItem(index) : props.items[index], index, props.alias, props.itemIdGenerator);
@@ -91,7 +101,8 @@ export const CollectionView = defineComponent({
91101

92102
// find the vnode rendering this cell
93103
const container = event.view?.[LIST_CELL_CONTAINER] ?? new NSVRoot();
94-
const vnode = ctx.slots[slotName]?.(itemCtx)[0];
104+
const vnode = ctx.slots[slotName]?.(itemCtx)[0] as VNode;
105+
propagateAppContext(vnode, vm?.appContext);
95106

96107
if (event.view) {
97108
event.view._batchUpdate(() => {
@@ -109,34 +120,13 @@ export const CollectionView = defineComponent({
109120
event.view = cellEl;
110121
}
111122

112-
// render all realized templates as children
113-
// const cellVNODES = () =>
114-
// Object.entries(cells.value).map(([id, entry]) => {
115-
// const vnodes: VNode[] = ctx.slots[entry.slotName]?.(entry.itemCtx) ?? [
116-
// // default template is just a label
117-
// h('Label', {
118-
// text: entry.itemCtx[props.alias]
119-
// })
120-
// ];
121-
122-
// if (vnodes.length > 1) {
123-
// warn(`ListView template must contain a single root element. Found: ${vnodes.length}. Only the first one will be used.`);
124-
// }
125-
126-
// const vnode: VNode = vnodes[0];
127-
// // set the key to the list cell id, so we can find this cell later...
128-
// vnode.key = id;
129-
130-
// return vnode;
131-
// });
132-
133123
return () =>
134124
h('NativeCollectionView', {
135125
ref: collectionView,
136126
items: props.items,
137127
itemTemplates,
138128
onItemLoading,
139-
itemTemplateSelector: (item, index, items) => getSlotName(item, index, items)
129+
itemTemplateSelector: (item: any, index: number, items: ListItem[]) => getSlotName(item, index, items)
140130
});
141131
}
142132
});

0 commit comments

Comments
 (0)