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 pathField.tsx
More file actions
339 lines (329 loc) · 11.2 KB
/
Field.tsx
File metadata and controls
339 lines (329 loc) · 11.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import React from 'react'
import { Input, InputNumber, Form, Space, Select, Switch, Typography, Row, Col, Button } from 'antd'
import { ISwitch, IDatePicker } from '@/components/Fields'
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
const { Option } = Select
const { Text } = Typography
/**
* JSON Object 输入框
*/
const ObjectInput: React.FC<{
value?: any
onChange?: any
}> = ({ value, onChange }) => {
let formatValue = value
if (typeof value === 'object') {
formatValue = JSON.stringify(value)
}
return <Input.TextArea value={formatValue} placeholder="请输入 JSON 字符串" onChange={onChange} />
}
/**
* 获取字段默认值的输入 JSX
*/
export const getFieldDefaultValueInput = (
type: SchemaFieldType,
options: {
enumElementType: 'string' | 'number'
dateFormatType: 'timestamp-ms' | 'timestamp-s' | 'date' | 'string'
}
) => {
const { dateFormatType, enumElementType } = options
switch (type) {
case 'Number':
return <InputNumber style={{ width: '60%' }} placeholder="此值的默认值" />
case 'Boolean':
return <ISwitch />
case 'Date':
case 'DateTime':
return <IDatePicker style={{ width: '60%' }} type={type} dateFormatType={dateFormatType} />
case 'Object':
return <ObjectInput />
case 'Enum':
if (enumElementType === 'number') {
return <InputNumber style={{ width: '60%' }} placeholder="此值的默认值" />
} else {
return <Input placeholder="此值的默认值" />
}
default:
return <Input placeholder="此值的默认值" />
}
}
/**
* 获取类型对应的编辑信息
*/
export function getFieldFormItem(
type: string,
options: {
formValue: any
schemas: Schema[]
selectedField: SchemaField
fieldAction: 'edit' | 'create'
connectSchema?: Schema
}
) {
const { schemas, connectSchema, selectedField, fieldAction, formValue } = options
switch (type) {
case 'String':
case 'MultiLineString':
case 'Number':
if (selectedField.name === '_id') return
return (
<Form.Item style={{ marginBottom: 0 }}>
<Row gutter={[24, 0]}>
<Col flex="1 1 auto">
<Form.Item label={selectedField.type === 'Number' ? '最小值' : '最小长度'} name="min">
<InputNumber
style={{ width: '100%' }}
placeholder={selectedField.type === 'Number' ? '最小值,如 1' : '最小长度,如 1'}
/>
</Form.Item>
</Col>
<Col flex="1 1 auto">
<Form.Item label={selectedField.type === 'Number' ? '最大值' : '最大长度'} name="max">
<InputNumber
style={{ width: '100%' }}
placeholder={
selectedField.type === 'Number' ? '最大值,如 1000' : '最大长度,如 1000'
}
/>
</Form.Item>
</Col>
</Row>
</Form.Item>
)
case 'Date':
case 'DateTime':
return (
<Form.Item label="时间存储格式" name="dateFormatType" validateTrigger={['onChange']}>
<Select placeholder="时间存储格式">
<Option value="timestamp-ms">Unix Timestamp 毫秒</Option>
<Option value="timestamp-s">Unix Timestamp 秒</Option>
<Option value="date">Date 对象</Option>
<Option value="string">时间字符串</Option>
</Select>
</Form.Item>
)
case 'Enum':
return (
<>
<Form.Item label="枚举元素类型" name="enumElementType" validateTrigger={['onChange']}>
<Select placeholder="元素值类型">
<Option value="string">字符串</Option>
<Option value="number">数字</Option>
</Select>
</Form.Item>
<Form.Item label="枚举元素">
<Form.List name="enumElements">
{(fields, { add, remove }) => {
return (
<div>
{fields?.map((field, index) => {
return (
<EnumListItem
key={index}
field={field}
onRemove={remove}
formValue={formValue}
/>
)
})}
<Form.Item>
<Button
type="dashed"
onClick={() => {
add()
}}
style={{ width: '60%' }}
>
<PlusOutlined /> 添加枚举元素
</Button>
</Form.Item>
</div>
)
}}
</Form.List>
</Form.Item>
</>
)
case 'File':
case 'Image':
return (
<>
<Form.Item label="资源链接格式" name="resourceLinkType" validateTrigger={['onChange']}>
<Select placeholder="资源链接格式">
<Option value="fileId">FileId</Option>
<Option value="https">HTTPS</Option>
</Select>
</Form.Item>
<Form.Item>
<div className="form-item">
<Form.Item style={{ marginBottom: 0 }}>
<Text>允许多个内容</Text>
<Form.Item name="isMultiple" valuePropName="checked" style={{ marginBottom: 0 }}>
<Switch />
</Form.Item>
<Text type="secondary">在创建内容时,允许创建多个内容,数据将以数组格式存储</Text>
</Form.Item>
</div>
</Form.Item>
</>
)
case 'Media':
return (
<>
<Form.Item label="资源链接格式" name="resourceLinkType" validateTrigger={['onChange']}>
<Select placeholder="资源链接格式">
<Option value="fileId">FileId</Option>
<Option value="https">HTTPS</Option>
</Select>
</Form.Item>
<Form.Item>
<div className="form-item">
<Form.Item style={{ marginBottom: 0 }}>
<Text>允许多个内容</Text>
<Form.Item name="isMultiple" valuePropName="checked" style={{ marginBottom: 0 }}>
<Switch />
</Form.Item>
<Text type="secondary">在创建内容时,允许创建多个内容,数据将以数组格式存储</Text>
</Form.Item>
</div>
</Form.Item>
<Form.Item label="媒体类型" name="mediaType" validateTrigger={['onChange']}>
<Select placeholder="媒体类型">
<Option value="video">视频</Option>
<Option value="music">音频</Option>
</Select>
</Form.Item>
</>
)
case 'Connect':
return (
<>
<Form.Item label="关联">
<Space>
<Form.Item
label="关联内容"
name="connectResource"
rules={[{ required: true, message: '请选择关联内容!' }]}
>
<Select style={{ width: 200 }}>
{schemas?.map((schema: Schema) => (
<Option value={schema._id} key={schema._id}>
{schema.displayName}
</Option>
))}
</Select>
</Form.Item>
<Form.Item
label="展示字段"
name="connectField"
rules={[{ required: true, message: '请选择关联需要展示的字段!' }]}
>
<Select style={{ width: 200 }} placeholder="关联字段">
{connectSchema?.fields?.length ? (
connectSchema.fields?.map((field: SchemaField) => (
<Option value={field.name} key={field.name}>
{field.displayName}
</Option>
))
) : (
<Option value="" key={selectedField.name} disabled>
空
</Option>
)}
</Select>
</Form.Item>
</Space>
</Form.Item>
<Form.Item>
<div className="form-item">
<Form.Item name="connectMany" valuePropName="checked" style={{ marginBottom: 0 }}>
<Switch disabled={fieldAction === 'edit'} />
</Form.Item>
<Form.Item style={{ marginBottom: 0 }}>
<span>是否关联多项(支持选择多个关联文档)</span>
{fieldAction === 'edit' && (
<>
<br />
<Text type="warning">关联多项与关联单项无法转换</Text>
</>
)}
</Form.Item>
</div>
</Form.Item>
</>
)
case 'Array':
const renderElement = (fieldType: string,) => {
switch (fieldType) {
case 'Enum':
return getFieldFormItem(fieldType, { ...options });
default:
return null;
}
}
return (
<>
<Form.Item label="数组元素类型" name="elementType" validateTrigger={['onChange']}>
<Select placeholder="元素值类型">
<Option value="Enum">枚举</Option>
<Option value="any">任意类型</Option>
</Select>
</Form.Item>
<Form.Item label="数组元素配置" dependencies={['elementType']}>
{(ele) => {
const filed = ele.getFieldValue('elementType');
return renderElement(filed);
}}
</Form.Item>
</>
)
default:
return ''
}
}
/**
* 枚举值列表 Item
*/
const EnumListItem: React.FC<{ field: any; formValue: any; onRemove: (name: number) => void }> = (
props
) => {
const { field, formValue, onRemove } = props
const enumValueType = formValue?.enumElementType || 'string'
return (
<Form.Item>
<Form.Item noStyle name={[field.name, 'label']} validateTrigger={['onChange', 'onBlur']}>
<Input placeholder="枚举元素展示别名,如 “已发布”" style={{ width: '45%' }} />
</Form.Item>
{enumValueType === 'number' && (
<Form.Item noStyle name={[field.name, 'value']} validateTrigger={['onChange', 'onBlur']}>
<InputNumber
placeholder="枚举元素值,如 100"
style={{
width: '45%',
marginLeft: '2%',
}}
/>
</Form.Item>
)}
{enumValueType === 'string' && (
<Form.Item noStyle name={[field.name, 'value']} validateTrigger={['onChange', 'onBlur']}>
<Input
placeholder="枚举元素值,如 published"
style={{
width: '45%',
marginLeft: '2%',
}}
/>
</Form.Item>
)}
<MinusCircleOutlined
className="dynamic-delete-button"
style={{ margin: '0 0 0 15px' }}
onClick={() => {
onRemove(field.name)
}}
/>
</Form.Item>
)
}