2022-11-09 12:28:04 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2024-01-11 15:55:58 +08:00
|
|
|
import type { GetProp, TableProps } from 'antd';
|
2024-06-20 15:07:32 +08:00
|
|
|
import { Table } from 'antd';
|
|
|
|
import type { SorterResult } from 'antd/es/table/interface';
|
2024-01-11 15:55:58 +08:00
|
|
|
import qs from 'qs';
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
type ColumnsType<T extends object = object> = TableProps<T>['columns'];
|
2024-01-11 15:55:58 +08:00
|
|
|
type TablePaginationConfig = Exclude<GetProp<TableProps, 'pagination'>, boolean>;
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
interface DataType {
|
|
|
|
name: {
|
|
|
|
first: string;
|
|
|
|
last: string;
|
|
|
|
};
|
|
|
|
gender: string;
|
|
|
|
email: string;
|
|
|
|
login: {
|
|
|
|
uuid: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TableParams {
|
|
|
|
pagination?: TablePaginationConfig;
|
2024-06-20 15:07:32 +08:00
|
|
|
sortField?: SorterResult<any>['field'];
|
|
|
|
sortOrder?: SorterResult<any>['order'];
|
2024-01-11 15:55:58 +08:00
|
|
|
filters?: Parameters<GetProp<TableProps, 'onChange'>>[1];
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const columns: ColumnsType<DataType> = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: true,
|
2022-11-19 13:47:33 +08:00
|
|
|
render: (name) => `${name.first} ${name.last}`,
|
2022-11-09 12:28:04 +08:00
|
|
|
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 = () => {
|
2022-12-13 17:04:12 +08:00
|
|
|
const [data, setData] = useState<DataType[]>();
|
2022-11-09 12:28:04 +08:00
|
|
|
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))}`)
|
2022-11-19 13:47:33 +08:00
|
|
|
.then((res) => res.json())
|
2022-11-09 12:28:04 +08:00
|
|
|
.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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-09-16 00:08:45 +08:00
|
|
|
useEffect(fetchData, [
|
2024-06-20 15:07:32 +08:00
|
|
|
tableParams.pagination?.current,
|
|
|
|
tableParams.pagination?.pageSize,
|
|
|
|
tableParams?.sortOrder,
|
|
|
|
tableParams?.sortField,
|
|
|
|
JSON.stringify(tableParams.filters),
|
|
|
|
]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const handleTableChange: TableProps<DataType>['onChange'] = (pagination, filters, sorter) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
setTableParams({
|
|
|
|
pagination,
|
|
|
|
filters,
|
2024-06-20 15:07:32 +08:00
|
|
|
sortOrder: Array.isArray(sorter) ? undefined : sorter.order,
|
|
|
|
sortField: Array.isArray(sorter) ? undefined : sorter.field,
|
2022-11-09 12:28:04 +08:00
|
|
|
});
|
2022-12-13 17:04:12 +08:00
|
|
|
|
|
|
|
// `dataSource` is useless since `pageSize` changed
|
|
|
|
if (pagination.pageSize !== tableParams.pagination?.pageSize) {
|
|
|
|
setData([]);
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-09-16 00:08:45 +08:00
|
|
|
<Table<DataType>
|
2022-11-09 12:28:04 +08:00
|
|
|
columns={columns}
|
2022-11-19 13:47:33 +08:00
|
|
|
rowKey={(record) => record.login.uuid}
|
2022-11-09 12:28:04 +08:00
|
|
|
dataSource={data}
|
|
|
|
pagination={tableParams.pagination}
|
|
|
|
loading={loading}
|
|
|
|
onChange={handleTableChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|