|
@@ -1,4 +1,4 @@
|
|
|
-import React, { useEffect, useState } from 'react';
|
|
|
|
|
|
|
+import React, { useEffect, useMemo, useState } from 'react';
|
|
|
import { message, Modal, Spin } from 'antd';
|
|
import { message, Modal, Spin } from 'antd';
|
|
|
import {
|
|
import {
|
|
|
ArrayItems,
|
|
ArrayItems,
|
|
@@ -43,20 +43,33 @@ interface Props<T> {
|
|
|
modelConfig?: ModalProps & { loading?: boolean };
|
|
modelConfig?: ModalProps & { loading?: boolean };
|
|
|
formEffect?: () => void;
|
|
formEffect?: () => void;
|
|
|
customForm?: Form1;
|
|
customForm?: Form1;
|
|
|
|
|
+ footer?: React.ReactNode;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
|
const intl = useIntl();
|
|
const intl = useIntl();
|
|
|
- const { service, schema, reload, schemaConfig, modelConfig, formEffect, customForm } = props;
|
|
|
|
|
|
|
+ const { service, schema, reload, schemaConfig, modelConfig, formEffect, customForm, footer } =
|
|
|
|
|
+ props;
|
|
|
|
|
|
|
|
const [visible, setVisible] = useState<boolean>(false);
|
|
const [visible, setVisible] = useState<boolean>(false);
|
|
|
const [current, setCurrent] = useState<T>();
|
|
const [current, setCurrent] = useState<T>();
|
|
|
const [model, setModel] = useState<'edit' | 'add' | 'preview'>('edit');
|
|
const [model, setModel] = useState<'edit' | 'add' | 'preview'>('edit');
|
|
|
|
|
|
|
|
|
|
+ const form = useMemo(
|
|
|
|
|
+ () =>
|
|
|
|
|
+ createForm({
|
|
|
|
|
+ validateFirst: true,
|
|
|
|
|
+ initialValues: current,
|
|
|
|
|
+ effects: formEffect,
|
|
|
|
|
+ }),
|
|
|
|
|
+ [current, model],
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
const visibleSubscription = Store.subscribe(SystemConst.BASE_CURD_MODAL_VISIBLE, setVisible);
|
|
const visibleSubscription = Store.subscribe(SystemConst.BASE_CURD_MODAL_VISIBLE, setVisible);
|
|
|
const dataSubscription = Store.subscribe(SystemConst.BASE_CURD_CURRENT, setCurrent);
|
|
const dataSubscription = Store.subscribe(SystemConst.BASE_CURD_CURRENT, setCurrent);
|
|
|
const modelSubscription = Store.subscribe(SystemConst.BASE_CURD_MODEL, setModel);
|
|
const modelSubscription = Store.subscribe(SystemConst.BASE_CURD_MODEL, setModel);
|
|
|
|
|
+
|
|
|
return () => {
|
|
return () => {
|
|
|
visibleSubscription.unsubscribe();
|
|
visibleSubscription.unsubscribe();
|
|
|
dataSubscription.unsubscribe();
|
|
dataSubscription.unsubscribe();
|
|
@@ -64,12 +77,6 @@ const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
|
};
|
|
};
|
|
|
}, [current]);
|
|
}, [current]);
|
|
|
|
|
|
|
|
- const form = createForm({
|
|
|
|
|
- validateFirst: true,
|
|
|
|
|
- initialValues: current,
|
|
|
|
|
- effects: formEffect,
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
const SchemaField = createSchemaField({
|
|
const SchemaField = createSchemaField({
|
|
|
components: {
|
|
components: {
|
|
|
FormItem,
|
|
FormItem,
|
|
@@ -100,7 +107,9 @@ const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
|
|
|
|
|
|
const save = async () => {
|
|
const save = async () => {
|
|
|
const values: T = await (customForm || form).submit();
|
|
const values: T = await (customForm || form).submit();
|
|
|
|
|
+ console.log(form.values, 'value', values);
|
|
|
// 特殊处理通知模版
|
|
// 特殊处理通知模版
|
|
|
|
|
+
|
|
|
if (service?.getUri().includes('/notifier/template')) {
|
|
if (service?.getUri().includes('/notifier/template')) {
|
|
|
(values as T & { template: Record<string, any> | string }).template = JSON.stringify(
|
|
(values as T & { template: Record<string, any> | string }).template = JSON.stringify(
|
|
|
values.template,
|
|
values.template,
|
|
@@ -121,6 +130,20 @@ const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
|
reload();
|
|
reload();
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ const subscription = Store.subscribe('save-data', async (callback: (values: any) => void) => {
|
|
|
|
|
+ if (!callback) return;
|
|
|
|
|
+ await save();
|
|
|
|
|
+ if (typeof callback === 'function') {
|
|
|
|
|
+ callback(form);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ return () => {
|
|
|
|
|
+ Store.set('save-data', undefined);
|
|
|
|
|
+ subscription.unsubscribe();
|
|
|
|
|
+ };
|
|
|
|
|
+ }, [current]);
|
|
|
|
|
+
|
|
|
return (
|
|
return (
|
|
|
<Modal
|
|
<Modal
|
|
|
title={intl.formatMessage({
|
|
title={intl.formatMessage({
|
|
@@ -130,6 +153,7 @@ const Save = <T extends Record<string, any>>(props: Props<T>) => {
|
|
|
maskClosable={false}
|
|
maskClosable={false}
|
|
|
visible={visible}
|
|
visible={visible}
|
|
|
onCancel={CurdModel.close}
|
|
onCancel={CurdModel.close}
|
|
|
|
|
+ footer={footer}
|
|
|
onOk={save}
|
|
onOk={save}
|
|
|
{...modelConfig}
|
|
{...modelConfig}
|
|
|
>
|
|
>
|