Skip to content

Commit

Permalink
Support extra props and data-source in to whole object source. #176 #177
Browse files Browse the repository at this point in the history
  • Loading branch information
tangbc committed Apr 15, 2020
1 parent 06a52e2 commit ab51381
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 47 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Since the `v2.0` is **not compatible** with `v1.x`, please note before upgrading
npm install vue-virtual-scroll-list --save
```

Root component is:
```vue
<template>
<div>
Expand All @@ -70,26 +71,49 @@ npm install vue-virtual-scroll-list --save
:data-key="'uid'"
:data-sources="items"
:data-component="itemComponent"
:extra-props="{ otherPropValue: otherDataAssginToItemComponet }"
/>
</div>
</template>
<script>
import Item from './Item'
import VirtualList from 'vue-virtual-scroll-list'
export default {
name: 'root',
data () {
return {
itemComponent: Item,
items: [ {uid: 'unique_1'}, {uid: 'unique_2'}, ... ]
items: [{uid: 'unique_1', text: 'abc'}, {uid: 'unique_2', text: 'xyz'}, ...],
otherDataAssginToItemComponet: 'The Progressive JavaScript Framework',
}
},
components: { 'virtual-list': VirtualList }
}
</script>
```

Item component is:
```vue
<template>
<div>{{ source.text }} - {{ otherPropValue }}</div>
</template>
<script>
export default {
name: 'item-component',
props: {
source: { // here is: {uid: 'unique_1', text: 'abc'}
type: Object,
default () {
return {}
}
},
otherPropValue: String // here is: 'The Progressive JavaScript Framework'
}
}
</script>
```

More usages or getting start you can refer to these clearly [examples](https://tangbc.github.com/vue-virtual-scroll-list).


Expand All @@ -103,12 +127,13 @@ More usages or getting start you can refer to these clearly [examples](https://t
| `keeps` | Number | How many items you are expecting the list to keep rendering in the real dom. |
| `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. |
| `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. |
| `data-component` | Component | The render item component created / declared by vue, and it will use the data object in `datas-sources` as render props. |
| `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`. |

### Optional props

| **&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** |
|--------------------|----------|-------------|------------------------------------------------------------------------|
|--------------------|----------|----------|---------------------------------------------------------------------------|
| `extra-props` | Object | {} | Extra props assign to item component. |
| `root-tag` | String | div | Root element tag name. |
| `wrap-tag` | String | div | List wrapper element tag name. |
| `item-tag` | String | div | Item wrapper element tag name. |
Expand Down
68 changes: 60 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,55 @@
return Constructor;
}

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}

return obj;
}

function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);

if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}

return keys;
}

function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}

return target;
}

/**
* virtual list core calculating center.
*/
Expand Down Expand Up @@ -382,6 +431,9 @@
type: Object,
require: true
},
extraProps: {
type: Object
},
rootTag: {
type: String,
"default": 'div'
Expand Down Expand Up @@ -462,14 +514,17 @@
},
uniqueKey: {
type: String
},
extraProps: {
type: Object
}
};
var SlotProps = {
event: {
type: String
},
uniqueKey: {
String: String
type: String
},
tag: {
type: String
Expand All @@ -479,10 +534,6 @@
}
};

/**
* item and slot component both use similar wrapper
* we need to know their size change at any time.
*/
var Wrapper = {
created: function created() {
this.hasInitial = false;
Expand Down Expand Up @@ -530,9 +581,9 @@
return h(this.tag, {
role: 'item'
}, [h(this.component, {
props: {
data: this.source
}
props: _objectSpread2({}, this.extraProps, {
source: this.source
})
})]);
}
}); // wrapping for slot.
Expand Down Expand Up @@ -695,6 +746,7 @@
horizontal: this.isHorizontal,
uniqueKey: dataSource[this.dataKey],
source: dataSource,
extraProps: this.extraProps,
component: this.dataComponent
}
}));
Expand Down
15 changes: 9 additions & 6 deletions example/src/views/dynamic-size/Item.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div class="item-inner">
<div class="head">
<span># {{ index }}</span>
<span>{{ name }}</span>
<span># {{ source.index }}</span>
<span>{{ source.name }}</span>
</div>
<div class="desc">{{ desc }}</div>
<div class="desc">{{ source.desc }}</div>
</div>
</template>

Expand All @@ -14,9 +14,12 @@ export default {
name: 'dynamic-size-item',
props: {
index: Number,
name: String,
desc: String
source: {
type: Object,
default () {
return {}
}
}
}
}
</script>
Expand Down
12 changes: 8 additions & 4 deletions example/src/views/fixed-size/Item.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="item-inner">
<span># {{ index }}</span>
<span>{{ name }}</span>
<span># {{ source.index }}</span>
<span>{{ source.name }}</span>
</div>
</template>

Expand All @@ -11,8 +11,12 @@ export default {
name: 'fix-size-item',
props: {
index: Number,
name: String
source: {
type: Object,
default () {
return {}
}
}
}
}
</script>
Expand Down
15 changes: 9 additions & 6 deletions example/src/views/horizontal/Item.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="item-inner" v-bind:style="{width: size + 'px'}">
<div class="index"># {{ index }}</div>
<div class="size">{{ size }}</div>
<div class="item-inner" v-bind:style="{width: source.size + 'px'}">
<div class="index"># {{ source.index }}</div>
<div class="size">{{ source.size }}</div>
</div>
</template>

Expand All @@ -11,9 +11,12 @@ export default {
name: 'horizontal-item',
props: {
index: Number,
name: String,
size: Number
source: {
type: Object,
default () {
return {}
}
}
}
}
</script>
Expand Down
21 changes: 12 additions & 9 deletions example/src/views/infinite-loading/Item.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div class="item-inner">
<div class="head">
<span class="index"># {{ index }}</span>
<span class="name">{{ name }}</span>
<span class="index"># {{ source.index }}</span>
<span class="name">{{ source.name }}</span>
</div>
<div class="desc">{{ desc }}</div>
<!-- <div class="desc">{{ source.desc }}</div> -->
</div>
</template>

Expand All @@ -14,18 +14,21 @@ export default {
name: 'infinite-loading-item',
props: {
index: Number,
name: String,
desc: String
source: {
type: Object,
default () {
return {}
}
}
}
}
</script>

<style lang="less" scoped>
.item-inner {
.head {
font-weight: 500;
}
// .head {
// font-weight: 500;
// }
.index {
margin-right: 1em;
}
Expand Down
16 changes: 9 additions & 7 deletions example/src/views/keep-state/Item.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div class="item-inner">
<span class="index"># {{ index }}</span>
<input class="checkbox" v-bind:checked="checked" @change="onChange" type="checkbox" />
<span class="name" @click="onClickName">{{ name }}</span>
<span class="index"># {{ source.index }}</span>
<input class="checkbox" v-bind:checked="source.checked" @change="onChange" type="checkbox" />
<span class="name" @click="onClickName">{{ source.name }}</span>
</div>
</template>

Expand All @@ -15,10 +15,12 @@ export default {
mixins: [mixins],
props: {
id: String,
index: Number,
name: String,
checked: Boolean
source: {
type: Object,
default () {
return {}
}
}
},
methods: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-virtual-scroll-list",
"version": "2.0.2",
"version": "2.0.3",
"description": "A vue component support big amount data list with high scroll performance.",
"main": "dist/index.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const VirtualList = Vue.component(NAME, {
horizontal: this.isHorizontal,
uniqueKey: dataSource[this.dataKey],
source: dataSource,
extraProps: this.extraProps,
component: this.dataComponent
}
}))
Expand Down
5 changes: 4 additions & 1 deletion src/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const Item = Vue.component('virtual-list-item', {
return h(this.tag, {
role: 'item'
}, [h(this.component, {
props: { data: this.source }
props: {
...this.extraProps,
source: this.source
}
})])
}
})
Expand Down
Loading

0 comments on commit ab51381

Please sign in to comment.