react-pullLoad

React 版本的 pullLoad 下拉更新 上拉加載更多 組件

pullLoad 非 react 版本,支持 require.js 模塊化調用

示例

demo1 document.body 作為容器

demo2 ReactPullLoad 根節點 DOM 作為容器

demo3 document.body 作為容器 且自定義刷新和加載更多 UI 組件

當前版本 1.0.0

簡介

  1. 只依賴 react/react-dom
  2. 樣式使用 less 編寫
  3. 支持 body 或者組件根 DOM 固定高度作為外部容器 contianer(即可視區域大小)
  4. 觸摸事件綁定在內容塊 content(即高度為 auto 的DOM )
  5. 純 React 組件方式開發的
  6. 支持自定義刷新和加載更多 UI 組件
  7. 支持代碼動態調起刷新和加載更多(組件將展示刷新和加載更多樣式)
  8. 只支持移動觸摸設備

功能點

  1. 下拉距離大於閾值觸發刷新動作
  2. 滾動到距底部距離小於閾值加載更多
  3. 支持自定義刷新和加載更多 UI 組件

使用説明

npm install --save react-pullload1.  
import ReactPullLoad,{ STATS } from 'react-pullload'
2.   
3.  
export class App extends Component{
4.  
constructor(){
5.  
super();
6.  
this.state ={
7.  
hasMore: true,
8.  
data: cData,
9.  
action: STATS.init,
10.  
index: loadMoreLimitNum //loading more test time limit
11.  
}
12.  
}
13.   
14.  
handleAction = (action) => {
15.  
console.info(action, this.state.action,action === this.state.action);
16.  
//new action must do not equel to old action
17.  
if(action === this.state.action){
18.  
return false
19.  
}
20.   
21.  
if(action === STATS.refreshing){//刷新
22.  
this.handRefreshing();
23.  
} else if(action === STATS.loading){//加載更多
24.  
this.handLoadMore();
25.  
} else{
26.  
//DO NOT modify below code
27.  
this.setState({
28.  
action: action
29.  
})
30.  
}
31.  
}
32.   
33.  
handRefreshing = () =>{
34.  
if(STATS.refreshing === this.state.action){
35.  
return false
36.  
}
37.   
38.  
setTimeout(()=>{
39.  
//refreshing complete
40.  
this.setState({
41.  
data: cData,
42.  
hasMore: true,
43.  
action: STATS.refreshed,
44.  
index: loadMoreLimitNum
45.  
});
46.  
}, 3000)
47.   
48.  
this.setState({
49.  
action: STATS.refreshing
50.  
})
51.  
}
52.   
53.  
handLoadMore = () => {
54.  
if(STATS.loading === this.state.action){
55.  
return false
56.  
}
57.   
58.  
setTimeout(()=>{
59.  
if(this.state.index === 0){
60.  
this.setState({
61.  
action: STATS.reset,
62.  
hasMore: false
63.  
});
64.  
} else{
65.  
this.setState({
66.  
data: [...this.state.data, cData[0], cData[0]],
67.  
action: STATS.reset,
68.  
index: this.state.index - 1
69.  
});
70.  
}
71.  
}, 3000)
72.   
73.  
this.setState({
74.  
action: STATS.loading
75.  
})
76.  
}
77.   
78.  
render(){
79.  
const {
80.  
data,
81.  
hasMore
82.  
} = this.state
83.   
84.  
const fixHeaderStyle = {
85.  
position: "fixed",
86.  
width: "100%",
87.  
height: "50px",
88.  
color: "#fff",
89.  
lineHeight: "50px",
90.  
backgroundColor: "#e24f37",
91.  
left: 0,
92.  
top: 0,
93.  
textAlign: "center",
94.  
zIndex: 1
95.  
}
96.   
97.  
return (
98.  
<div>
99.  
<div style={fixHeaderStyle}>
100.  
fixed header
101.  
</div>
102.  
<ReactPullLoad 
103.  
downEnough={150}
104.  
action={this.state.action}
105.  
handleAction={this.handleAction}
106.  
hasMore={hasMore}
107.  
style={{paddingTop: 50}}
108.  
distanceBottom={1000}>
109.  
<ul className="test-ul">
110.  
<button onClick={this.handRefreshing}>refreshing</button>
111.  
<button onClick={this.handLoadMore}>loading more</button>
112.  
{
113.  
data.map( (str, index )=>{
114.  
return <li key={index}><img src={str} alt=""/></li>
115.  
})
116.  
}
117.  
</ul>
118.  
</ReactPullLoad>
119.  
</div>
120.  
)
121.  
}
122.  
}

參數説明:

參數

説明

類型

默認值

備註

action

用於同步狀態

string

 

isRequired

handleAction

用於處理狀態

func

 

isRequired

hasMore

是否還有更多內容可加載

bool

false

在 onLoadMore reject() 之後設置

downEnough

下拉距離是否滿足要求

num

100

 

distanceBottom

距離底部距離觸發加載更多

num

100

 

isBlockContainer

是否開啓使用組件根 DOM 作為外部容器 contianer

bool

false

 

HeadNode

自定義頂部刷新 UI 組件

any

ReactPullLoad HeadNode

必須是一個 React 組件

FooterNode

自定義底部加載更多 UI 組件

any

ReactPullLoad FooterNode

必須是一個 React 組件

另外 ReactPullLoad 組件支持根屬性擴展 例如: classNamestyle 等等

STATS list

屬性


根節點 className

説明

init

''

 

組件初始狀態

pulling

'pulling'

state-pulling

下拉狀態

enough

'pulling enough'

state-pulling.enough

下拉並且已經滿足閾值

refreshing

'refreshing'

state-refreshing

刷新中(加載數據中)

refreshed

'refreshed'

state-refreshed

完成刷新動作

reset

'reset'

state-reset

恢復默認狀態

loading

'loading'

state-loading

加載中

init/reset -> pulling -> enough -> refreshing -> refreshed -> reset

init/reset -> pulling -> reset

init/reset -> loading -> reset

自定義刷新及加載組件

請參考默認刷新及加載組件源碼(通過 css 根節點不同 className 設置對應 UI 樣式來實現)

ReactPullLoad HeadNode

ReactPullLoad FooterNode

或參考 demo3 中的實現方式在組件內容通過獲取的 loaderState 與 STATS 不同狀態對比來現實