Skip to content

Commit 6f986b2

Browse files
authored
feat: add useLoadMore (#28)
* feat: add useLoadMore * refactor: update * refactor: add test
1 parent 3f87a8b commit 6f986b2

File tree

6 files changed

+680
-13
lines changed

6 files changed

+680
-13
lines changed

example/App.tsx

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,87 @@
1-
import { defineComponent } from 'vue';
2-
import { useRequest } from 'vue-request';
1+
import { defineComponent, reactive, watchEffect } from 'vue';
2+
import { useLoadMore } from 'vue-request';
3+
import Mock from 'mockjs';
34

4-
function testService() {
5-
return new Promise<string>(resolve => {
5+
function api(current: number, total = 10) {
6+
let list: string[] = [];
7+
if (current <= total) {
8+
list = Mock.mock({
9+
'list|5': ['@name'],
10+
}).list;
11+
} else {
12+
list = [];
13+
}
14+
return {
15+
current,
16+
total,
17+
list,
18+
};
19+
}
20+
type APIReturnType = ReturnType<typeof api>;
21+
type rawDataType = {
22+
data: APIReturnType;
23+
dataList: APIReturnType['list'];
24+
};
25+
26+
function testService(rawData: rawDataType) {
27+
const current = (rawData?.data?.current || 0) + 1;
28+
console.log(`${current}`, rawData);
29+
30+
return new Promise<APIReturnType>(resolve => {
631
setTimeout(() => {
7-
resolve('success');
32+
resolve(api(current));
833
}, 1000);
934
});
1035
}
1136

1237
export default defineComponent({
1338
name: 'App',
1439
setup() {
15-
const { run, data, loading } = useRequest(testService);
40+
const { loadMore, loadingMore, dataList, noMore, reload } = useLoadMore<
41+
APIReturnType,
42+
any,
43+
APIReturnType['list']
44+
>(testService, {
45+
isNoMore: d => {
46+
return d?.current >= d?.total;
47+
},
48+
});
49+
50+
const testReactive = reactive({ first: 'init' });
51+
52+
watchEffect(() => {
53+
console.log(testReactive);
54+
});
55+
1656
return () => (
1757
<div>
18-
<button onClick={() => run()}>run</button>
58+
<button
59+
disabled={noMore.value}
60+
onClick={() => {
61+
loadMore();
62+
Object.keys(testReactive).forEach(key => {
63+
testReactive[key] = new Date();
64+
});
65+
}}
66+
>
67+
run
68+
</button>
69+
70+
<button
71+
onClick={() => {
72+
reload();
73+
}}
74+
>
75+
reload
76+
</button>
1977
<br />
20-
{loading.value ? 'loading...' : data.value}
78+
{loadingMore.value
79+
? 'loading...'
80+
: dataList.value?.map((i, idx) => (
81+
<div key={idx}>
82+
<h4 style="display: inline-block">{idx}</h4>: {i}
83+
</div>
84+
))}
2185
</div>
2286
);
2387
},

0 commit comments

Comments
 (0)