Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f38b8c

Browse files
aisintangbc
authored andcommitted
Extend the type of dataKey to support function
1 parent a71fedb commit 7f38b8c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ const VirtualList = Vue.component('virtual-list', {
205205
},
206206

207207
getUniqueIdFromDataSources () {
208-
return this.dataSources.map((dataSource) => dataSource[this.dataKey])
208+
return this.dataSources.map((dataSource) => {
209+
return typeof this.dataKey === 'function' ? this.dataKey(dataSource) : dataSource[this.dataKey]
210+
})
209211
},
210212

211213
// event called when each item mounted or size changed
@@ -267,15 +269,16 @@ const VirtualList = Vue.component('virtual-list', {
267269
for (let index = start; index <= end; index++) {
268270
const dataSource = dataSources[index]
269271
if (dataSource) {
270-
if (Object.prototype.hasOwnProperty.call(dataSource, dataKey)) {
272+
const uniqueKey = typeof dataKey === 'function' ? dataKey(dataSource) : dataSource[dataKey]
273+
if (uniqueKey) {
271274
slots.push(h(Item, {
272275
// key: dataSource[dataKey],
273276
props: {
274277
index,
275278
tag: itemTag,
276279
event: EVENT_TYPE.ITEM,
277280
horizontal: isHorizontal,
278-
uniqueKey: dataSource[dataKey],
281+
uniqueKey: uniqueKey,
279282
source: dataSource,
280283
extraProps: extraProps,
281284
component: dataComponent,

src/props.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
export const VirtualProps = {
66
dataKey: {
7-
type: String,
7+
type: [String, Function],
88
required: true
99
},
1010
dataSources: {

0 commit comments

Comments
 (0)