This repository has been archived by the owner on May 6, 2019. It is now read-only.
forked from Meituan-Dianping/beeshell
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.tsx
159 lines (143 loc) · 3.09 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from 'react'
import {
View,
Text,
TouchableOpacity,
TouchableHighlight,
Image,
StyleSheet,
TextInput,
ScrollView,
PixelRatio,
FlatList,
RefreshControl,
ActivityIndicator
} from 'react-native'
import { Longlist } from '../../src'
import styles from '../common/styles'
import variables from '../customTheme'
const dataModal = {
total: 30,
list: [
{
id: 1
},
{
id: 2
},
{
id: 3
},
{
id: 4
},
{
id: 5
},
{
id: 6
},
{
id: 7
},
]
}
export default class LonglistScreen extends React.Component<any, any> {
private fetchListTimes: number
private _longlist: any
constructor(props) {
super(props)
this.fetchListTimes = 0
this.state = {
pageNo: 0,
pagesize: 7,
list: [],
total: 0
}
}
componentDidMount () {
setTimeout(() => {
this._longlist && this._longlist.flatList.scrollToIndex({
index: 9
})
}, 10000)
}
fetchList(params) {
const { pageNo, pagesize } = params
return new Promise((resolve) => {
setTimeout(() => {
resolve(dataModal)
}, 2000)
}).catch((e) => {
console.log(e)
})
}
modifyList(list, pageNo) {
return list.map((item, index) => {
return {
...item,
label: `第 ${pageNo} 页,第 ${index + 1} 项`
}
})
}
refreshState(pageNo?: number) {
pageNo = pageNo || (this.state.pageNo + 1)
const params = {
pageNo,
pagesize: this.state.pagesize,
id: '123456',
}
this.fetchListTimes++
const tmpFetchListTimes = this.fetchListTimes
return this.fetchList(params).then((resData: any) => {
// Promise 一旦发起不能终止,通过请求数据的次数,判断请求是否有效
if (tmpFetchListTimes !== this.fetchListTimes) {
return
}
const pageNo = params.pageNo
const { list, total } = resData
const newList = this.modifyList(list, pageNo)
const oldList = (pageNo === 1 || this.state.list == null) ? [] : this.state.list
this.setState({
pageNo,
total,
list: oldList.concat(newList),
})
}).catch((e) => {
console.log(e)
})
}
render() {
const { list, total } = this.state
return (
<View style={styles.body}>
<Longlist
ref={(c) => {
this._longlist = c
}}
total={total}
data={list}
renderItem={({ item, index }) => {
return (
<View
style={{
marginBottom: 12,
paddingVertical: 30,
paddingHorizontal: variables.mtdHSpacingXL,
backgroundColor: '#fff'
}}>
<Text style={{ color: variables.mtdGrayBase }}>{item.label}</Text>
</View>
)
}}
onEndReached={() => {
return this.refreshState()
}}
onRefresh={() => {
return this.refreshState(1)
}}
/>
</View>
)
}
}