List.jsx 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import { Button, Popconfirm, message } from 'antd';
  3. import { ProTable } from '@ant-design/pro-components';
  4. import { queryTable } from '@/utils/request';
  5. import Page from './index';
  6. export default React.forwardRef((props, ref) => {
  7. /**
  8. * request 与 ProTable 定义不同,这个只是普通的接口即可
  9. */
  10. const { request, rowKey, columns, onAdd, onDelete, onEdit, toolBarRender, optionRender, ...leftProps } = props;
  11. const actionRef = React.useRef();
  12. const hideRef = React.useRef();
  13. const api = React.useMemo(() => queryTable(request), [request]);
  14. // 统一实现操作列
  15. const cols = React.useMemo(() => [
  16. ...columns,
  17. {
  18. title: '操作',
  19. valueType: 'option',
  20. key: 'option',
  21. width: 120,
  22. render: (_, record) => [
  23. (
  24. onEdit &&
  25. <Button style={{ padding: 0 }} type="link" key={1} onClick={() => onEdit(record) }>
  26. 编辑
  27. </Button>
  28. ),
  29. (
  30. onDelete &&
  31. <Popconfirm
  32. key={3}
  33. title="您是否确认删除 ?"
  34. onConfirm={() => onDelete(record)}
  35. okText="确定"
  36. cancelText="取消"
  37. >
  38. <Button style={{ padding: 0 }} type="link">
  39. 删除
  40. </Button>
  41. </Popconfirm>
  42. ),
  43. ...(optionRender ? optionRender(_, record) : []),
  44. ].filter(Boolean),
  45. },
  46. ], [columns, onEdit, onDelete]);
  47. React.useImperativeHandle(ref, () => {
  48. const showLoading = msg => (hideRef.current = message.loading(msg || '操作中, 请稍候...'));
  49. const hideLoading = () => hideRef.current && hideRef.current();
  50. const reload = () => actionRef.current?.reload && actionRef.current.reload();
  51. return {
  52. showLoading,
  53. hideLoading,
  54. reload,
  55. actionRef: actionRef,
  56. }
  57. }, []);
  58. return (
  59. <Page>
  60. <ProTable
  61. rowKey={rowKey}
  62. columns={cols}
  63. request={api}
  64. cardBordered
  65. actionRef={actionRef}
  66. toolBarRender={() => [
  67. (
  68. onAdd &&
  69. <Button
  70. key="1"
  71. type="primary"
  72. onClick={onAdd}
  73. >
  74. 新增
  75. </Button>
  76. ),
  77. ...(toolBarRender ? toolBarRender() : []),
  78. ].filter(Boolean)}
  79. {...leftProps}
  80. />
  81. </Page>
  82. )
  83. });