-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
131 lines (116 loc) · 2.96 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { PropsWithChildren, forwardRef, useImperativeHandle, useState } from 'react'
import { forwardrefHOC } from 'react-foundations-utils'
import { Button, ColProps, Form, FormInstance, FormProps, Modal, ModalProps } from 'antd'
import HangarForm, { HandleFormItemType } from './handleForm'
import styles from './index.less'
export type FormModalProps<
T extends Record<string, any>,
K extends string
> = PropsWithChildren &
FormProps & {
actionType: 'add' | 'edit' | 'preview'
formValues?: T
modalProps?: ModalProps
dictionary: Record<K, string>
items?: HandleFormItemType<keyof T>[]
span?: ColProps['span']
onSubmit: (vlaues: T) => any
renderForm?: (hangarForm: React.ReactElement) => React.ReactNode
}
// 操作类型文本
export const ActTypeTextMap = {
add: '新增',
edit: '编辑',
preview: '查看',
}
export type FormModalRef = {
form: FormInstance
setOpen: (open: boolean) => void
}
function FormModal<
T extends Record<string, any>,
K extends string
>({
children,
actionType,
formValues,
dictionary,
items,
modalProps,
span,
onSubmit,
onError,
renderForm,
...formProps
}: FormModalProps<T, K>, ref: React.Ref<FormModalRef>) {
const [open, setOpen] = useState(false)
const [form] = Form.useForm()
const handleClickBtn = () => {
setOpen(true)
}
const handleOk = async (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
await form
.validateFields()
.then(async (values) => {
await onSubmit(values)
form.resetFields()
setOpen(false)
})
.catch((error) => {
onError?.(error)
})
modalProps?.onOk?.(e)
}
const handleCancel = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
setOpen(false)
modalProps?.onCancel?.(e)
}
useImperativeHandle(ref, () => ({
form,
setOpen,
}), [form])
const handleForm = (
<HangarForm<T, K>
actionType={actionType}
values={formValues}
items={items}
dictionary={dictionary}
form={form}
span={span}
{...formProps}
/>
)
return (
<>
{React.isValidElement(children) ? (
React.cloneElement(children, {
...children?.props,
onClick: (...args: any) => {
handleClickBtn()
return children?.props?.onClick?.(...args)
},
})
) : (
<Button type="primary" onClick={handleClickBtn}>
{children}
</Button>
)}
<Modal
open={open}
title={modalProps?.title ?? `${ActTypeTextMap[actionType]}`}
okText="确认"
cancelText="取消"
destroyOnClose
maskClosable={false}
width={600}
{...modalProps}
onCancel={handleCancel}
onOk={handleOk}
className={`${styles.modal} ${modalProps?.className || ''}`}
>
{renderForm ? renderForm(handleForm) : handleForm}
</Modal>
</>
)
}
export default forwardrefHOC(FormModal)