Skip to content

Commit 40d7180

Browse files
committed
feat(list): fix list review qa
fix Tencent#463
1 parent ddfeab4 commit 40d7180

File tree

7 files changed

+176
-162
lines changed

7 files changed

+176
-162
lines changed
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import React, { useEffect, useState, useRef} from 'react';
1+
import React, { useEffect, useState, useRef } from 'react';
22
import './style/index.less';
33
import { Cell, List } from 'tdesign-mobile-react';
44

5+
interface ListItem {
6+
id: number;
7+
content: string;
8+
icon: string;
9+
title: string;
10+
}
11+
512
export default function ListDemo() {
613
const [isLoading, setIsLoading] = useState(false);
714
const pageSize = 20;
815
const stateRef = useRef([]);
916
const pageRef = useRef(1);
10-
const dataSource = [];
17+
const dataSource: ListItem[] = [];
1118
const total = 100;
1219
for (let i = 0; i < total; i++) {
1320
dataSource.push({
@@ -27,20 +34,20 @@ export default function ListDemo() {
2734
const { pageNum, pageSize } = pageInfo;
2835
const newDataSource = dataSource.slice((pageNum - 1) * pageSize, pageNum * pageSize);
2936
const newListData = stateRef.current.concat(newDataSource);
30-
pageRef.current = pageNum
31-
stateRef.current = newListData
37+
pageRef.current = pageNum;
38+
stateRef.current = newListData;
3239
setIsLoading(false);
3340
}, 0);
3441
} catch (err) {
35-
stateRef.current = []
42+
stateRef.current = [];
3643
}
3744
};
3845

3946
const onScroll = (scrollBottom) => {
4047
if (!scrollBottom && stateRef.current.length < total) {
4148
fetchData({ pageNum: pageRef.current + 1, pageSize });
4249
}
43-
}
50+
};
4451

4552
useEffect(() => {
4653
fetchData({ pageNum: pageRef.current, pageSize });
@@ -49,11 +56,11 @@ export default function ListDemo() {
4956

5057
return (
5158
<List asyncLoading={isLoading} onScroll={onScroll}>
52-
{
53-
stateRef.current.map((item) => <Cell key={item.id} align="middle">
54-
<span className="cell">{item.id}</span>
55-
</Cell>)
56-
}
59+
{stateRef.current.map((item) => (
60+
<Cell key={item.id} align="middle">
61+
<span className="cell">{item.id}</span>
62+
</Cell>
63+
))}
5764
</List>
5865
);
5966
}

src/list/_example/err-tip.jsx

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/list/_example/err-tip.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { useState, useRef, useEffect } from 'react';
2+
import { Cell, List, Loading } from 'tdesign-mobile-react';
3+
4+
export default function ListDemo() {
5+
const listError = useRef<string[]>([]);
6+
const [loading, setLoading] = useState('');
7+
const [showError, setShowError] = useState(false);
8+
9+
const onLoadError = () => {
10+
setLoading('loading');
11+
12+
setTimeout(() => {
13+
const newVal: string[] = [...listError.current];
14+
for (let i = listError.current.length; i < 8; i++) {
15+
newVal.push(`${i}`);
16+
}
17+
listError.current = newVal;
18+
19+
setShowError(true);
20+
setLoading('');
21+
}, 1000);
22+
};
23+
24+
const onLoadMore = () => {
25+
setShowError(false);
26+
if (listError.current.length >= 60 || loading) {
27+
return;
28+
}
29+
setLoading('loading');
30+
31+
setTimeout(() => {
32+
for (let i = 0; i < 15; i++) {
33+
listError.current.push(`${listError.current.length + 1}`);
34+
}
35+
setLoading('');
36+
}, 1000);
37+
};
38+
39+
useEffect(() => {
40+
onLoadError();
41+
}, []);
42+
43+
return (
44+
<List
45+
asyncLoading={loading}
46+
onScroll={onLoadMore}
47+
footer={
48+
showError && (
49+
<Loading indicator={false}>
50+
<div className="custom-error">
51+
请求失败,点击重新<span onClick={onLoadMore}>加载</span>
52+
</div>
53+
</Loading>
54+
)
55+
}
56+
>
57+
{listError.current.map((item) => (
58+
<Cell key={item} align="middle">
59+
<span className="cell">{item}</span>
60+
</Cell>
61+
))}
62+
</List>
63+
);
64+
}

src/list/_example/index.jsx

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/list/_example/index.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { useState } from 'react';
2+
import { Button } from 'tdesign-mobile-react';
3+
import TDemoBlock from '../../../site/mobile/components/DemoBlock';
4+
// import TDemoHeader from '../../../site/mobile/components/DemoHeader';
5+
import './style/index.less';
6+
7+
import BaseList from './base.jsx';
8+
import ErrTipDemo from './err-tip.jsx';
9+
import PullRefreshDemo from './pull-refresh.jsx';
10+
11+
export default function ListDemo() {
12+
const [currentTab, setCurrentTab] = useState('info');
13+
14+
const onChangeTab = (val) => {
15+
setCurrentTab(val);
16+
history.pushState({}, '', '?tab=demo');
17+
};
18+
19+
return (
20+
<div className="tdesign-mobile-demo">
21+
<div className="list-demo">
22+
{currentTab === 'info' && (
23+
<div>
24+
<h1 className="title">List 列表</h1>
25+
<p className="summary">
26+
瀑布流滚动加载,用于展示同一类型信息的长列表。当列表即将滚动到底部时,会触发事件并加载更多列表项。
27+
</p>
28+
<TDemoBlock title="01 类型" summary="基础列表">
29+
<Button size="large" variant="outline" theme="primary" onClick={() => onChangeTab('base')}>
30+
{' '}
31+
基础列表{' '}
32+
</Button>
33+
<Button size="large" variant="outline" theme="primary" onClick={() => onChangeTab('pull-refresh')}>
34+
下拉刷新
35+
</Button>
36+
<Button size="large" variant="outline" theme="primary" onClick={() => onChangeTab('error-tip')}>
37+
错误提示
38+
</Button>
39+
</TDemoBlock>
40+
</div>
41+
)}
42+
{currentTab === 'base' && <BaseList></BaseList>}
43+
{currentTab === 'error-tip' && <ErrTipDemo></ErrTipDemo>}
44+
{currentTab === 'pull-refresh' && (
45+
<div className="pull-refresh-wrap">
46+
<PullRefreshDemo />
47+
</div>
48+
)}
49+
</div>
50+
</div>
51+
);
52+
}
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import React, { useState, useEffect, useRef } from 'react';
22
import { Cell, List, PullDownRefresh } from 'tdesign-mobile-react';
33

44
export default function ListDemo() {
5-
const [loading, setLoading] = useState('')
6-
const [refreshing, setRefreshing] = useState(false)
5+
const [loading, setLoading] = useState('');
6+
const [refreshing, setRefreshing] = useState(false);
77

8-
const listData = useRef([])
8+
const listData = useRef<string[]>([]);
99

1010
const MAX_DATA_LEN = 60;
1111

1212
const loadData = (isRefresh) => {
1313
const ONCE_LOAD_NUM = 20;
14-
return new Promise((resolve) => {
14+
return new Promise(() => {
1515
setTimeout(() => {
16-
const temp = [];
16+
const temp: string[] = [];
1717
for (let i = 0; i < ONCE_LOAD_NUM; i++) {
1818
if (isRefresh) {
1919
temp.push(`${i + 1}`);
@@ -23,22 +23,22 @@ export default function ListDemo() {
2323
}
2424

2525
if (isRefresh) {
26-
listData.current = temp
26+
listData.current = temp;
2727
} else {
28-
listData.current= [...listData.current, ...temp ]
28+
listData.current = [...listData.current, ...temp];
2929
}
3030
setLoading('');
3131
setRefreshing(false);
3232
}, 1000);
3333
});
3434
};
3535

36-
const onLoadData = (isRefresh) => {
36+
const onLoadData = (isRefresh?) => {
3737
if ((listData.current.length >= MAX_DATA_LEN && !isRefresh) || loading.value) {
3838
return;
3939
}
4040
setLoading('loading');
41-
loadData(isRefresh)
41+
loadData(isRefresh);
4242
};
4343

4444
const onScroll = (scrollBottom) => {
@@ -52,18 +52,19 @@ export default function ListDemo() {
5252
onLoadData(true);
5353
};
5454

55-
useEffect(()=>{
55+
useEffect(() => {
5656
onLoadData();
57+
// eslint-disable-next-line react-hooks/exhaustive-deps
5758
}, []);
5859

5960
return (
60-
<PullDownRefresh value={refreshing} onChange={(val)=>setRefreshing(val)} onRefresh={onRefresh}>
61+
<PullDownRefresh value={refreshing} onChange={(val) => setRefreshing(val)} onRefresh={onRefresh}>
6162
<List asyncLoading={loading} onScroll={onScroll}>
62-
{
63-
listData.current.map((item) => <Cell key={item} align="middle">
64-
<span className="cell">{item}</span>
65-
</Cell>)
66-
}
63+
{listData.current.map((item) => (
64+
<Cell key={item} align="middle">
65+
<span className="cell">{item}</span>
66+
</Cell>
67+
))}
6768
</List>
6869
</PullDownRefresh>
6970
);

0 commit comments

Comments
 (0)