|
@@ -1,90 +1,89 @@
|
1
|
|
-import React, { useRef, useMemo } from 'react';
|
|
1
|
+import React from 'react';
|
2
|
2
|
import { Button, Popconfirm, message } from 'antd';
|
3
|
3
|
import { ProTable } from '@ant-design/pro-components';
|
4
|
|
-import { Link, useNavigate } from "react-router-dom";
|
5
|
|
-import { queryTable, restful } from '@/utils/request';
|
|
4
|
+import { queryTable } from '@/utils/request';
|
6
|
5
|
import Page from './index';
|
7
|
6
|
|
8
|
|
-export default (props) => {
|
|
7
|
+export default React.forwardRef((props, ref) => {
|
9
|
8
|
/**
|
10
|
|
- * resource 用来确定应该调用哪个接口
|
11
|
|
- * rowKey 标识数据每一行的唯一键, 只能是字符串
|
12
|
|
- * columns 定义表格每一列, 符合 ProTable 定义
|
13
|
|
- * editURL 编辑页路由
|
|
9
|
+ * request 与 ProTable 定义不同,这个只是普通的接口即可
|
14
|
10
|
*/
|
15
|
|
- const { resource, rowKey, columns, editURL } = props;
|
16
|
|
- const actionRef = useRef();
|
17
|
|
- const navigate = useNavigate();
|
|
11
|
+ const { request, rowKey, columns, onAdd, onDelete, onEdit, toolBarRender, optionRender, ...leftProps } = props;
|
|
12
|
+ const actionRef = React.useRef();
|
|
13
|
+ const hideRef = React.useRef();
|
18
|
14
|
|
19
|
|
- // 接口汇总
|
20
|
|
- const api = useMemo(() => {
|
21
|
|
- // 通过 resource 知道是哪个接口
|
22
|
|
- // 通过 restfule 生成 增删改查的接口
|
23
|
|
- const apis = restful(resource);
|
24
|
|
-
|
25
|
|
- // 返回两个,一个是列表查询, 一个是删除
|
26
|
|
- return {
|
27
|
|
- list: apis[0],
|
28
|
|
- delete: apis[4],
|
29
|
|
- }
|
30
|
|
- }, [resource]);
|
31
|
|
-
|
32
|
|
- // 响应 删除 操作
|
33
|
|
- const onDelete = (row) => {
|
34
|
|
- const hide = message.loading('操作中, 请稍候...');
|
35
|
|
- api.delete(row[rowKey]).then(() => {
|
36
|
|
- hide();
|
37
|
|
- actionRef.current.reload();
|
38
|
|
- }).catch(hide);
|
39
|
|
- }
|
|
15
|
+ const api = React.useMemo(() => queryTable(request), [request]);
|
40
|
16
|
|
41
|
17
|
// 统一实现操作列
|
42
|
|
- const cols = [
|
|
18
|
+ const cols = React.useMemo(() => [
|
43
|
19
|
...columns,
|
44
|
20
|
{
|
45
|
21
|
title: '操作',
|
46
|
22
|
valueType: 'option',
|
47
|
23
|
key: 'option',
|
48
|
|
- ellipsis: true,
|
|
24
|
+ width: 120,
|
49
|
25
|
render: (_, record) => [
|
50
|
|
- <Button style={{ padding: 0 }} type="link" key={1} onClick={() => { navigate(`${editURL}?id=${record[rowKey]}`) }}>
|
51
|
|
- 编辑
|
52
|
|
- </Button>,
|
53
|
|
- <Popconfirm
|
54
|
|
- key={3}
|
55
|
|
- title="您是否确认删除 ?"
|
56
|
|
- onConfirm={() => onDelete(record)}
|
57
|
|
- okText="确定"
|
58
|
|
- cancelText="取消"
|
59
|
|
- >
|
60
|
|
- <Button style={{ padding: 0 }} type="link">
|
61
|
|
- 删除
|
|
26
|
+ (
|
|
27
|
+ onEdit &&
|
|
28
|
+ <Button style={{ padding: 0 }} type="link" key={1} onClick={() => onEdit(record) }>
|
|
29
|
+ 编辑
|
62
|
30
|
</Button>
|
63
|
|
- </Popconfirm>,
|
64
|
|
- ],
|
|
31
|
+ ),
|
|
32
|
+ (
|
|
33
|
+ onDelete &&
|
|
34
|
+ <Popconfirm
|
|
35
|
+ key={3}
|
|
36
|
+ title="您是否确认删除 ?"
|
|
37
|
+ onConfirm={() => onDelete(record)}
|
|
38
|
+ okText="确定"
|
|
39
|
+ cancelText="取消"
|
|
40
|
+ >
|
|
41
|
+ <Button style={{ padding: 0 }} type="link">
|
|
42
|
+ 删除
|
|
43
|
+ </Button>
|
|
44
|
+ </Popconfirm>
|
|
45
|
+ ),
|
|
46
|
+ ...(optionRender ? optionRender(_, record) : []),
|
|
47
|
+ ].filter(Boolean),
|
65
|
48
|
},
|
66
|
|
- ]
|
|
49
|
+ ], [columns, onEdit, onDelete]);
|
|
50
|
+
|
|
51
|
+ React.useImperativeHandle(ref, () => {
|
|
52
|
+ const showLoading = msg => (hideRef.current = message.loading(msg || '操作中, 请稍候...'));
|
|
53
|
+ const hideLoading = () => hideRef.current && hideRef.current();
|
|
54
|
+ const reload = () => actionRef.current?.reload && actionRef.current.reload();
|
|
55
|
+
|
|
56
|
+ return {
|
|
57
|
+ showLoading,
|
|
58
|
+ hideLoading,
|
|
59
|
+ reload,
|
|
60
|
+ actionRef: actionRef,
|
|
61
|
+ }
|
|
62
|
+ }, []);
|
67
|
63
|
|
68
|
64
|
return (
|
69
|
65
|
<Page>
|
70
|
66
|
<ProTable
|
71
|
67
|
rowKey={rowKey}
|
72
|
68
|
columns={cols}
|
73
|
|
- request={queryTable(api.list)}
|
|
69
|
+ request={api}
|
74
|
70
|
cardBordered
|
75
|
71
|
actionRef={actionRef}
|
76
|
72
|
toolBarRender={() => [
|
77
|
|
- <Button
|
78
|
|
- key="1"
|
79
|
|
- type="primary"
|
80
|
|
- onClick={() => {
|
81
|
|
- navigate(editURL);
|
82
|
|
- }}
|
83
|
|
- >
|
84
|
|
- 新增
|
85
|
|
- </Button>,
|
86
|
|
- ]}
|
|
73
|
+ (
|
|
74
|
+ onAdd &&
|
|
75
|
+ <Button
|
|
76
|
+ key="1"
|
|
77
|
+ type="primary"
|
|
78
|
+ onClick={onAdd}
|
|
79
|
+ >
|
|
80
|
+ 新增
|
|
81
|
+ </Button>
|
|
82
|
+ ),
|
|
83
|
+ ...(toolBarRender ? toolBarRender() : []),
|
|
84
|
+ ].filter(Boolean)}
|
|
85
|
+ {...leftProps}
|
87
|
86
|
/>
|
88
|
87
|
</Page>
|
89
|
88
|
)
|
90
|
|
-}
|
|
89
|
+});
|