index.jsx 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { useState, useCallback, useRef } from 'react';
  2. import { Button, message } 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, 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. if (typeof beforeSearchSubmit === 'function') {
  16. exportParams.current = beforeSearchSubmit(params);
  17. } else {
  18. exportParams.current = params;
  19. }
  20. return exportParams.current;
  21. }
  22. // 自动封装查询函数
  23. const getList = useCallback(queryTable(request), [request]);
  24. // 获取导出函数
  25. const [exporting, exportFunc] = useExport(expfunc);
  26. const handleExport = useCallback(() => {
  27. if (!exportParams.current) {
  28. const formData = ref.current ? ref.current.getFieldsValue() : {};
  29. exportParams.current = { ...formData, ...params }
  30. }
  31. let obj = exportParams.current
  32. if (obj.prefix) {
  33. if (obj.start) {
  34. if (obj.end) {
  35. exportFunc(exportParams.current);
  36. } else {
  37. message.info('请输入终止号码点击查询后再点击导出')
  38. }
  39. } else {
  40. message.info('请输入起始号码点击查询后再点击导出')
  41. }
  42. } else {
  43. message.info('请输入卡号前缀点击查询后再点击导出')
  44. }
  45. }, [exportFunc, params]);
  46. // 添加导出按钮
  47. const renderToolbar = useCallback(() => {
  48. const acts = [];
  49. if (toolBarRender) {
  50. acts.push(...toolBarRender());
  51. }
  52. if (typeof expfunc === 'function') {
  53. acts.push(
  54. <Button loading={exporting} key="export" icon={<UploadOutlined />} onClick={handleExport}>
  55. 导出
  56. </Button>,
  57. );
  58. }
  59. return acts;
  60. }, [toolBarRender, exporting, handleExport, expfunc]);
  61. return (
  62. <ProTable
  63. {...leftProps}
  64. params={params}
  65. formRef={ref}
  66. request={getList}
  67. toolBarRender={renderToolbar}
  68. beforeSearchSubmit={handleBeforSearch}
  69. />
  70. );
  71. };