@hzab/list-render 1.10.21-alpha.4 → 1.10.21-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/DetailModal/index.jsx +120 -120
- package/src/FormModal/index.tsx +210 -210
- package/src/components/CellEditTable/Template/ExpandListTemplate/index.tsx +34 -57
- package/src/components/CellEditTable/Template/GetBaseTemplate.tsx +33 -0
- package/src/components/CellEditTable/Template/widthWrap.tsx +25 -11
- package/src/components/CellEditTable/common/WidthWrapHoc.tsx +215 -0
- package/src/components/CellEditTable/common/childAntdComponent.ts +14 -0
- package/src/components/CellEditTable/common/utils.ts +257 -0
- package/src/components/CellEditTable/index.less +18 -0
- package/src/components/CellEditTable/index.tsx +94 -122
- package/src/table-render/index.jsx +124 -119
- package/src/components/CellEditTable/Template/SelectCellTemplate.tsx +0 -202
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hzab/list-render",
|
|
3
|
-
"version": "1.10.21-alpha.
|
|
3
|
+
"version": "1.10.21-alpha.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"lib": "lib"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
+
"@hzab/edit-table": "^0.0.1",
|
|
69
70
|
"@silevis/reactgrid": "^4.1.17",
|
|
70
71
|
"array-move": "^4.0.0",
|
|
71
72
|
"react-sortable-hoc": "^2.0.0"
|
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
2
|
-
|
|
3
|
-
import { Modal, Drawer, Button } from "antd";
|
|
4
|
-
import Descriptions from "@hzab/schema-descriptions";
|
|
5
|
-
|
|
6
|
-
import "./index.less";
|
|
7
|
-
|
|
8
|
-
let _formData = null;
|
|
9
|
-
|
|
10
|
-
function DetailModal(props, parentRef) {
|
|
11
|
-
const { modalMode, Slots = {}, modalConf = {}, modalProps = {} } = props;
|
|
12
|
-
const [open, setOpen] = useState(false);
|
|
13
|
-
const [data, setData] = useState({});
|
|
14
|
-
const [title, setTitle] = useState("详情");
|
|
15
|
-
|
|
16
|
-
const formRef = useRef();
|
|
17
|
-
function show(formData = props.formInitialValues, title) {
|
|
18
|
-
setOpen(true);
|
|
19
|
-
// 处理 formRef.current 为 undefined 的问题
|
|
20
|
-
setData(formData);
|
|
21
|
-
setTitle(title || "详情");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function close() {
|
|
25
|
-
props.onClose && props.onClose();
|
|
26
|
-
setOpen(false);
|
|
27
|
-
formRef.current?.formRender?.reset();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
useImperativeHandle(parentRef, () => ({
|
|
31
|
-
show,
|
|
32
|
-
close,
|
|
33
|
-
cancel: close,
|
|
34
|
-
formRef,
|
|
35
|
-
}));
|
|
36
|
-
|
|
37
|
-
// Hack: 解决 FormModal DetailModal 相互影响的问题
|
|
38
|
-
if (!open) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let footer = undefined;
|
|
43
|
-
const options = {
|
|
44
|
-
close,
|
|
45
|
-
formRef,
|
|
46
|
-
scenario: "detail",
|
|
47
|
-
};
|
|
48
|
-
if (modalConf?.footer) {
|
|
49
|
-
footer = typeof modalConf?.footer === "function" ? modalConf.footer({ ...options, options }) : modalConf?.footer;
|
|
50
|
-
} else {
|
|
51
|
-
footer = [];
|
|
52
|
-
|
|
53
|
-
if (Slots.modalFooterPre) {
|
|
54
|
-
footer.push(<Slots.modalFooterPre key="pre" options={options} />);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
footer.push(
|
|
58
|
-
<Button key="confirm" onClick={close}>
|
|
59
|
-
{modalConf.okText || "关 闭"}
|
|
60
|
-
</Button>,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
if (Slots.modalFooterSuffix) {
|
|
64
|
-
footer.push(<Slots.modalFooterSuffix key="suffix" options={options} />);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 解决 show 函数中 formRef.current 为 undefined 的问题
|
|
70
|
-
*/
|
|
71
|
-
function didMount() {
|
|
72
|
-
props.modalFormMount && props.modalFormMount();
|
|
73
|
-
if (_formData) {
|
|
74
|
-
setData(_formData);
|
|
75
|
-
_formData = null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const CModal = modalMode === "drawer" ? Drawer : Modal;
|
|
80
|
-
const _modalProps = {
|
|
81
|
-
className: "detail-modal",
|
|
82
|
-
wrapClassName: "detail-modal",
|
|
83
|
-
title: title,
|
|
84
|
-
visible: open,
|
|
85
|
-
open: open,
|
|
86
|
-
onClose: close,
|
|
87
|
-
onCancel: close,
|
|
88
|
-
footer: footer,
|
|
89
|
-
maskClosable: modalProps.maskClosable || false,
|
|
90
|
-
width: modalConf?.width ?? 720,
|
|
91
|
-
// 解决弹窗不销毁,表单远程数据没有更新的问题
|
|
92
|
-
destroyOnClose: true,
|
|
93
|
-
...modalProps,
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
return (
|
|
97
|
-
<CModal {..._modalProps}>
|
|
98
|
-
<Descriptions
|
|
99
|
-
{...props.detailProps}
|
|
100
|
-
components={props.components}
|
|
101
|
-
schema={props.schema}
|
|
102
|
-
schemaScope={{
|
|
103
|
-
scenario: "detail",
|
|
104
|
-
...(props.schemaScope || {}),
|
|
105
|
-
}}
|
|
106
|
-
data={data}
|
|
107
|
-
/>
|
|
108
|
-
<DidMount didMount={didMount} />
|
|
109
|
-
</CModal>
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function DidMount(props) {
|
|
114
|
-
useEffect(() => {
|
|
115
|
-
props.didMount();
|
|
116
|
-
}, []);
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export default forwardRef(DetailModal);
|
|
1
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { Modal, Drawer, Button } from "antd";
|
|
4
|
+
import Descriptions from "@hzab/schema-descriptions";
|
|
5
|
+
|
|
6
|
+
import "./index.less";
|
|
7
|
+
|
|
8
|
+
let _formData = null;
|
|
9
|
+
|
|
10
|
+
function DetailModal(props, parentRef) {
|
|
11
|
+
const { modalMode, Slots = {}, modalConf = {}, modalProps = {} } = props;
|
|
12
|
+
const [open, setOpen] = useState(false);
|
|
13
|
+
const [data, setData] = useState({});
|
|
14
|
+
const [title, setTitle] = useState("详情");
|
|
15
|
+
|
|
16
|
+
const formRef = useRef();
|
|
17
|
+
function show(formData = props.formInitialValues, title) {
|
|
18
|
+
setOpen(true);
|
|
19
|
+
// 处理 formRef.current 为 undefined 的问题
|
|
20
|
+
setData(formData);
|
|
21
|
+
setTitle(title || "详情");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function close() {
|
|
25
|
+
props.onClose && props.onClose();
|
|
26
|
+
setOpen(false);
|
|
27
|
+
formRef.current?.formRender?.reset();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
useImperativeHandle(parentRef, () => ({
|
|
31
|
+
show,
|
|
32
|
+
close,
|
|
33
|
+
cancel: close,
|
|
34
|
+
formRef,
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
// Hack: 解决 FormModal DetailModal 相互影响的问题
|
|
38
|
+
if (!open) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let footer = undefined;
|
|
43
|
+
const options = {
|
|
44
|
+
close,
|
|
45
|
+
formRef,
|
|
46
|
+
scenario: "detail",
|
|
47
|
+
};
|
|
48
|
+
if (modalConf?.footer) {
|
|
49
|
+
footer = typeof modalConf?.footer === "function" ? modalConf.footer({ ...options, options }) : modalConf?.footer;
|
|
50
|
+
} else {
|
|
51
|
+
footer = [];
|
|
52
|
+
|
|
53
|
+
if (Slots.modalFooterPre) {
|
|
54
|
+
footer.push(<Slots.modalFooterPre key="pre" options={options} />);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
footer.push(
|
|
58
|
+
<Button key="confirm" onClick={close}>
|
|
59
|
+
{modalConf.okText || "关 闭"}
|
|
60
|
+
</Button>,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (Slots.modalFooterSuffix) {
|
|
64
|
+
footer.push(<Slots.modalFooterSuffix key="suffix" options={options} />);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 解决 show 函数中 formRef.current 为 undefined 的问题
|
|
70
|
+
*/
|
|
71
|
+
function didMount() {
|
|
72
|
+
props.modalFormMount && props.modalFormMount();
|
|
73
|
+
if (_formData) {
|
|
74
|
+
setData(_formData);
|
|
75
|
+
_formData = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const CModal = modalMode === "drawer" ? Drawer : Modal;
|
|
80
|
+
const _modalProps = {
|
|
81
|
+
className: "detail-modal",
|
|
82
|
+
wrapClassName: "detail-modal",
|
|
83
|
+
title: title,
|
|
84
|
+
visible: open,
|
|
85
|
+
open: open,
|
|
86
|
+
onClose: close,
|
|
87
|
+
onCancel: close,
|
|
88
|
+
footer: footer,
|
|
89
|
+
maskClosable: modalProps.maskClosable || false,
|
|
90
|
+
width: modalConf?.width ?? 720,
|
|
91
|
+
// 解决弹窗不销毁,表单远程数据没有更新的问题
|
|
92
|
+
destroyOnClose: true,
|
|
93
|
+
...modalProps,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<CModal {..._modalProps}>
|
|
98
|
+
<Descriptions
|
|
99
|
+
{...props.detailProps}
|
|
100
|
+
components={props.components}
|
|
101
|
+
schema={props.schema}
|
|
102
|
+
schemaScope={{
|
|
103
|
+
scenario: "detail",
|
|
104
|
+
...(props.schemaScope || {}),
|
|
105
|
+
}}
|
|
106
|
+
data={data}
|
|
107
|
+
/>
|
|
108
|
+
<DidMount didMount={didMount} />
|
|
109
|
+
</CModal>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function DidMount(props) {
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
props.didMount();
|
|
116
|
+
}, []);
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default forwardRef(DetailModal);
|
package/src/FormModal/index.tsx
CHANGED
|
@@ -1,210 +1,210 @@
|
|
|
1
|
-
import { forwardRef, useEffect, useImperativeHandle, useRef, useState, useMemo } from "react";
|
|
2
|
-
import _ from "lodash";
|
|
3
|
-
|
|
4
|
-
import { Modal, Drawer, Button, message } from "antd";
|
|
5
|
-
|
|
6
|
-
import FormRender from "@hzab/form-render";
|
|
7
|
-
|
|
8
|
-
import "./index.less";
|
|
9
|
-
|
|
10
|
-
let _formData = null;
|
|
11
|
-
|
|
12
|
-
export interface IFormRender {
|
|
13
|
-
setValues: Function;
|
|
14
|
-
reset: Function;
|
|
15
|
-
validate: Function;
|
|
16
|
-
values: Object;
|
|
17
|
-
}
|
|
18
|
-
export interface IFormRef {
|
|
19
|
-
formRender: IFormRender;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 表单弹层,包含弹窗、抽屉两种模式
|
|
24
|
-
* @param props
|
|
25
|
-
* @param parentRef
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
export function FormModal(props, parentRef) {
|
|
29
|
-
const { modalMode, Slots = {}, modalConf = {}, modalProps = {} } = props;
|
|
30
|
-
const [loading, setLoading] = useState(false);
|
|
31
|
-
const [title, setTitle] = useState("新增");
|
|
32
|
-
const [open, setOpen] = useState(false);
|
|
33
|
-
const formRef = useRef<IFormRef>();
|
|
34
|
-
const scenarioRef = useRef<string>("create");
|
|
35
|
-
|
|
36
|
-
const FormSlot = useMemo(() => props.Slots?.FormSlot, [props.Slots?.FormSlot]);
|
|
37
|
-
|
|
38
|
-
function show(formData = props.formInitialValues, title, scenario = "create") {
|
|
39
|
-
scenarioRef.current = scenario || "create";
|
|
40
|
-
setOpen(true);
|
|
41
|
-
// 处理 formRef.current 为 undefined 的问题
|
|
42
|
-
if (formRef.current?.formRender?.setValues) {
|
|
43
|
-
formRef.current?.formRender?.setValues(formData);
|
|
44
|
-
_formData = null;
|
|
45
|
-
} else {
|
|
46
|
-
_formData = formData;
|
|
47
|
-
}
|
|
48
|
-
setTitle(title || "新增");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function close() {
|
|
52
|
-
props.onClose && props.onClose();
|
|
53
|
-
setOpen(false);
|
|
54
|
-
formRef.current?.formRender?.reset();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
useImperativeHandle(parentRef, () => ({
|
|
58
|
-
show,
|
|
59
|
-
close,
|
|
60
|
-
cancel: close,
|
|
61
|
-
onOk,
|
|
62
|
-
formRef,
|
|
63
|
-
}));
|
|
64
|
-
|
|
65
|
-
// Hack: 解决 FormModal DetailModal 相互影响的问题
|
|
66
|
-
if (!open) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function onOk() {
|
|
71
|
-
validate().then(async () => {
|
|
72
|
-
const submitForm = _.cloneDeep(await formRef.current?.formRender?.values);
|
|
73
|
-
if (modalConf.beforeSubmit) {
|
|
74
|
-
const isContinue = await modalConf.beforeSubmit(submitForm, {
|
|
75
|
-
cancel: close,
|
|
76
|
-
formRef,
|
|
77
|
-
scenario: scenarioRef.current,
|
|
78
|
-
});
|
|
79
|
-
if (isContinue === false) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
setLoading(true);
|
|
86
|
-
await (props.onSubmit && props.onSubmit(submitForm));
|
|
87
|
-
close();
|
|
88
|
-
} catch (error) {
|
|
89
|
-
console.error(error);
|
|
90
|
-
}
|
|
91
|
-
setLoading(false);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 校验表单
|
|
97
|
-
* @returns
|
|
98
|
-
*/
|
|
99
|
-
function validate(hideMessage = false) {
|
|
100
|
-
return new Promise((resolve, reject) => {
|
|
101
|
-
formRef.current?.formRender
|
|
102
|
-
?.validate()
|
|
103
|
-
.then((values) => {
|
|
104
|
-
resolve(values);
|
|
105
|
-
})
|
|
106
|
-
.catch((err) => {
|
|
107
|
-
reject(err);
|
|
108
|
-
console.error("Error validate: ", err);
|
|
109
|
-
!hideMessage && message.error("输入有误!");
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let footer = undefined;
|
|
115
|
-
const options = {
|
|
116
|
-
cancel: close,
|
|
117
|
-
onOk,
|
|
118
|
-
close,
|
|
119
|
-
formRef,
|
|
120
|
-
validate,
|
|
121
|
-
scenario: scenarioRef.current,
|
|
122
|
-
};
|
|
123
|
-
if (modalConf?.footer) {
|
|
124
|
-
footer = typeof modalConf?.footer === "function" ? modalConf.footer({ ...options, options }) : modalConf?.footer;
|
|
125
|
-
} else {
|
|
126
|
-
footer = [];
|
|
127
|
-
|
|
128
|
-
if (Slots.modalFooterPre) {
|
|
129
|
-
footer.push(<Slots.modalFooterPre key="pre" options={options} />);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
footer.push(
|
|
133
|
-
<Button key="cancel" onClick={close}>
|
|
134
|
-
{modalConf.cancelText || "取 消"}
|
|
135
|
-
</Button>,
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
if (Slots.modalFooterCenter) {
|
|
139
|
-
footer.push(<Slots.modalFooterCenter key="center" options={options} />);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
footer.push(
|
|
143
|
-
<Button key="confirm" type="primary" onClick={onOk} loading={loading}>
|
|
144
|
-
{modalConf.okText || "确 定"}
|
|
145
|
-
</Button>,
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
if (Slots.modalFooterSuffix) {
|
|
149
|
-
footer.push(<Slots.modalFooterSuffix key="suffix" options={options} />);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 解决 show 函数中 formRef.current 为 undefined 的问题
|
|
155
|
-
*/
|
|
156
|
-
function didMount() {
|
|
157
|
-
props.modalFormMount && props.modalFormMount();
|
|
158
|
-
if (_formData) {
|
|
159
|
-
formRef.current?.formRender?.setValues(_formData);
|
|
160
|
-
_formData = null;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const CModal = modalMode === "drawer" ? Drawer : Modal;
|
|
165
|
-
const _modalProps = {
|
|
166
|
-
className: "form-modal",
|
|
167
|
-
wrapClassName: "form-modal",
|
|
168
|
-
title: title,
|
|
169
|
-
visible: open,
|
|
170
|
-
open: open,
|
|
171
|
-
onClose: close,
|
|
172
|
-
onCancel: close,
|
|
173
|
-
onOk: onOk,
|
|
174
|
-
footer: footer,
|
|
175
|
-
maskClosable: modalProps.maskClosable || false,
|
|
176
|
-
width: modalConf?.width ?? 720,
|
|
177
|
-
// 解决弹窗不销毁,表单远程数据没有更新的问题
|
|
178
|
-
destroyOnClose: true,
|
|
179
|
-
...modalProps,
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
return (
|
|
183
|
-
<CModal {..._modalProps}>
|
|
184
|
-
{FormSlot ? (
|
|
185
|
-
<FormSlot {...props} formRef={formRef} scenario={scenarioRef.current} schema={props.schema} />
|
|
186
|
-
) : (
|
|
187
|
-
<FormRender
|
|
188
|
-
{...props.formProps}
|
|
189
|
-
ref={formRef}
|
|
190
|
-
schema={props.schema}
|
|
191
|
-
schemaScope={{
|
|
192
|
-
scenario: scenarioRef.current,
|
|
193
|
-
...(props.schemaScope || {}),
|
|
194
|
-
}}
|
|
195
|
-
components={props.components}
|
|
196
|
-
></FormRender>
|
|
197
|
-
)}
|
|
198
|
-
<DidMount didMount={didMount} />
|
|
199
|
-
</CModal>
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function DidMount(props) {
|
|
204
|
-
useEffect(() => {
|
|
205
|
-
props.didMount();
|
|
206
|
-
}, []);
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export default forwardRef(FormModal);
|
|
1
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef, useState, useMemo } from "react";
|
|
2
|
+
import _ from "lodash";
|
|
3
|
+
|
|
4
|
+
import { Modal, Drawer, Button, message } from "antd";
|
|
5
|
+
|
|
6
|
+
import FormRender from "@hzab/form-render";
|
|
7
|
+
|
|
8
|
+
import "./index.less";
|
|
9
|
+
|
|
10
|
+
let _formData = null;
|
|
11
|
+
|
|
12
|
+
export interface IFormRender {
|
|
13
|
+
setValues: Function;
|
|
14
|
+
reset: Function;
|
|
15
|
+
validate: Function;
|
|
16
|
+
values: Object;
|
|
17
|
+
}
|
|
18
|
+
export interface IFormRef {
|
|
19
|
+
formRender: IFormRender;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 表单弹层,包含弹窗、抽屉两种模式
|
|
24
|
+
* @param props
|
|
25
|
+
* @param parentRef
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
export function FormModal(props, parentRef) {
|
|
29
|
+
const { modalMode, Slots = {}, modalConf = {}, modalProps = {} } = props;
|
|
30
|
+
const [loading, setLoading] = useState(false);
|
|
31
|
+
const [title, setTitle] = useState("新增");
|
|
32
|
+
const [open, setOpen] = useState(false);
|
|
33
|
+
const formRef = useRef<IFormRef>();
|
|
34
|
+
const scenarioRef = useRef<string>("create");
|
|
35
|
+
|
|
36
|
+
const FormSlot = useMemo(() => props.Slots?.FormSlot, [props.Slots?.FormSlot]);
|
|
37
|
+
|
|
38
|
+
function show(formData = props.formInitialValues, title, scenario = "create") {
|
|
39
|
+
scenarioRef.current = scenario || "create";
|
|
40
|
+
setOpen(true);
|
|
41
|
+
// 处理 formRef.current 为 undefined 的问题
|
|
42
|
+
if (formRef.current?.formRender?.setValues) {
|
|
43
|
+
formRef.current?.formRender?.setValues(formData);
|
|
44
|
+
_formData = null;
|
|
45
|
+
} else {
|
|
46
|
+
_formData = formData;
|
|
47
|
+
}
|
|
48
|
+
setTitle(title || "新增");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function close() {
|
|
52
|
+
props.onClose && props.onClose();
|
|
53
|
+
setOpen(false);
|
|
54
|
+
formRef.current?.formRender?.reset();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
useImperativeHandle(parentRef, () => ({
|
|
58
|
+
show,
|
|
59
|
+
close,
|
|
60
|
+
cancel: close,
|
|
61
|
+
onOk,
|
|
62
|
+
formRef,
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
// Hack: 解决 FormModal DetailModal 相互影响的问题
|
|
66
|
+
if (!open) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function onOk() {
|
|
71
|
+
validate().then(async () => {
|
|
72
|
+
const submitForm = _.cloneDeep(await formRef.current?.formRender?.values);
|
|
73
|
+
if (modalConf.beforeSubmit) {
|
|
74
|
+
const isContinue = await modalConf.beforeSubmit(submitForm, {
|
|
75
|
+
cancel: close,
|
|
76
|
+
formRef,
|
|
77
|
+
scenario: scenarioRef.current,
|
|
78
|
+
});
|
|
79
|
+
if (isContinue === false) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
setLoading(true);
|
|
86
|
+
await (props.onSubmit && props.onSubmit(submitForm));
|
|
87
|
+
close();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error(error);
|
|
90
|
+
}
|
|
91
|
+
setLoading(false);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 校验表单
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
function validate(hideMessage = false) {
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
formRef.current?.formRender
|
|
102
|
+
?.validate()
|
|
103
|
+
.then((values) => {
|
|
104
|
+
resolve(values);
|
|
105
|
+
})
|
|
106
|
+
.catch((err) => {
|
|
107
|
+
reject(err);
|
|
108
|
+
console.error("Error validate: ", err);
|
|
109
|
+
!hideMessage && message.error("输入有误!");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let footer = undefined;
|
|
115
|
+
const options = {
|
|
116
|
+
cancel: close,
|
|
117
|
+
onOk,
|
|
118
|
+
close,
|
|
119
|
+
formRef,
|
|
120
|
+
validate,
|
|
121
|
+
scenario: scenarioRef.current,
|
|
122
|
+
};
|
|
123
|
+
if (modalConf?.footer) {
|
|
124
|
+
footer = typeof modalConf?.footer === "function" ? modalConf.footer({ ...options, options }) : modalConf?.footer;
|
|
125
|
+
} else {
|
|
126
|
+
footer = [];
|
|
127
|
+
|
|
128
|
+
if (Slots.modalFooterPre) {
|
|
129
|
+
footer.push(<Slots.modalFooterPre key="pre" options={options} />);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
footer.push(
|
|
133
|
+
<Button key="cancel" onClick={close}>
|
|
134
|
+
{modalConf.cancelText || "取 消"}
|
|
135
|
+
</Button>,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (Slots.modalFooterCenter) {
|
|
139
|
+
footer.push(<Slots.modalFooterCenter key="center" options={options} />);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
footer.push(
|
|
143
|
+
<Button key="confirm" type="primary" onClick={onOk} loading={loading}>
|
|
144
|
+
{modalConf.okText || "确 定"}
|
|
145
|
+
</Button>,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (Slots.modalFooterSuffix) {
|
|
149
|
+
footer.push(<Slots.modalFooterSuffix key="suffix" options={options} />);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 解决 show 函数中 formRef.current 为 undefined 的问题
|
|
155
|
+
*/
|
|
156
|
+
function didMount() {
|
|
157
|
+
props.modalFormMount && props.modalFormMount();
|
|
158
|
+
if (_formData) {
|
|
159
|
+
formRef.current?.formRender?.setValues(_formData);
|
|
160
|
+
_formData = null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const CModal = modalMode === "drawer" ? Drawer : Modal;
|
|
165
|
+
const _modalProps = {
|
|
166
|
+
className: "form-modal",
|
|
167
|
+
wrapClassName: "form-modal",
|
|
168
|
+
title: title,
|
|
169
|
+
visible: open,
|
|
170
|
+
open: open,
|
|
171
|
+
onClose: close,
|
|
172
|
+
onCancel: close,
|
|
173
|
+
onOk: onOk,
|
|
174
|
+
footer: footer,
|
|
175
|
+
maskClosable: modalProps.maskClosable || false,
|
|
176
|
+
width: modalConf?.width ?? 720,
|
|
177
|
+
// 解决弹窗不销毁,表单远程数据没有更新的问题
|
|
178
|
+
destroyOnClose: true,
|
|
179
|
+
...modalProps,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<CModal {..._modalProps}>
|
|
184
|
+
{FormSlot ? (
|
|
185
|
+
<FormSlot {...props} formRef={formRef} scenario={scenarioRef.current} schema={props.schema} />
|
|
186
|
+
) : (
|
|
187
|
+
<FormRender
|
|
188
|
+
{...props.formProps}
|
|
189
|
+
ref={formRef}
|
|
190
|
+
schema={props.schema}
|
|
191
|
+
schemaScope={{
|
|
192
|
+
scenario: scenarioRef.current,
|
|
193
|
+
...(props.schemaScope || {}),
|
|
194
|
+
}}
|
|
195
|
+
components={props.components}
|
|
196
|
+
></FormRender>
|
|
197
|
+
)}
|
|
198
|
+
<DidMount didMount={didMount} />
|
|
199
|
+
</CModal>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function DidMount(props) {
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
props.didMount();
|
|
206
|
+
}, []);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export default forwardRef(FormModal);
|