index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { useIntl } from '@@/plugin-locale/localeExports';
  2. import { Button, Dropdown } from 'antd';
  3. import type { ActionType, ProColumns, RequestData } from '@jetlinks/pro-table';
  4. import ProTable from '@jetlinks/pro-table';
  5. import { EllipsisOutlined, PlusOutlined } from '@ant-design/icons';
  6. import type BaseService from '@/utils/BaseService';
  7. import * as React from 'react';
  8. import { useRef, useState } from 'react';
  9. import Save from '@/components/BaseCrud/save';
  10. import type { ISchema } from '@formily/json-schema';
  11. import { CurdModel } from '@/components/BaseCrud/model';
  12. import type { ISchemaFieldProps } from '@formily/react/lib/types';
  13. import type { ModalProps } from 'antd/lib/modal/Modal';
  14. import type { TablePaginationConfig } from 'antd/lib/table/interface';
  15. import type { Form } from '@formily/core';
  16. import SearchComponent from '@/components/SearchComponent';
  17. import type { ProFormInstance } from '@ant-design/pro-form';
  18. import type { SearchConfig } from '@ant-design/pro-form/lib/components/Submitter';
  19. export type Option = {
  20. model: 'edit' | 'preview' | 'add';
  21. current: any;
  22. visible: boolean;
  23. add: () => void;
  24. update: (current: any) => void;
  25. close: () => void;
  26. };
  27. declare type OverlayFunc = () => React.ReactElement;
  28. export type Props<T> = {
  29. columns: ProColumns<T>[];
  30. service: BaseService<T>;
  31. title: string;
  32. menu?: React.ReactElement | OverlayFunc;
  33. schema: ISchema;
  34. schemaConfig?: ISchemaFieldProps;
  35. defaultParams?: Record<string, any>;
  36. actionRef: React.MutableRefObject<ActionType | undefined>;
  37. modelConfig?: ModalProps & { loading?: boolean };
  38. request?: (params: any) => Promise<Partial<RequestData<T>>>;
  39. toolBar?: React.ReactNode[];
  40. pagination?: false | TablePaginationConfig;
  41. search?: false | SearchConfig;
  42. formEffect?: () => void; // 与form参数 只有一个生效
  43. form?: Form;
  44. /** @name 用于存储搜索历史记录的标记*/
  45. moduleName?: string; //
  46. };
  47. const BaseCrud = <T extends Record<string, any>>(props: Props<T>) => {
  48. const intl = useIntl();
  49. const ref = useRef<ProFormInstance>();
  50. const {
  51. columns,
  52. service,
  53. title,
  54. menu,
  55. schema,
  56. defaultParams,
  57. actionRef,
  58. schemaConfig,
  59. modelConfig,
  60. request,
  61. toolBar,
  62. pagination,
  63. search,
  64. formEffect,
  65. form,
  66. moduleName,
  67. } = props;
  68. const [param, setParam] = useState({});
  69. return (
  70. <>
  71. <SearchComponent<T>
  72. field={columns}
  73. onSearch={async (data) => {
  74. // actionRef.current?.reset?.();
  75. actionRef.current?.setPageInfo?.({ pageSize: 10 });
  76. setParam(data);
  77. }}
  78. target={moduleName}
  79. onReset={() => {
  80. // 重置分页及搜索参数
  81. actionRef.current?.reset?.();
  82. setParam({});
  83. }}
  84. />
  85. <ProTable<T>
  86. params={param}
  87. formRef={ref}
  88. columns={columns}
  89. actionRef={actionRef}
  90. options={{ fullScreen: true }}
  91. request={
  92. request ||
  93. (async (params = {}) =>
  94. service.query({
  95. ...params,
  96. sorts: [{ name: 'createTime', order: 'desc' }], // 默认排序
  97. }))
  98. }
  99. editable={{
  100. type: 'multiple',
  101. }}
  102. rowKey="id"
  103. search={
  104. search === false
  105. ? false
  106. : {
  107. labelWidth: 'auto',
  108. }
  109. }
  110. form={{
  111. syncToUrl: false,
  112. }}
  113. pagination={
  114. pagination === false
  115. ? false
  116. : {
  117. pageSize: 10,
  118. }
  119. }
  120. dateFormatter="string"
  121. headerTitle={title}
  122. defaultParams={defaultParams}
  123. toolBarRender={() =>
  124. toolBar || [
  125. <Button onClick={CurdModel.add} key="button" icon={<PlusOutlined />} type="primary">
  126. {intl.formatMessage({
  127. id: 'pages.data.option.add',
  128. defaultMessage: '新增',
  129. })}
  130. </Button>,
  131. menu && (
  132. <Dropdown key="menu" overlay={menu}>
  133. <Button>
  134. <EllipsisOutlined />
  135. </Button>
  136. </Dropdown>
  137. ),
  138. ]
  139. }
  140. />
  141. <Save
  142. reload={() => actionRef.current?.reload()}
  143. service={service}
  144. schema={schema}
  145. schemaConfig={schemaConfig}
  146. modelConfig={modelConfig}
  147. formEffect={formEffect}
  148. customForm={form}
  149. />
  150. </>
  151. );
  152. };
  153. export default BaseCrud;