8000 fix: allow comments in ListView templates · hardstonepaul/nativescript-vue@f3993cf · GitHub
[go: up one dir, main page]

Skip to content

Commit f3993cf

Browse files
committed
fix: allow comments in ListView templates
1 parent 116ab8d commit f3993cf

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

demo/src/components/demo_ListView.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ const onItemTap = useDebounceFn((item: Item) => {
114114

115115
<ListView :items="items2" height="800">
116116
<template #default="{ item, index }: ListItem<Item>">
117+
<!-- TEST -->
118+
<!-- TEST -->
119+
<!-- TEST -->
120+
<!-- TEST -->
117121
<StackLayout
118122
@tap="onItemTap(item)"
119123
:backgroundColor="selected.includes(item) ? '#ffedd5' : ''"

src/components/ListView.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
warn,
77
watch,
88
ref,
9+
Comment,
910
} from '@vue/runtime-core';
1011

1112
import {
@@ -128,20 +129,27 @@ export const ListView = /*#__PURE__*/ defineComponent({
128129
// render all realized templates as children
129130
const cellVNODES = () =>
130131
Object.entries(cells.value).map(([id, entry]) => {
131-
const vnodes: VNode[] = ctx.slots[entry.slotName]?.(entry.itemCtx) ?? [
132-
// default template is just a label
133-
h('Label', {
134-
text: entry.itemCtx.item,
135-
}),
136-
];
137-
138-
if (vnodes.length > 1) {
132+
const vnodes: VNode[] =
133+
ctx.slots[entry.slotName]?.(entry.itemCtx) ?? [];
134+
const nonCommentVnodes = vnodes.filter(
135+
(vnode) => vnode.type !== Comment,
136+
);
137+
138+
if (nonCommentVnodes.length === 0) {
139+
warn(`ListView template must contain at least one element.`);
140+
} else if (nonCommentVnodes.length > 1) {
139141
warn(
140142
`ListView template must contain a single root element. Found: ${vnodes.length}. Only the first one will be used.`,
141143
);
142144
}
143145

144-
const vnode: VNode = vnodes[0];
146+
const vnode: VNode =
147+
nonCommentVnodes.at(0) ??
148+
// default template is just a label
149+
h('Label', {
150+
text: entry.itemCtx.item,
151+
});
152+
145153
// set the key to the list cell id, so we can find this cell later...
146154
vnode.key = id;
147155

0 commit comments

Comments
 (0)
0