index.tsx
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { useEffect } from 'react';
import { Modal, ModalProps } from 'antd';
import { FormProvider } from '@formily/react';
import { createForm } from '@formily/core';
interface Props extends ModalProps {
handleOk?: (values: { [props: string]: string }, e: React.MouseEvent<HTMLButtonElement>) => void;
handleCancel?: (e: React.MouseEvent<HTMLButtonElement>) => void;
initialValues?: { props?: string };
open: boolean;
}
const FormModal: React.FC<Props> = (props) => {
const form = createForm();
const {
initialValues,
title = '我是弹框标题',
handleOk,
handleCancel,
children = [],
open,
...args
} = props;
const items = React.Children.toArray(children);
useEffect(() => {
form.setInitialValues(initialValues);
});
const onOK = (e: React.MouseEvent<HTMLButtonElement>) => {
if (handleOk) {
form.submit().then((values: any) => {
handleOk(values, e);
});
return;
}
};
const onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
if (handleCancel) {
handleCancel(e);
return;
}
};
return (
<Modal
title={title}
open={open}
onOk={onOK}
onCancel={onCancel}
maskClosable={false}
centered
{...args}
getContainer={false}
>
<FormProvider form={form}>{React.Children.map(items, (item: any) => item)}</FormProvider>
</Modal>
);
};
export default FormModal;