This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathFieldContentEditor.tsx
More file actions
283 lines (259 loc) · 7.46 KB
/
FieldContentEditor.tsx
File metadata and controls
283 lines (259 loc) · 7.46 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import React, { Suspense } from 'react'
import { Spin, Form, Input, Switch, Button, Select, InputNumber, Typography } from 'antd'
import { Rule } from 'antd/es/form'
import { IConnectEditor, IDatePicker, IFileAndImageEditor } from '@/components/Fields'
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
import { IObjectEditor } from './Object'
import { FormListFieldData } from 'antd/es/form/FormList'
const MarkdownEditor = React.lazy(() => import('@/components/Fields/Markdown'))
const RichTextEditor = React.lazy(() => import('@/components/Fields/RichText'))
const { TextArea } = Input
const { Option } = Select
const { Text } = Typography
const LazyMarkdownEditor: React.FC<{ id: number }> = (props: any) => (
<Suspense fallback={<Spin />}>
<MarkdownEditor {...props} />
</Suspense>
)
const LazyRichTextEditor: React.FC<{ id: number }> = (props: any) => (
<Suspense fallback={<Spin />}>
<RichTextEditor {...props} />
</Suspense>
)
/**
* 根据类型获取验证规则
*/
function getValidateRule(type: string) {
let rule: Rule | null
switch (type) {
case 'Url':
rule = { type: 'url', message: '请输入正确的网址' }
break
case 'Email':
rule = {
type: 'email',
message: '请输入正确的邮箱',
}
break
case 'Number':
rule = { type: 'number', message: '请输入正确的数字' }
break
case 'Tel':
rule = {
pattern: /^\d+$/,
message: '请输入正确的电话号码',
}
break
default:
rule = null
}
return rule
}
const getRules = (field: SchemaField): Rule[] => {
const { isRequired, displayName, min, max, type } = field
const rules: Rule[] = []
if (isRequired) {
rules.push({ required: isRequired, message: `${displayName} 字段是必须要的` })
}
if (min) {
const validType = type === 'String' || type === 'MultiLineString' ? 'string' : 'number'
rules.push({
min,
type: validType,
message: validType === 'string' ? `不能小于最小长度 ${min}` : `不能小于最小值 ${min}`,
})
}
if (max) {
const validType = type === 'String' || type === 'MultiLineString' ? 'string' : 'number'
rules.push({
max,
type: validType,
message: validType === 'string' ? `不能大于最大长度 ${max}` : `不能大于最大值 ${max}`,
})
}
const rule = getValidateRule(field.type)
rule && rules.push(rule)
return rules
}
/**
* 字段编辑器
*/
export function getFieldEditor(field: SchemaField, key: number) {
const { name, type, min, max, enumElements } = field
let FieldEditor: React.ReactNode
switch (type) {
case 'String':
FieldEditor = <Input type="text" />
break
case 'MultiLineString':
FieldEditor = <TextArea />
break
case 'Boolean':
FieldEditor = <Switch checkedChildren="True" unCheckedChildren="False" />
break
case 'Number':
FieldEditor = <InputNumber style={{ width: '100%' }} min={min} max={max} />
break
case 'Url':
FieldEditor = <Input />
break
case 'Email':
FieldEditor = <Input />
break
case 'Tel':
FieldEditor = <Input style={{ width: '100%' }} />
break
case 'Time':
case 'Date':
case 'DateTime':
FieldEditor = <IDatePicker type={type} dateFormatType={field.dateFormatType} />
break
case 'Image':
FieldEditor = (
<IFileAndImageEditor type="image" field={field} resourceLinkType={field.resourceLinkType} />
)
break
case 'File':
case 'Media':
FieldEditor = (
<IFileAndImageEditor type="file" field={field} resourceLinkType={field.resourceLinkType} />
)
break
case 'Enum':
FieldEditor = (
<Select>
{enumElements?.length ? (
enumElements?.map((ele, index) => (
<Option value={ele.value} key={index}>
{ele.label}
</Option>
))
) : (
<Option value="" disabled>
空
</Option>
)}
</Select>
)
break
case 'Array':
const schemaField = field as SchemaField & { [key: string]: any };
const renderItem = (formField: FormListFieldData) => {
switch (schemaField.elementType) {
case 'Enum':
return (
<Form.Item {...formField} noStyle validateTrigger={['onChange', 'onBlur']}>
<Select style={{ width: '40%' }} >
{enumElements?.length ? (
enumElements?.map((ele, index) => (
<Option value={ele.value} key={index}>
{ele.label}
</Option>
))
) : (
<Option value="" disabled>
空
</Option>
)}
</Select>
</Form.Item>
);
default:
return (
<Form.Item {...formField} noStyle validateTrigger={['onChange', 'onBlur']}>
<Input style={{ width: '60%' }} />
</Form.Item>
);
}
}
FieldEditor = (
<Form.List name={name}>
{(fields, { add, remove }) => {
return (
<div>
{fields?.map((field, index) => {
return (
<Form.Item key={index}>
{renderItem(field)}
<MinusCircleOutlined
className="dynamic-delete-button"
style={{ margin: '0 8px' }}
onClick={() => {
remove(field.name)
}}
/>
</Form.Item>
)
})}
<Form.Item>
<Button
type="dashed"
onClick={() => {
add()
}}
style={{ width: '60%' }}
>
<PlusOutlined /> 添加字段
</Button>
</Form.Item>
</div>
)
}}
</Form.List>
)
break
case 'Markdown':
FieldEditor = <LazyMarkdownEditor id={key} />
break
case 'RichText':
FieldEditor = <LazyRichTextEditor id={key} />
break
case 'Connect':
FieldEditor = <IConnectEditor field={field} />
break
case 'Object':
FieldEditor = <IObjectEditor />
break
default:
FieldEditor = <Input />
}
return FieldEditor
}
/**
* 字段编辑表单
*/
export function getFieldFormItem(field: SchemaField, key: number) {
const rules = getRules(field)
const { name, type, description, displayName } = field
let FieldEditor: any = getFieldEditor(field, key)
let FormItem
switch (type) {
case 'Boolean':
FormItem = (
<Form.Item
key={key}
name={name}
rules={rules}
label={<Text strong>{displayName}</Text>}
extra={description}
valuePropName="checked"
>
{FieldEditor}
</Form.Item>
)
break
default:
FormItem = (
<Form.Item
key={key}
name={name}
rules={rules}
label={<Text strong>{displayName}</Text>}
extra={description}
>
{FieldEditor}
</Form.Item>
)
}
return FormItem
}