index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { useIntl } from '@@/plugin-locale/localeExports';
  2. import { Button, Tooltip } from 'antd';
  3. import type { ActionType, ProColumns, RequestData } from '@jetlinks/pro-table';
  4. import ProTable from '@jetlinks/pro-table';
  5. import { 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. footer?: React.ReactNode;
  47. disableAdd?: boolean;
  48. };
  49. const BaseCrud = <T extends Record<string, any>>(props: Props<T>) => {
  50. const intl = useIntl();
  51. const ref = useRef<ProFormInstance>();
  52. const {
  53. columns,
  54. service,
  55. // title,
  56. // menu,
  57. schema,
  58. defaultParams,
  59. actionRef,
  60. schemaConfig,
  61. modelConfig,
  62. request,
  63. // toolBar,
  64. pagination,
  65. search,
  66. formEffect,
  67. form,
  68. moduleName,
  69. footer,
  70. } = props;
  71. const [param, setParam] = useState({});
  72. return (
  73. <>
  74. <SearchComponent<T>
  75. field={columns}
  76. onSearch={async (data) => {
  77. actionRef.current?.setPageInfo?.({ pageSize: 10 });
  78. setParam(data);
  79. }}
  80. target={moduleName}
  81. />
  82. <ProTable<T>
  83. params={param}
  84. formRef={ref}
  85. columns={columns}
  86. actionRef={actionRef}
  87. options={{ fullScreen: true }}
  88. request={
  89. request ||
  90. (async (params = {}) =>
  91. service.query({
  92. ...params,
  93. sorts: [{ name: 'createTime', order: 'desc' }], // 默认排序
  94. }))
  95. }
  96. editable={{
  97. type: 'multiple',
  98. }}
  99. rowKey="id"
  100. search={
  101. search === false
  102. ? false
  103. : {
  104. labelWidth: 'auto',
  105. }
  106. }
  107. form={{
  108. syncToUrl: false,
  109. }}
  110. pagination={
  111. pagination === false
  112. ? false
  113. : {
  114. pageSize: 10,
  115. }
  116. }
  117. dateFormatter="string"
  118. headerTitle={
  119. <Tooltip title={props.disableAdd ? '暂无权限,请联系管理员' : ''}>
  120. <Button
  121. disabled={props.disableAdd}
  122. onClick={CurdModel.add}
  123. key="button"
  124. icon={<PlusOutlined />}
  125. type="primary"
  126. >
  127. {intl.formatMessage({
  128. id: 'pages.data.option.add',
  129. defaultMessage: '新增',
  130. })}
  131. </Button>
  132. </Tooltip>
  133. }
  134. defaultParams={defaultParams}
  135. // toolBarRender={() =>
  136. // toolBar || [
  137. // <Button onClick={CurdModel.add} key="button" icon={<PlusOutlined />} type="primary">
  138. // {intl.formatMessage({
  139. // id: 'pages.data.option.add',
  140. // defaultMessage: '新增',
  141. // })}
  142. // </Button>,
  143. // menu && (
  144. // <Dropdown key="menu" overlay={menu}>
  145. // <Button>
  146. // <EllipsisOutlined />
  147. // </Button>
  148. // </Dropdown>
  149. // ),
  150. // ]
  151. // }
  152. />
  153. <Save
  154. reload={() => actionRef.current?.reload()}
  155. service={service}
  156. schema={schema}
  157. schemaConfig={schemaConfig}
  158. modelConfig={modelConfig}
  159. formEffect={formEffect}
  160. customForm={form}
  161. footer={footer}
  162. />
  163. </>
  164. );
  165. };
  166. export default BaseCrud;