/
index.tsx
111 lines (102 loc) · 2.82 KB
/
index.tsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* eslint-disable no-use-before-define */
import React, { Fragment, useContext } from 'react';
import { Modal, Form, Row, Col, Spin } from 'antd';
import _debounce from 'lodash/debounce';
import { ColProps } from "antd/lib/col";
import { ModalProps } from 'antd/lib/modal';
import { FormComponentProps } from 'antd/lib/form';
import { WrappedFormUtils } from 'antd/lib/form/Form';
import ConfigContext from '../../config-provider/context';
import { ItemConfig } from 'antd-form-mate/dist/lib/props';
import { injectChildren } from '../../utils';
export interface PopupProps extends FormComponentProps {
loading?: boolean;
setItemsConfig: (form: WrappedFormUtils) => ItemConfig[];
itemsLayout?: {
labelCol?: ColProps;
wrapperCol?: ColProps;
};
getFormInstance?: (form: WrappedFormUtils) => void;
mode?: string;
onOk?: (fieldsValue: any) => void;
onClose?: () => void;
visible?: boolean;
afterClose?: () => void;
title?: string;
}
export type CustomModalProps = Omit<ModalProps,
| 'title'
| 'visible'
| 'onOk'
| 'onCancel'
| 'afterClose'
>
export interface DetailModalProps extends PopupProps {
modalProps?: CustomModalProps;
itemsWrapperProps?: React.HTMLAttributes<any>;
cols?: 1 | 2 | 3 | 4 | 6 | 8 | 12 | 24;
children?: JSX.Element;
}
function DetailModal(props: DetailModalProps) {
const { debounceWait, createFormItemsFn } = useContext(ConfigContext);
const {
loading = false,
setItemsConfig,
itemsLayout,
getFormInstance = () => { },
mode,
onOk: handleOk = () => { },
onClose,
visible,
afterClose,
title,
modalProps,
cols = 1,
itemsWrapperProps = {} as any,
form = {} as WrappedFormUtils,
children,
} = props;
getFormInstance(form);
const itemsConfig = setItemsConfig(form);
const onOk = _debounce(() => {
console.log('DetailFormModal _debounce onOk');
form.validateFieldsAndScroll((err?: any, fieldsValue?: any) => {
if (err) return;
handleOk(fieldsValue);
});
}, debounceWait);
const colsItems = cols === 1 ? (
createFormItemsFn(form)(itemsConfig, itemsLayout)
) : (
<Row type="flex">
{createFormItemsFn(form)(itemsConfig, itemsLayout).map(item => {
return (
<Col span={24 / cols} key={item.key as any}>
{item}
</Col>
);
})}
</Row>
);
return (
<Modal
destroyOnClose
{...modalProps}
onOk={onOk}
onCancel={onClose}
visible={visible}
afterClose={afterClose}
title={title}
>
<Fragment>
<div {...itemsWrapperProps}>
<Spin spinning={loading}>
{colsItems}
</Spin>
</div>
{mode ? injectChildren(children, { mode }) : children}
</Fragment>
</Modal>
);
}
export default Form.create<DetailModalProps>()(DetailModal);