index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { useEffect, useState } from 'react';
  2. import { message, Modal } from 'antd';
  3. import {
  4. NumberPicker,
  5. Editable,
  6. Form,
  7. FormItem,
  8. Input,
  9. Password,
  10. Upload,
  11. PreviewText,
  12. FormTab,
  13. Select,
  14. ArrayTable,
  15. Switch,
  16. FormGrid,
  17. } from '@formily/antd';
  18. import { createForm } from '@formily/core';
  19. import { createSchemaField } from '@formily/react';
  20. import * as ICONS from '@ant-design/icons';
  21. import { useIntl } from '@@/plugin-locale/localeExports';
  22. import type { ISchema } from '@formily/json-schema';
  23. import type BaseService from '@/utils/BaseService';
  24. import { Store } from 'jetlinks-store';
  25. import SystemConst from '@/utils/const';
  26. import { CurdModel } from '@/components/BaseCrud/model';
  27. import type { ISchemaFieldProps } from '@formily/react/lib/types';
  28. import type { ModalProps } from 'antd/lib/modal/Modal';
  29. interface Props<T> {
  30. schema: ISchema;
  31. service: BaseService<T>;
  32. reload: () => void;
  33. schemaConfig?: ISchemaFieldProps;
  34. modelConfig?: ModalProps;
  35. }
  36. const Save = <T extends Record<string, any>>(props: Props<T>) => {
  37. const intl = useIntl();
  38. const { service, schema, reload, schemaConfig, modelConfig } = props;
  39. const [visible, setVisible] = useState<boolean>(false);
  40. const [current, setCurrent] = useState<T>();
  41. const [model, setModel] = useState<'edit' | 'add' | 'preview'>('edit');
  42. useEffect(() => {
  43. const visibleSubscription = Store.subscribe(SystemConst.BASE_CURD_MODAL_VISIBLE, setVisible);
  44. const dataSubscription = Store.subscribe(SystemConst.BASE_CURD_CURRENT, setCurrent);
  45. const modelSubscription = Store.subscribe(SystemConst.BASE_CURD_MODEL, setModel);
  46. return () => {
  47. visibleSubscription.unsubscribe();
  48. dataSubscription.unsubscribe();
  49. modelSubscription.unsubscribe();
  50. };
  51. }, [current]);
  52. const form = createForm({
  53. validateFirst: true,
  54. initialValues: current,
  55. });
  56. const SchemaField = createSchemaField({
  57. components: {
  58. FormItem,
  59. FormTab,
  60. Input,
  61. Password,
  62. Upload,
  63. Select,
  64. ArrayTable,
  65. Switch,
  66. FormGrid,
  67. Editable,
  68. NumberPicker,
  69. },
  70. scope: {
  71. icon(name: any) {
  72. return React.createElement(ICONS[name]);
  73. },
  74. },
  75. });
  76. const save = async () => {
  77. const values: T = await form.submit();
  78. await service.update(values);
  79. message.success(
  80. intl.formatMessage({
  81. id: 'pages.data.option.success',
  82. defaultMessage: '操作成功',
  83. }),
  84. );
  85. CurdModel.close();
  86. reload();
  87. };
  88. return (
  89. <Modal
  90. title={intl.formatMessage({
  91. id: `pages.data.option.${model}`,
  92. defaultMessage: '编辑',
  93. })}
  94. visible={visible}
  95. onCancel={CurdModel.close}
  96. onOk={save}
  97. {...modelConfig}
  98. >
  99. <PreviewText.Placeholder value="-">
  100. <Form form={form} labelCol={5} wrapperCol={16}>
  101. <SchemaField schema={schema} {...schemaConfig} />
  102. </Form>
  103. </PreviewText.Placeholder>
  104. </Modal>
  105. );
  106. };
  107. export default Save;