mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 00:29:12 +08:00
3f9e711d3b
* type(demo): improve type by GetProps util * fix lint
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Button, Flex, Table } from 'antd';
|
|
import type { TableColumnsType, TableProps } from 'antd';
|
|
|
|
type TableRowSelection<T> = TableProps<T>['rowSelection'];
|
|
|
|
interface DataType {
|
|
key: React.Key;
|
|
name: string;
|
|
age: number;
|
|
address: string;
|
|
}
|
|
|
|
const columns: TableColumnsType<DataType> = [
|
|
{ title: 'Name', dataIndex: 'name' },
|
|
{ title: 'Age', dataIndex: 'age' },
|
|
{ title: 'Address', dataIndex: 'address' },
|
|
];
|
|
|
|
const dataSource = Array.from<DataType>({ length: 46 }).map<DataType>((_, i) => ({
|
|
key: i,
|
|
name: `Edward King ${i}`,
|
|
age: 32,
|
|
address: `London, Park Lane no. ${i}`,
|
|
}));
|
|
|
|
const App: React.FC = () => {
|
|
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const start = () => {
|
|
setLoading(true);
|
|
// ajax request after empty completing
|
|
setTimeout(() => {
|
|
setSelectedRowKeys([]);
|
|
setLoading(false);
|
|
}, 1000);
|
|
};
|
|
|
|
const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
|
|
console.log('selectedRowKeys changed: ', newSelectedRowKeys);
|
|
setSelectedRowKeys(newSelectedRowKeys);
|
|
};
|
|
|
|
const rowSelection: TableRowSelection<DataType> = {
|
|
selectedRowKeys,
|
|
onChange: onSelectChange,
|
|
};
|
|
|
|
const hasSelected = selectedRowKeys.length > 0;
|
|
|
|
return (
|
|
<Flex gap="middle" vertical>
|
|
<Flex align="center" gap="middle">
|
|
<Button type="primary" onClick={start} disabled={!hasSelected} loading={loading}>
|
|
Reload
|
|
</Button>
|
|
{hasSelected ? `Selected ${selectedRowKeys.length} items` : null}
|
|
</Flex>
|
|
<Table rowSelection={rowSelection} columns={columns} dataSource={dataSource} />
|
|
</Flex>
|
|
);
|
|
};
|
|
|
|
export default App;
|