tachybase_todo/packages/app/src/components/views/SimpleTable.tsx
chenos b5ddd6a6ba
feat: collection options & hooks (#21)
* feat: collection hooks

* export action middlewares

* add associated middleware

* cleanup

* add field interface options

* 调整配置参数

* 补充字段类型 options

* 继续调整配置参数

* 支持排序

* filterable & sortable & draggable

* feat: add random name for creating table (#23)

* feat: add random name for creating table

* fix: random number

* Feature: collections field (#24)

* feat: add random name for field and update table options

* fix: make field name required

* fix: this declaration

* showInXX 参数调整

* showInXX 放 component 里

* 继续调整参数

* 字段分组、pages 表配置参数等

* change date to datetime

* 选择类型字段的 options 改为 dataSource

* feat: refactor hooks initialization and add field options by interface (#25)

* feat: refactor hooks initialization and add field options by interface

* refactor: use model.set to build input values

* refactor: extend setter/getter to adapt field options

* fix: try to fix virtual field

* refactor: setter/getter of FieldModel

* 改进自定义 model 等细节

* 补充注释

* bugfix

Co-authored-by: Junyi <mytharcher@users.noreply.github.com>
2020-12-01 20:11:39 +08:00

110 lines
3.1 KiB
TypeScript

import React, { useEffect, useRef, useState } from 'react';
import { Table as AntdTable, Card, Pagination } from 'antd';
import { Actions } from '@/components/actions';
import ViewFactory from '@/components/views';
import { useRequest } from 'umi';
import api from '@/api-client';
import get from 'lodash/get';
import { components, fields2columns } from './SortableTable';
export interface SimpleTableProps {
schema?: any;
activeTab?: any;
resourceName: string;
associatedName?: string;
associatedKey?: string;
[key: string]: any;
}
export function SimpleTable(props: SimpleTableProps) {
console.log(props);
const {
activeTab = {},
pageInfo = {},
schema,
resourceName,
associatedName,
associatedKey,
} = props;
const { rowKey = 'id', fields = [], rowViewName, actions = [], paginated = true, defaultPageSize = 10 } = schema;
const { sourceKey = 'id' } = activeTab.field||{};
const drawerRef = useRef<any>();
const name = associatedName ? `${associatedName}.${resourceName}` : resourceName;
const { data, loading, pagination, mutate, refresh } = useRequest((params = {}) => {
const { current, pageSize, ...restParams } = params;
return api.resource(name).list({
associatedKey,
page: paginated ? current : 1,
perPage: paginated ? pageSize : -1,
})
.then(({data = [], meta = {}}) => {
return {
data: {
list: data,
total: meta.count||data.length,
},
};
});
}, {
paginated,
defaultPageSize,
});
console.log(schema, data);
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
const onChange = (selectedRowKeys: React.ReactText[]) => {
setSelectedRowKeys(selectedRowKeys);
}
const tableProps: any = {};
if (actions.length) {
tableProps.rowSelection = {
selectedRowKeys,
onChange,
}
}
console.log('rowViewName', {rowViewName})
return (
<Card bordered={false}>
<Actions {...props} style={{ marginBottom: 14 }} actions={actions}/>
<ViewFactory
{...props}
viewName={rowViewName}
reference={drawerRef}
/>
<AntdTable
rowKey={rowKey}
loading={loading}
columns={fields2columns(fields)}
dataSource={data?.list||(data as any)}
components={components({
data,
mutate,
rowKey,
onMoved: async ({resourceKey, offset}) => {
await api.resource(name).sort({
associatedKey,
resourceKey,
field: 'sort',
offset,
});
await refresh();
console.log({resourceKey, offset});
}
})}
onRow={(record) => ({
onClick: () => {
drawerRef.current.setVisible(true);
drawerRef.current.getData(record[rowKey]);
}
})}
pagination={false}
{...tableProps}
/>
{paginated && (
<div className={'table-pagination'}>
<Pagination {...pagination} showQuickJumper showSizeChanger size={'default'}/>
</div>
)}
</Card>
);
}