2020-11-19 21:12:15 +08:00
|
|
|
import React, { useState } from 'react';
|
2020-11-11 15:23:39 +08:00
|
|
|
import { Table as AntdTable, Card } from 'antd';
|
|
|
|
import { redirectTo } from '@/components/pages/CollectionLoader/utils';
|
|
|
|
import { Actions } from '@/components/actions';
|
|
|
|
import { request, useRequest } from 'umi';
|
2020-11-13 22:01:14 +08:00
|
|
|
import api from '@/api-client';
|
2020-11-19 21:12:15 +08:00
|
|
|
import { components, fields2columns } from './SortableTable';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: '姓名',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '年龄',
|
|
|
|
dataIndex: 'age',
|
|
|
|
key: 'age',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '住址',
|
|
|
|
dataIndex: 'address',
|
|
|
|
key: 'address',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-11-13 22:01:14 +08:00
|
|
|
export interface TableProps {
|
|
|
|
schema?: any;
|
|
|
|
activeTab?: any;
|
|
|
|
resourceName: string;
|
|
|
|
associatedName?: string;
|
|
|
|
associatedKey?: string;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Table(props: TableProps) {
|
|
|
|
const {
|
|
|
|
activeTab = {},
|
|
|
|
schema,
|
|
|
|
resourceName,
|
|
|
|
associatedName,
|
|
|
|
associatedKey,
|
|
|
|
} = props;
|
2020-11-19 21:12:15 +08:00
|
|
|
const { fields, defaultTabName, rowKey = 'id', actions = [] } = schema;
|
2020-11-13 22:01:14 +08:00
|
|
|
const name = associatedName ? `${associatedName}.${resourceName}` : resourceName;
|
2020-11-19 21:12:15 +08:00
|
|
|
const { data, mutate } = useRequest(() => api.resource(name).list({
|
2020-11-13 22:01:14 +08:00
|
|
|
associatedKey,
|
|
|
|
}));
|
|
|
|
const { sourceKey = 'id' } = activeTab.field||{};
|
2020-11-19 21:12:15 +08:00
|
|
|
console.log(props);
|
|
|
|
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
|
|
|
|
const onChange = (selectedRowKeys: React.ReactText[]) => {
|
|
|
|
setSelectedRowKeys(selectedRowKeys);
|
|
|
|
}
|
|
|
|
const tableProps: any = {};
|
|
|
|
if (actions.length) {
|
|
|
|
tableProps.rowSelection = {
|
|
|
|
selectedRowKeys,
|
|
|
|
onChange,
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 15:23:39 +08:00
|
|
|
return (
|
|
|
|
<Card bordered={false}>
|
|
|
|
<Actions {...props} style={{ marginBottom: 14 }} actions={actions}/>
|
2020-11-19 21:12:15 +08:00
|
|
|
<AntdTable
|
|
|
|
dataSource={data}
|
|
|
|
rowKey={rowKey}
|
|
|
|
columns={fields2columns(fields)}
|
|
|
|
components={components({data, mutate})}
|
|
|
|
onRow={(data) => ({
|
|
|
|
onClick: () => {
|
|
|
|
redirectTo({
|
|
|
|
...props.match.params,
|
|
|
|
[activeTab ? 'newItem' : 'lastItem']: {
|
|
|
|
itemId: data[rowKey]||data.id,
|
|
|
|
tabName: defaultTabName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
{...tableProps}
|
|
|
|
/>
|
2020-11-11 15:23:39 +08:00
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|