index.jsx 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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,invoiceId, params = {}, toolBarRender, beforeSearchSubmit, ...leftProps } = props;
  9. // 如果 formRef 未设置, 则使用 fmRef 去代理
  10. const fmRef = useRef();
  11. const ref = formRef || fmRef;
  12. // 导出函数需要使用的 params
  13. const exportParams = useRef()
  14. const handleBeforSearch = (params) => {
  15. params.invoiceId=invoiceId||params.invoiceId||undefined
  16. if (typeof beforeSearchSubmit === 'function') {
  17. exportParams.current = beforeSearchSubmit(params);
  18. } else {
  19. exportParams.current = params;
  20. }
  21. return exportParams.current;
  22. }
  23. // 自动封装查询函数
  24. const getList = useCallback(queryTable(request), [request]);
  25. // 获取导出函数
  26. const [exporting, exportFunc] = useExport(expfunc);
  27. const handleExport = useCallback(() => {
  28. if (!exportParams.current) {
  29. const formData = ref.current ? ref.current.getFieldsValue() : {};
  30. exportParams.current = { ...formData, ...params }
  31. }
  32. exportFunc(exportParams.current);
  33. }, [exportFunc, params]);
  34. // 添加导出按钮
  35. const renderToolbar = useCallback(() => {
  36. const acts = [];
  37. if (toolBarRender) {
  38. acts.push(...toolBarRender());
  39. }
  40. if (typeof expfunc === 'function') {
  41. acts.push(
  42. <Button loading={exporting} key="export" icon={<UploadOutlined />} onClick={handleExport}>
  43. 导出
  44. </Button>,
  45. );
  46. }
  47. return acts;
  48. }, [toolBarRender, exporting, handleExport, expfunc]);
  49. return (
  50. <ProTable
  51. {...leftProps}
  52. params={params}
  53. formRef={ref}
  54. request={getList}
  55. toolBarRender={renderToolbar}
  56. beforeSearchSubmit={handleBeforSearch}
  57. />
  58. );
  59. };