index.jsx 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React, { useState, useCallback, useRef } from 'react';
  2. import { Button } from 'antd';
  3. import { UploadOutlined } from '@ant-design/icons';
  4. import ProTable from '@ant-design/pro-table';
  5. import { queryTable } from '@/utils/request';
  6. import useExport from '@/utils/hooks/useExport';
  7. export default (props) => {
  8. const { formRef, request, expfunc, params = {}, toolBarRender, ...leftProps } = props;
  9. // 如果 formRef 未设置, 则使用 fmRef 去代理
  10. const fmRef = useRef();
  11. const ref = formRef || fmRef;
  12. // 自动封装查询函数
  13. const getList = useCallback(queryTable(request), [request]);
  14. // 获取导出函数
  15. const [exporting, exportFunc] = useExport(expfunc);
  16. const handleExport = useCallback(() => {
  17. const formData = ref.current ? ref.current.getFieldsValue() : {};
  18. exportFunc({ ...formData, ...params });
  19. }, [exportFunc, params]);
  20. // 添加导出按钮
  21. const renderToolbar = useCallback(() => {
  22. const acts = [];
  23. if (toolBarRender) {
  24. acts.push(...toolBarRender());
  25. }
  26. if (typeof expfunc === 'function') {
  27. acts.push(
  28. <Button loading={exporting} key="export" icon={<UploadOutlined />} onClick={handleExport}>
  29. 导出
  30. </Button>,
  31. );
  32. }
  33. return acts;
  34. }, [toolBarRender, exporting, handleExport, expfunc]);
  35. return (
  36. <ProTable
  37. {...leftProps}
  38. params={params}
  39. formRef={ref}
  40. request={getList}
  41. toolBarRender={renderToolbar}
  42. />
  43. );
  44. };