|
@@ -1,35 +1,29 @@
|
1
|
|
-import React, { useEffect, useMemo, useRef, useState } from 'react';
|
2
|
|
-import VirtualList from '@tarojs/components/virtual-list';
|
|
1
|
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+import { ScrollView } from '@tarojs/components';
|
3
|
3
|
import Taro from '@tarojs/taro';
|
4
|
4
|
|
5
|
5
|
export default (props) => {
|
6
|
6
|
const {
|
7
|
|
- className,
|
8
|
|
- height,
|
9
|
|
- width = '100%',
|
10
|
|
- itemHeight = 100,
|
11
|
7
|
render,
|
12
|
8
|
request,
|
13
|
9
|
params,
|
14
|
10
|
pageSize = 10,
|
15
|
|
- onError
|
|
11
|
+ onError,
|
|
12
|
+ onDataChange,
|
|
13
|
+ ...leftProps
|
16
|
14
|
} = props
|
17
|
15
|
|
18
|
16
|
const loadingRef = useRef(false)
|
19
|
17
|
const [payload, setPayload] = useState({})
|
20
|
18
|
const [list, setList] = useState([])
|
21
|
|
- const pageRef = useRef({ current: 1, total: 0 })
|
22
|
|
-
|
23
|
|
- const dataLen = list.length
|
|
19
|
+ const pageRef = useRef({ current: 1, pages: 0 })
|
24
|
20
|
|
25
|
21
|
// 滚动
|
26
|
|
- const handleScroll = ({ scrollDirection, scrollOffset }) => {
|
|
22
|
+ const handleScrollToLower = (e) => {
|
27
|
23
|
const loading = loadingRef.current
|
28
|
|
- const isUping = scrollDirection === 'forward'
|
29
|
|
- const canScroll = scrollOffset > ((dataLen - 5) * itemHeight + 100)
|
30
|
|
- const hasMore = pageRef.current.current < pageRef.current.total
|
|
24
|
+ const hasMore = pageRef.current.current < pageRef.current.pages
|
31
|
25
|
|
32
|
|
- if (!loading && isUping && canScroll && hasMore) {
|
|
26
|
+ if (!loading && hasMore) {
|
33
|
27
|
setPayload({
|
34
|
28
|
...payload,
|
35
|
29
|
pageNum: pageRef.current.current + 1
|
|
@@ -41,19 +35,24 @@ export default (props) => {
|
41
|
35
|
useEffect(() => {
|
42
|
36
|
setPayload({
|
43
|
37
|
...params,
|
44
|
|
- pageNum: pageRef.current,
|
|
38
|
+ pageNum: 1,
|
45
|
39
|
pageSize,
|
46
|
40
|
})
|
47
|
41
|
}, [pageSize, params])
|
48
|
42
|
|
49
|
|
-
|
50
|
43
|
// 请求数据
|
51
|
44
|
useEffect(() => {
|
52
|
45
|
Taro.showLoading()
|
53
|
46
|
loadingRef.current = true
|
54
|
47
|
request(payload).then((res) => {
|
55
|
48
|
const { records, ...pageInfo } = res || {}
|
56
|
|
- setList(records || [])
|
|
49
|
+
|
|
50
|
+ const lst = pageInfo.current === 1 ? records || [] : list.concat(records || [])
|
|
51
|
+ setList(lst)
|
|
52
|
+ if (onDataChange) {
|
|
53
|
+ onDataChange(lst)
|
|
54
|
+ }
|
|
55
|
+
|
57
|
56
|
pageRef.current = pageInfo
|
58
|
57
|
loadingRef.current = false
|
59
|
58
|
Taro.hideLoading()
|
|
@@ -67,17 +66,16 @@ export default (props) => {
|
67
|
66
|
})
|
68
|
67
|
}, [payload])
|
69
|
68
|
|
70
|
|
- const Row = useMemo(render, [])
|
71
|
|
-
|
72
|
69
|
return (
|
73
|
|
- <VirtualList
|
74
|
|
- className={className}
|
75
|
|
- width={width}
|
76
|
|
- height={height}
|
77
|
|
- itemData={list}
|
78
|
|
- itemCount={dataLen}
|
79
|
|
- itemSize={itemHeight}
|
80
|
|
- onScroll={handleScroll}
|
81
|
|
- >{Row}</VirtualList>
|
|
70
|
+ <ScrollView
|
|
71
|
+ scrollY
|
|
72
|
+ onScrollToLower={handleScrollToLower}
|
|
73
|
+ {...leftProps}
|
|
74
|
+ >
|
|
75
|
+ { !render
|
|
76
|
+ ? props.children
|
|
77
|
+ : list.map((item, index) => render({ item, index }))
|
|
78
|
+ }
|
|
79
|
+ </ScrollView>
|
82
|
80
|
)
|
83
|
81
|
}
|