mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-25 05:32:31 +08:00

* docs: move mock api from randomuser.me to mocky.io * update * update * update * test: update snapshot
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { Avatar, Button, List, Skeleton } from 'antd';
|
|
|
|
interface DataType {
|
|
gender?: string;
|
|
name?: string;
|
|
email?: string;
|
|
avatar?: string;
|
|
loading: boolean;
|
|
}
|
|
|
|
const PAGE_SIZE = 3;
|
|
|
|
const App: React.FC = () => {
|
|
const [initLoading, setInitLoading] = useState(true);
|
|
const [loading, setLoading] = useState(false);
|
|
const [data, setData] = useState<DataType[]>([]);
|
|
const [list, setList] = useState<DataType[]>([]);
|
|
const [page, setPage] = useState(1);
|
|
|
|
const fetchData = (currentPage: number) => {
|
|
const fakeDataUrl = `https://660d2bd96ddfa2943b33731c.mockapi.io/api/users?page=${currentPage}&limit=${PAGE_SIZE}`;
|
|
return fetch(fakeDataUrl).then((res) => res.json());
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchData(page).then((res) => {
|
|
const results = Array.isArray(res) ? res : [];
|
|
setInitLoading(false);
|
|
setData(results);
|
|
setList(results);
|
|
});
|
|
}, []);
|
|
|
|
const onLoadMore = () => {
|
|
setLoading(true);
|
|
setList(data.concat(Array.from({ length: PAGE_SIZE }).map(() => ({ loading: true }))));
|
|
const nextPage = page + 1;
|
|
setPage(nextPage);
|
|
fetchData(nextPage).then((res) => {
|
|
const results = Array.isArray(res) ? res : [];
|
|
const newData = data.concat(results);
|
|
setData(newData);
|
|
setList(newData);
|
|
setLoading(false);
|
|
// Resetting window's offsetTop so as to display react-virtualized demo underfloor.
|
|
// In real scene, you can using public method of react-virtualized:
|
|
// https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
|
|
window.dispatchEvent(new Event('resize'));
|
|
});
|
|
};
|
|
|
|
const loadMore =
|
|
!initLoading && !loading ? (
|
|
<div
|
|
style={{
|
|
textAlign: 'center',
|
|
marginTop: 12,
|
|
height: 32,
|
|
lineHeight: '32px',
|
|
}}
|
|
>
|
|
<Button onClick={onLoadMore}>loading more</Button>
|
|
</div>
|
|
) : null;
|
|
|
|
return (
|
|
<List
|
|
className="demo-loadmore-list"
|
|
loading={initLoading}
|
|
itemLayout="horizontal"
|
|
loadMore={loadMore}
|
|
dataSource={list}
|
|
renderItem={(item) => (
|
|
<List.Item
|
|
actions={[<a key="list-loadmore-edit">edit</a>, <a key="list-loadmore-more">more</a>]}
|
|
>
|
|
<Skeleton avatar title={false} loading={item.loading} active>
|
|
<List.Item.Meta
|
|
avatar={<Avatar src={item.avatar} />}
|
|
title={<a href="https://ant.design">{item.name}</a>}
|
|
description="Ant Design, a design language for background applications, is refined by Ant UED Team"
|
|
/>
|
|
<div>content</div>
|
|
</Skeleton>
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default App;
|