Skip to content

Commit ab51381

Browse files
committed
Support extra props and data-source in to whole object source. #176 #177
1 parent 06a52e2 commit ab51381

File tree

11 files changed

+149
-47
lines changed

11 files changed

+149
-47
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Since the `v2.0` is **not compatible** with `v1.x`, please note before upgrading
6161
npm install vue-virtual-scroll-list --save
6262
```
6363

64+
Root component is:
6465
```vue
6566
<template>
6667
<div>
@@ -70,26 +71,49 @@ npm install vue-virtual-scroll-list --save
7071
:data-key="'uid'"
7172
:data-sources="items"
7273
:data-component="itemComponent"
74+
:extra-props="{ otherPropValue: otherDataAssginToItemComponet }"
7375
/>
7476
</div>
7577
</template>
76-
7778
<script>
7879
import Item from './Item'
7980
import VirtualList from 'vue-virtual-scroll-list'
8081
8182
export default {
83+
name: 'root',
8284
data () {
8385
return {
8486
itemComponent: Item,
85-
items: [ {uid: 'unique_1'}, {uid: 'unique_2'}, ... ]
87+
items: [{uid: 'unique_1', text: 'abc'}, {uid: 'unique_2', text: 'xyz'}, ...],
88+
otherDataAssginToItemComponet: 'The Progressive JavaScript Framework',
8689
}
8790
},
8891
components: { 'virtual-list': VirtualList }
8992
}
9093
</script>
9194
```
9295

96+
Item component is:
97+
```vue
98+
<template>
99+
<div>{{ source.text }} - {{ otherPropValue }}</div>
100+
</template>
101+
<script>
102+
export default {
103+
name: 'item-component',
104+
props: {
105+
source: { // here is: {uid: 'unique_1', text: 'abc'}
106+
type: Object,
107+
default () {
108+
return {}
109+
}
110+
},
111+
otherPropValue: String // here is: 'The Progressive JavaScript Framework'
112+
}
113+
}
114+
</script>
115+
```
116+
93117
More usages or getting start you can refer to these clearly [examples](https://tangbc.github.com/vue-virtual-scroll-list).
94118

95119

@@ -103,12 +127,13 @@ More usages or getting start you can refer to these clearly [examples](https://t
103127
| `keeps` | Number | How many items you are expecting the list to keep rendering in the real dom. |
104128
| `data-key` | String | The unique key get from `data-sources` in each data object, its value **must be unique** in `data-sources`, it is used for identifying item size. |
105129
| `data-sources` | Array[Object] | The source array built for list, each array data must be an object and has an unique key for `data-key` property. |
106-
| `data-component` | Component | The render item component created / declared by vue, and it will use the data object in `datas-sources` as render props. |
130+
| `data-component` | Component | The render item component created / declared by vue, and it will use the data object in `datas-sources` as render prop and named: `source`. |
107131

108132
### Optional props
109133

110134
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Prop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** | **Type** | **Default** | **Description** |
111-
|--------------------|----------|-------------|------------------------------------------------------------------------|
135+
|--------------------|----------|----------|---------------------------------------------------------------------------|
136+
| `extra-props` | Object | {} | Extra props assign to item component. |
112137
| `root-tag` | String | div | Root element tag name. |
113138
| `wrap-tag` | String | div | List wrapper element tag name. |
114139
| `item-tag` | String | div | Item wrapper element tag name. |

dist/index.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,55 @@
3434
return Constructor;
3535
}
3636

37+
function _defineProperty(obj, key, value) {
38+
if (key in obj) {
39+
Object.defineProperty(obj, key, {
40+
value: value,
41+
enumerable: true,
42+
configurable: true,
43+
writable: true
44+
});
45+
} else {
46+
obj[key] = value;
47+
}
48+
49+
return obj;
50+
}
51+
52+
function ownKeys(object, enumerableOnly) {
53+
var keys = Object.keys(object);
54+
55+
if (Object.getOwnPropertySymbols) {
56+
var symbols = Object.getOwnPropertySymbols(object);
57+
if (enumerableOnly) symbols = symbols.filter(function (sym) {
58+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
59+
});
60+
keys.push.apply(keys, symbols);
61+
}
62+
63+
return keys;
64+
}
65+
66+
function _objectSpread2(target) {
67+
for (var i = 1; i < arguments.length; i++) {
68+
var source = arguments[i] != null ? arguments[i] : {};
69+
70+
if (i % 2) {
71+
ownKeys(Object(source), true).forEach(function (key) {
72+
_defineProperty(target, key, source[key]);
73+
});
74+
} else if (Object.getOwnPropertyDescriptors) {
75+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
76+
} else {
77+
ownKeys(Object(source)).forEach(function (key) {
78+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
79+
});
80+
}
81+
}
82+
83+
return target;
84+
}
85+
3786
/**
3887
* virtual list core calculating center.
3988
*/
@@ -382,6 +431,9 @@
382431
type: Object,
383432
require: true
384433
},
434+
extraProps: {
435+
type: Object
436+
},
385437
rootTag: {
386438
type: String,
387439
"default": 'div'
@@ -462,14 +514,17 @@
462514
},
463515
uniqueKey: {
464516
type: String
517+
},
518+
extraProps: {
519+
type: Object
465520
}
466521
};
467522
var SlotProps = {
468523
event: {
469524
type: String
470525
},
471526
uniqueKey: {
472-
String: String
527+
type: String
473528
},
474529
tag: {
475530
type: String
@@ -479,10 +534,6 @@
479534
}
480535
};
481536

482-
/**
483-
* item and slot component both use similar wrapper
484-
* we need to know their size change at any time.
485-
*/
486537
var Wrapper = {
487538
created: function created() {
488539
this.hasInitial = false;
@@ -530,9 +581,9 @@
530581
return h(this.tag, {
531582
role: 'item'
532583
}, [h(this.component, {
533-
props: {
534-
data: this.source
535-
}
584+
props: _objectSpread2({}, this.extraProps, {
585+
source: this.source
586+
})
536587
})]);
537588
}
538589
}); // wrapping for slot.
@@ -695,6 +746,7 @@
695746
horizontal: this.isHorizontal,
696747
uniqueKey: dataSource[this.dataKey],
697748
source: dataSource,
749+
extraProps: this.extraProps,
698750
component: this.dataComponent
699751
}
700752
}));

example/src/views/dynamic-size/Item.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div class="item-inner">
33
<div class="head">
4-
<span># {{ index }}</span>
5-
<span>{{ name }}</span>
4+
<span># {{ source.index }}</span>
5+
<span>{{ source.name }}</span>
66
</div>
7-
<div class="desc">{{ desc }}</div>
7+
<div class="desc">{{ source.desc }}</div>
88
</div>
99
</template>
1010

@@ -14,9 +14,12 @@ export default {
1414
name: 'dynamic-size-item',
1515
1616
props: {
17-
index: Number,
18-
name: String,
19-
desc: String
17+
source: {
18+
type: Object,
19+
default () {
20+
return {}
21+
}
22+
}
2023
}
2124
}
2225
</script>

example/src/views/fixed-size/Item.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="item-inner">
3-
<span># {{ index }}</span>
4-
<span>{{ name }}</span>
3+
<span># {{ source.index }}</span>
4+
<span>{{ source.name }}</span>
55
</div>
66
</template>
77

@@ -11,8 +11,12 @@ export default {
1111
name: 'fix-size-item',
1212
1313
props: {
14-
index: Number,
15-
name: String
14+
source: {
15+
type: Object,
16+
default () {
17+
return {}
18+
}
19+
}
1620
}
1721
}
1822
</script>

example/src/views/horizontal/Item.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<div class="item-inner" v-bind:style="{width: size + 'px'}">
3-
<div class="index"># {{ index }}</div>
4-
<div class="size">{{ size }}</div>
2+
<div class="item-inner" v-bind:style="{width: source.size + 'px'}">
3+
<div class="index"># {{ source.index }}</div>
4+
<div class="size">{{ source.size }}</div>
55
</div>
66
</template>
77

@@ -11,9 +11,12 @@ export default {
1111
name: 'horizontal-item',
1212
1313
props: {
14-
index: Number,
15-
name: String,
16-
size: Number
14+
source: {
15+
type: Object,
16+
default () {
17+
return {}
18+
}
19+
}
1720
}
1821
}
1922
</script>

example/src/views/infinite-loading/Item.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div class="item-inner">
33
<div class="head">
4-
<span class="index"># {{ index }}</span>
5-
<span class="name">{{ name }}</span>
4+
<span class="index"># {{ source.index }}</span>
5+
<span class="name">{{ source.name }}</span>
66
</div>
7-
<div class="desc">{{ desc }}</div>
7+
<!-- <div class="desc">{{ source.desc }}</div> -->
88
</div>
99
</template>
1010

@@ -14,18 +14,21 @@ export default {
1414
name: 'infinite-loading-item',
1515
1616
props: {
17-
index: Number,
18-
name: String,
19-
desc: String
17+
source: {
18+
type: Object,
19+
default () {
20+
return {}
21+
}
22+
}
2023
}
2124
}
2225
</script>
2326

2427
<style lang="less" scoped>
2528
.item-inner {
26-
.head {
27-
font-weight: 500;
28-
}
29+
// .head {
30+
// font-weight: 500;
31+
// }
2932
.index {
3033
margin-right: 1em;
3134
}

example/src/views/keep-state/Item.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<div class="item-inner">
3-
<span class="index"># {{ index }}</span>
4-
<input class="checkbox" v-bind:checked="checked" @change="onChange" type="checkbox" />
5-
<span class="name" @click="onClickName">{{ name }}</span>
3+
<span class="index"># {{ source.index }}</span>
4+
<input class="checkbox" v-bind:checked="source.checked" @change="onChange" type="checkbox" />
5+
<span class="name" @click="onClickName">{{ source.name }}</span>
66
</div>
77
</template>
88

@@ -15,10 +15,12 @@ export default {
1515
mixins: [mixins],
1616
1717
props: {
18-
id: String,
19-
index: Number,
20-
name: String,
21-
checked: Boolean
18+
source: {
19+
type: Object,
20+
default () {
21+
return {}
22+
}
23+
}
2224
},
2325
2426
methods: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-virtual-scroll-list",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "A vue component support big amount data list with high scroll performance.",
55
"main": "dist/index.js",
66
"files": [

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ const VirtualList = Vue.component(NAME, {
159159
horizontal: this.isHorizontal,
160160
uniqueKey: dataSource[this.dataKey],
161161
source: dataSource,
162+
extraProps: this.extraProps,
162163
component: this.dataComponent
163164
}
164165
}))

src/item.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export const Item = Vue.component('virtual-list-item', {
5858
return h(this.tag, {
5959
role: 'item'
6060
}, [h(this.component, {
61-
props: { data: this.source }
61+
props: {
62+
...this.extraProps,
63+
source: this.source
64+
}
6265
})])
6366
}
6467
})

0 commit comments

Comments
 (0)