mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 17:19:11 +08:00
e7aa014c31
* docs: init * chore: all types * docs: faq * chore: fix lint
116 lines
2.6 KiB
TypeScript
116 lines
2.6 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { Table } from 'antd';
|
|
import type { GetProp, TableProps } from 'antd';
|
|
import qs from 'qs';
|
|
|
|
type ColumnsType<T> = TableProps<T>['columns'];
|
|
type TablePaginationConfig = Exclude<GetProp<TableProps, 'pagination'>, boolean>;
|
|
|
|
interface DataType {
|
|
name: {
|
|
first: string;
|
|
last: string;
|
|
};
|
|
gender: string;
|
|
email: string;
|
|
login: {
|
|
uuid: string;
|
|
};
|
|
}
|
|
|
|
interface TableParams {
|
|
pagination?: TablePaginationConfig;
|
|
sortField?: string;
|
|
sortOrder?: string;
|
|
filters?: Parameters<GetProp<TableProps, 'onChange'>>[1];
|
|
}
|
|
|
|
const columns: ColumnsType<DataType> = [
|
|
{
|
|
title: 'Name',
|
|
dataIndex: 'name',
|
|
sorter: true,
|
|
render: (name) => `${name.first} ${name.last}`,
|
|
width: '20%',
|
|
},
|
|
{
|
|
title: 'Gender',
|
|
dataIndex: 'gender',
|
|
filters: [
|
|
{ text: 'Male', value: 'male' },
|
|
{ text: 'Female', value: 'female' },
|
|
],
|
|
width: '20%',
|
|
},
|
|
{
|
|
title: 'Email',
|
|
dataIndex: 'email',
|
|
},
|
|
];
|
|
|
|
const getRandomuserParams = (params: TableParams) => ({
|
|
results: params.pagination?.pageSize,
|
|
page: params.pagination?.current,
|
|
...params,
|
|
});
|
|
|
|
const App: React.FC = () => {
|
|
const [data, setData] = useState<DataType[]>();
|
|
const [loading, setLoading] = useState(false);
|
|
const [tableParams, setTableParams] = useState<TableParams>({
|
|
pagination: {
|
|
current: 1,
|
|
pageSize: 10,
|
|
},
|
|
});
|
|
|
|
const fetchData = () => {
|
|
setLoading(true);
|
|
fetch(`https://randomuser.me/api?${qs.stringify(getRandomuserParams(tableParams))}`)
|
|
.then((res) => res.json())
|
|
.then(({ results }) => {
|
|
setData(results);
|
|
setLoading(false);
|
|
setTableParams({
|
|
...tableParams,
|
|
pagination: {
|
|
...tableParams.pagination,
|
|
total: 200,
|
|
// 200 is mock data, you should read it from server
|
|
// total: data.totalCount,
|
|
},
|
|
});
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchData();
|
|
}, [JSON.stringify(tableParams)]);
|
|
|
|
const handleTableChange: TableProps['onChange'] = (pagination, filters, sorter) => {
|
|
setTableParams({
|
|
pagination,
|
|
filters,
|
|
...sorter,
|
|
});
|
|
|
|
// `dataSource` is useless since `pageSize` changed
|
|
if (pagination.pageSize !== tableParams.pagination?.pageSize) {
|
|
setData([]);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Table
|
|
columns={columns}
|
|
rowKey={(record) => record.login.uuid}
|
|
dataSource={data}
|
|
pagination={tableParams.pagination}
|
|
loading={loading}
|
|
onChange={handleTableChange}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default App;
|