Skip to content

Commit

Permalink
feat(ListView): emit itemTap with 'item'
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 committed Apr 25, 2020
1 parent 0e91662 commit 4976309
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
39 changes: 32 additions & 7 deletions apps/test/app/ListViewComp.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import { ListView } from 'nativescript-vue'

const randomText = () => {
let length = Math.floor(Math.random() * 200) + 20
let result = ''
let characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let charactersLength = characters.length
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength))
}
return result
}

export default {
components: {
ListView,
},
template: `
<ListView :items="items" _tap="items.push('Item ' + (items.length + 1))">
<ListView :items="items2" @itemTap="onItemTap">
<template v-slot:default="{item, index, even, odd}">
<Label textWrap="true">
Item: {{ item }}
Index: {{ index }}
Even: {{ even }}
Odd: {{ odd }}
</Label>
<GridLayout columns="*, auto">
<Label textWrap="true">
{{ index }} - {{ item }}
</Label>
<Button col="1" :text="'tap ' + index" @tap="onTestTap(item, index, even, odd)"/>
</GridLayout>
</template>
<!--<template v-slot:other="{item}">-->
<!-- <Label >{{ item }}Other</Label>-->
<!--</template>-->
</ListView>`,
data() {
return {
items2: [
...Array(200)
.fill('')
.map((item, index) => `Item ${index}\n${randomText()}`),
],
items: [
'Item 1',
'Item 2',
Expand All @@ -43,4 +60,12 @@ export default {
],
}
},
methods: {
onItemTap(event) {
console.log(`Tapped ${event.item}`)
},
onTestTap(item, index, even, odd) {
console.log(`Test tap ${index}! (${item}, ${index}, ${even}, ${odd})`)
},
},
}
22 changes: 21 additions & 1 deletion packages/runtime/src/components/ListView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ type ListViewProps = {
const NSV_LVR_REF = Symbol('NSV_LVR_REF')
const isListViewThingSymbol = Symbol()

export type ItemTapEvent<T> = ItemEventData & { item: T }

interface ListViewRealized {
[isListViewThingSymbol]: true
container: NSVRoot | null
}

export const ListView: FunctionalComponent<ListViewProps> = (props, ctx) => {
export const ListView: FunctionalComponent<
ListViewProps,
{
itemTap: (e: ItemTapEvent<any>) => any
}
> = (props, ctx) => {
const defaultKeyedTemplate = {
key: 'test',
createView(): ListViewRealized {
Expand All @@ -28,6 +35,12 @@ export const ListView: FunctionalComponent<ListViewProps> = (props, ctx) => {

return h('InternalListView', {
items: props.items,
onitemTap: (args: ItemEventData) => {
ctx.emit('itemTap', {
...args,
item: props.items[args.index],
})
},
itemTemplates: [defaultKeyedTemplate],
itemTemplateSelector: (item: any, index: any) => {
// debug('itemTemplateSelector', item, index)
Expand All @@ -53,6 +66,10 @@ export const ListView: FunctionalComponent<ListViewProps> = (props, ctx) => {
const lvr = (args.view as unknown) as ListViewRealized
// lets create an initial root
lvr.container = nodeOps.createRoot()

// @ts-ignore
// console.log(getSlotVnode('default').children[0].children)

render(getSlotVnode('default'), lvr.container)

if (lvr.container.el) {
Expand All @@ -62,6 +79,9 @@ export const ListView: FunctionalComponent<ListViewProps> = (props, ctx) => {
} else {
debug('LVR UPDATING' + args.index, 'ListView')
const lvr = (args.view as any)[NSV_LVR_REF] as ListViewRealized
// @ts-ignore
// console.log(getSlotVnode('default').children[0].children)

render(getSlotVnode('default'), lvr.container!)
view = lvr.container!.el!.nativeView
}
Expand Down

0 comments on commit 4976309

Please sign in to comment.