Skip to content

Commit a176aca

Browse files
author
Guillaume Chau
committed
feat(DynamicScroller): support simple arrays, closes #108
1 parent 368f0fe commit a176aca

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

src/components/DynamicScroller.vue

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
ref="scroller"
44
:items="itemsWithHeight"
55
:min-item-height="minItemHeight"
6+
:key-field="simpleArray ? null : keyField"
67
v-bind="$attrs"
78
@resize="onScrollerResize"
89
@visible="onScrollerVisible"
@@ -29,6 +30,7 @@
2930

3031
<script>
3132
import RecycleScroller from './RecycleScroller.vue'
33+
import { props, simpleArray } from './common'
3234
3335
export default {
3436
name: 'DynamicScroller',
@@ -47,20 +49,12 @@ export default {
4749
},
4850
4951
props: {
50-
items: {
51-
type: Array,
52-
required: true,
53-
},
52+
...props,
5453
5554
minItemHeight: {
5655
type: [Number, String],
5756
required: true,
5857
},
59-
60-
keyField: {
61-
type: String,
62-
default: 'id',
63-
},
6458
},
6559
6660
data () {
@@ -69,19 +63,21 @@ export default {
6963
active: true,
7064
heights: {},
7165
keyField: this.keyField,
66+
simpleArray: false,
7267
},
7368
}
7469
},
7570
7671
computed: {
72+
simpleArray,
73+
7774
itemsWithHeight () {
7875
const result = []
79-
const items = this.items
80-
const keyField = this.keyField
76+
const { items, keyField, simpleArray } = this
8177
const heights = this.vscrollData.heights
8278
for (let i = 0; i < items.length; i++) {
8379
const item = items[i]
84-
const id = item[keyField]
80+
const id = simpleArray ? i : item[keyField]
8581
let height = heights[id]
8682
if (typeof height === 'undefined' && !this.$_undefinedMap[id]) {
8783
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
@@ -114,6 +110,13 @@ export default {
114110
items () {
115111
this.forceUpdate(false)
116112
},
113+
114+
simpleArray: {
115+
handler (value) {
116+
this.vscrollData.simpleArray = value
117+
},
118+
immediate: true,
119+
},
117120
},
118121
119122
created () {
@@ -151,7 +154,7 @@ export default {
151154
},
152155
153156
forceUpdate (clear = true) {
154-
if (clear) this.vscrollData.heights = {}
157+
if (clear || this.simpleArray) this.vscrollData.heights = {}
155158
this.$emit('vscroll:update', { force: true })
156159
},
157160
@@ -164,8 +167,8 @@ export default {
164167
if (scroller) scroller.scrollToItem(index)
165168
},
166169
167-
getItemSize (item) {
168-
const id = item[this.keyField]
170+
getItemSize (item, index = undefined) {
171+
const id = this.simpleArray ? (index != null ? index : this.items.indexOf(item)) : item[this.keyField]
169172
return this.vscrollData.heights[id] || 0
170173
},
171174

src/components/DynamicScrollerItem.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export default {
99
1010
props: {
1111
item: {
12-
type: Object,
1312
required: true,
1413
},
1514
@@ -23,6 +22,11 @@ export default {
2322
required: true,
2423
},
2524
25+
index: {
26+
type: Number,
27+
default: undefined,
28+
},
29+
2630
sizeDependencies: {
2731
type: [Array, Object],
2832
default: null,
@@ -41,7 +45,7 @@ export default {
4145
4246
computed: {
4347
id () {
44-
return this.item[this.vscrollData.keyField]
48+
return this.vscrollData.simpleArray ? this.index : this.item[this.vscrollData.keyField]
4549
},
4650
4751
height () {

src/components/RecycleScroller.vue

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { ResizeObserver } from 'vue-resize'
4747
import { ObserveVisibility } from 'vue-observe-visibility'
4848
import ScrollParent from 'scrollparent'
4949
import config from '../config'
50+
import { props, simpleArray } from './common'
5051
import { supportsPassive } from '../utils'
5152
5253
let uid = 0
@@ -63,10 +64,7 @@ export default {
6364
},
6465
6566
props: {
66-
items: {
67-
type: Array,
68-
required: true,
69-
},
67+
...props,
7068
7169
itemHeight: {
7270
type: Number,
@@ -88,11 +86,6 @@ export default {
8886
default: 'type',
8987
},
9088
91-
keyField: {
92-
type: String,
93-
default: 'id',
94-
},
95-
9689
buffer: {
9790
type: Number,
9891
default: 200,
@@ -143,6 +136,8 @@ export default {
143136
}
144137
return []
145138
},
139+
140+
simpleArray,
146141
},
147142
148143
watch: {
@@ -262,7 +257,7 @@ export default {
262257
updateVisibleItems (checkItem) {
263258
const itemHeight = this.itemHeight
264259
const typeField = this.typeField
265-
const keyField = this.keyField
260+
const keyField = this.simpleArray ? null : this.keyField
266261
const items = this.items
267262
const count = items.length
268263
const heights = this.heights

src/components/common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const props = {
2+
items: {
3+
type: Array,
4+
required: true,
5+
},
6+
7+
keyField: {
8+
type: String,
9+
default: 'id',
10+
},
11+
}
12+
13+
export function simpleArray () {
14+
return this.items.length && typeof this.items[0] !== 'object'
15+
}

0 commit comments

Comments
 (0)