-
Notifications
You must be signed in to change notification settings - Fork 699
/
schema.ts
708 lines (655 loc) · 16.3 KB
/
schema.ts
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface ValidateError {
message: string;
field: string;
}
/**
* 整个表单的数据,会注入到各个组件中去
*/
export type FormState = {
config: FormConfig;
popperClass?: string;
initValues: FormValue;
values: FormValue;
$emit: (event: string, ...args: any[]) => void;
keyProp?: string;
parentValues?: FormValue;
setField: (prop: string, field: any) => void;
getField: (prop: string) => any;
deleteField: (prop: string) => any;
[key: string]: any;
};
/**
* 排序配置
*/
export interface SortProp {
/** 跟该值排序 */
prop: string;
order: 'ascending' | 'descending';
}
export interface FormItem {
/** vnode的key值,默认是遍历数组时的index */
__key?: string | number;
/** 表单域标签的的宽度,例如 '50px'。支持 auto。 */
labelWidth?: string;
className?: string;
/** 表单组件类型 */
type?: string | TypeFunction;
/** 字段名 */
name?: string | number;
/** 额外的提示信息,和 help 类似,当提示文案同时出现时,可以使用这个。 */
extra?: string | FilterFunction;
/** 配置提示信息 */
tooltip?: string | FilterFunction;
/** 是否置灰 */
disabled?: boolean | FilterFunction;
/** 使用表单中的值作为key,例如配置了text,则使用model.text作为key */
key?: string;
/** 是否显示 */
display?: boolean | 'expand' | FilterFunction;
/** 值发生改变时调用的方法 */
onChange?: OnChangeHandler;
/** label 标签的文本 */
text?: string;
/** 右侧感叹号 */
tip?: string;
filter?: 'number' | OnChangeHandler;
/** 是否去除首尾空格 */
trim?: boolean;
/** 默认值 */
defaultValue?: any | DefaultValueFunction;
/** 表单验证规则 */
rules?: Rule[];
extensible?: boolean;
dynamicKey?: string;
/** 是否需要显示`展开更多配置` */
expand?: boolean;
}
export interface ContainerCommonConfig {
items: FormConfig;
onInitValue?: (
mForm: FormState | undefined,
data: {
formValue: FormValue;
initValue: FormValue;
},
) => FormValue;
extensible?: boolean;
}
export interface Rule {
message?: string;
/** 系统提供的验证器类型。有:string,number,boolean,method,regexp,integer,float,array,object,enum,date,url,hex,email,any */
type?: string;
/** 是否必填 */
required?: boolean;
/** 自定义验证器 */
validator?: (
options: {
rule: string;
value: any;
callback: Function;
source: Object;
options: {
messages: string;
};
},
data: {
/** 表单的初始值 */
values: FormValue;
/** 当前作用域下的值 */
model: FormValue;
parent: FormValue;
/** 整个表单的值 */
formValue: FormValue;
prop: string;
config: any;
},
mForm: FormState | undefined,
) => void;
}
export interface Input {
/** 输入框没有内容时显示的文案 */
placeholder?: string;
}
export type TypeFunction = (
mForm: FormState | undefined,
data: {
model: Record<any, any>;
},
) => string;
type FilterFunction = (
mForm: FormState | undefined,
data: {
model: Record<any, any>;
values: Record<any, any>;
parent?: Record<any, any>;
formValue: Record<any, any>;
prop: string;
config: any;
},
) => boolean;
type OnChangeHandler = (
mForm: FormState | undefined,
value: any,
data: {
model: Record<any, any>;
values: Record<any, any>;
parent?: Record<any, any>;
formValue: Record<any, any>;
config: any;
},
) => any;
type DefaultValueFunction = (mForm: FormState | undefined) => any;
/**
* 下拉选择器选项配置
*/
export interface SelectConfigOption {
/** 选项的标签 */
text: string | SelectOptionTextFunction;
/** 选项的值 */
value: any | SelectOptionValueFunction;
/** 是否禁用该选项 */
disabled?: boolean;
}
export interface SelectOption {
/** 选项的标签 */
text: string;
/** 选项的值 */
value: any;
/** 是否禁用该选项 */
disabled?: boolean;
}
/**
* 下拉选择器分组选项配置
*/
export interface SelectConfigGroupOption {
/** 分组的组名 */
label: string;
/** 是否禁用该选项组 */
disabled: boolean;
options: {
/** 选项的标签 */
label: string | SelectOptionTextFunction;
/** 选项的值 */
value: any | SelectOptionValueFunction;
/** 是否禁用该选项 */
disabled?: boolean;
}[];
}
export interface SelectGroupOption {
/** 分组的组名 */
label: string;
/** 是否禁用该选项组 */
disabled: boolean;
options: {
/** 选项的标签 */
label?: string;
text?: string;
/** 选项的值 */
value: any;
/** 是否禁用该选项 */
disabled?: boolean;
}[];
}
type SelectOptionFunction = (
mForm: FormState | undefined,
data: {
model: any;
prop?: string;
formValues: any;
formValue: any;
config: any;
},
) => SelectOption[] | SelectGroupOption[];
type RemoteSelectOptionBodyFunction = (
mForm: FormState | undefined,
data: {
model: any;
formValue: any;
formValues: any;
config: any;
},
) => Record<string, any>;
type RemoteSelectOptionRequestFunction = (
mForm: FormState | undefined,
res: any,
data: {
model: any;
formValue: any;
formValues: any;
config: any;
},
) => any;
type RemoteSelectOptionItemFunction = (optionsData: Record<string, any>) => SelectOption[] | SelectGroupOption[];
type SelectOptionValueFunction = (item: Record<string, any>) => any;
type SelectOptionTextFunction = (item: Record<string, any>) => string;
export interface CascaderOption {
/** 指定选项的值为选项对象的某个属性值 */
value: any;
/** 指定选项标签为选项对象的某个属性值 */
label: string;
/** 指定选项的子选项为选项对象的某个属性值 */
children?: CascaderOption[];
}
/**
* 日期范围
*/
export interface DaterangeConfig extends FormItem {
type: 'daterange';
defaultTime?: Date[];
names?: string[];
}
/**
* html编辑器
*/
export interface HtmlField extends FormItem {
type: 'html';
/** 是否异步加载编辑的内容 */
asyncLoad?: {
name: string | number;
};
}
/** 展示文本,不可编辑 */
export interface DisplayConfig extends FormItem {
type: 'display';
initValue?: string | number | boolean;
}
/** 文本输入框 */
export interface TextConfig extends FormItem, Input {
type?: 'text';
tooltip?: string;
/** 后置元素,一般为标签或按钮 */
append?:
| string
| {
text: string;
type: 'button';
handler: (
mForm: FormState | undefined,
data: {
model: any;
values: any;
},
) => void;
};
}
/**
* 文本域
*/
export interface TextareaConfig extends FormItem {
type: 'textarea';
placeholder?: string;
}
/**
* 计数器
*/
export interface NumberConfig extends FormItem {
type?: 'number';
tooltip?: string;
min?: number;
max?: number;
step?: number;
placeholder?: string;
}
/**
* 隐藏域
*/
export interface HiddenConfig extends FormItem {
type: 'hidden';
}
/**
* 日期选择器
*/
export interface DateConfig extends FormItem, Input {
type: 'date';
format?: 'YYYY-MM-dd HH:mm:ss' | string;
valueFormat?: 'YYYY-MM-dd HH:mm:ss' | string;
}
/**
* 日期时间选择器
*/
export interface DateTimeConfig extends FormItem, Input {
type: 'datetime';
defaultTime?: Date[];
format?: 'YYYY-MM-dd HH:mm:ss' | string;
valueFormat?: 'YYYY-MM-dd HH:mm:ss' | string;
}
/**
* 时间选择器
*/
export interface TimeConfig extends FormItem, Input {
type: 'time';
format?: 'HH:mm:ss' | string;
valueFormat?: 'HH:mm:ss' | string;
}
/**
* 单个多选框
*/
export interface CheckboxConfig extends FormItem {
type: 'checkbox';
activeValue?: boolean | number | string;
inactiveValue?: boolean | number | string;
}
/**
* 开关
*/
export interface SwitchConfig extends FormItem {
type: 'switch';
activeValue?: boolean | number | string;
inactiveValue?: boolean | number | string;
}
/**
* 单选框
*/
export interface RadioGroupConfig extends FormItem {
type: 'radio-group';
options: {
value: string | number | boolean;
text: string;
}[];
}
/**
* 颜色选择器
*/
export interface ColorPickConfig extends FormItem {
type: 'colorPicker';
}
/**
* 多选框组
*/
export interface CheckboxGroupConfig extends FormItem {
type: 'checkbox-group';
options:
| {
value: any;
text: string;
disabled?: boolean;
}[]
| Function;
}
/**
* 下拉选择器
*/
export interface SelectConfig extends FormItem, Input {
type: 'select';
clearable?: boolean;
multiple?: boolean;
valueKey?: string;
allowCreate?: boolean;
group?: boolean;
options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
remote: true;
option: {
url: string;
initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
method?: 'jsonp' | string;
cache?: boolean;
timeout?: number;
mode?: string;
headers?: {
[key: string]: string;
};
json?: false | boolean;
body?: Record<string, any> | RemoteSelectOptionBodyFunction;
jsonpCallback?: 'callback' | string;
afterRequest?: RemoteSelectOptionRequestFunction;
beforeRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
root: string;
item?: RemoteSelectOptionItemFunction;
value: string | SelectOptionValueFunction;
text: string | SelectOptionTextFunction;
};
}
/**
* 链接
*/
export interface LinkConfig extends FormItem {
type: 'link';
href?: string | ((model: Record<string, any>) => string);
css?: {
[key: string]: string | number;
};
disabledCss?: {
[key: string]: string | number;
};
formTitle?: string;
formWidth?: number | string;
displayText?:
| ((
mForm: FormState | undefined,
data: {
model: Record<any, any>;
},
) => string)
| string;
form:
| FormConfig
| ((
mForm: FormState | undefined,
data: {
model: Record<any, any>;
values: Record<any, any>;
},
) => FormConfig);
fullscreen?: boolean;
}
/**
* 级联选择器
*/
export interface CascaderConfig extends FormItem, Input {
type: 'cascader';
remote?: boolean;
multiple?: boolean;
options?:
| ((
mForm: FormState | undefined,
data: {
model: Record<any, any>;
formValues: Record<any, any>;
},
) => CascaderOption[])
| CascaderOption[];
option?: {
url: string;
cache?: boolean;
timeout?: number;
body?: Record<string, any> | RemoteSelectOptionBodyFunction;
root: 'string';
item: (optionsData: Record<string, any>) => CascaderOption[];
};
add?: {
action: {
method: 'post' | 'get';
body?: Record<string, any>;
};
};
}
export interface DynamicFieldConfig extends FormItem {
type: 'dynamic-field';
returnFields: (
config: DynamicFieldConfig,
model: Record<any, any>,
request: Object,
) => {
name: string;
label: string;
defaultValue: string;
}[];
dynamicKey: string;
}
/**
* 分组容器
*/
export interface RowConfig extends FormItem, ContainerCommonConfig {
type: 'row';
span: number;
}
/**
* 标签页容器
*/
export interface TabPaneConfig {
status?: string;
title: string;
lazy?: boolean;
labelWidth?: string;
items: FormConfig;
onTabClick?: (mForm: FormState | undefined, tab: any, data: any) => void;
[key: string]: any;
}
export interface TabConfig extends FormItem, ContainerCommonConfig {
type: 'tab' | 'dynamic-tab';
tabType?: string;
editable?: boolean;
dynamic?: boolean;
tabPosition?: 'top' | 'right' | 'bottom' | 'left';
items: TabPaneConfig[];
onChange?: (mForm: FormState | undefined, data: any) => void;
onTabAdd?: (mForm: FormState | undefined, data: any) => void;
onTabRemove?: (mForm: FormState | undefined, tabName: string, data: any) => void;
onTabClick?: (mForm: FormState | undefined, tab: any, data: any) => void;
activeChange?: (mForm: FormState | undefined, tabName: string, data: any) => void;
}
/**
* 分组
*/
export interface FieldsetConfig extends FormItem, ContainerCommonConfig {
type: 'fieldset';
checkbox?: boolean;
expand?: boolean;
legend?: string;
schematic?: string;
}
/**
* 面板容器
*/
export interface PanelConfig extends FormItem, ContainerCommonConfig {
type: 'panel';
expand?: boolean;
title?: string;
schematic?: string;
}
export interface ColumnConfig extends FormItem, ContainerCommonConfig {
name: string;
label: string;
width: string | number;
sortable: boolean;
[key: string]: any;
}
/**
* 表格容器
*/
export interface TableConfig extends FormItem {
type: 'table' | 'groupList' | 'group-list';
items: ColumnConfig[];
tableItems?: ColumnConfig[];
groupItems?: ColumnConfig[];
enableToggleMode?: boolean;
/** 最大行数 */
max?: number;
/** 最大高度 */
maxHeight?: number | string;
border?: boolean;
/** 显示行号 */
showIndex?: boolean;
pagination?: boolean;
enum?: any[];
/** 是否显示添加按钮 */
addable?: (mForm: FormState | undefined, data: any) => boolean | 'undefined' | boolean;
/** 是否显示删除按钮 */
delete?: (model: any, index: number, values: any) => boolean | boolean;
/** 是否显示导入按钮 */
importable?: (mForm: FormState | undefined, data: any) => boolean | 'undefined' | boolean;
/** 是否显示checkbox */
selection?: (mForm: FormState | undefined, data: any) => boolean | boolean | 'single';
/** 新增的默认行 */
defaultAdd?: (mForm: FormState | undefined, data: any) => any;
onSelect?: (mForm: FormState | undefined, data: any) => any;
defautSort?: SortProp;
defaultSort?: SortProp;
dropSort?: boolean;
/** 是否显示全屏按钮 */
enableFullscreen?: boolean;
fixed?: boolean;
itemExtra?: string | FilterFunction;
rowKey: string;
}
export interface GroupListConfig extends FormItem {
type: 'table' | 'groupList' | 'group-list';
span?: number;
enableToggleMode?: boolean;
items: FormConfig;
groupItems?: FormConfig;
tableItems?: FormConfig;
titleKey?: string;
itemExtra?: string | FilterFunction;
addable?: (mForm: FormState | undefined, data: any) => boolean | 'undefined' | boolean;
defaultAdd?: (mForm: FormState | undefined, data: any) => any;
delete?: (model: any, index: number | string | symbol, values: any) => boolean | boolean;
movable?: (
mForm: FormState | undefined,
index: number | string | symbol,
model: any,
groupModel: any,
) => boolean | boolean;
[key: string]: any;
}
interface StepItemConfig extends FormItem, ContainerCommonConfig {
title: string;
}
export interface StepConfig extends FormItem {
type: 'step';
/** 每个 step 的间距,不填写将自适应间距。支持百分比。 */
space?: string | number;
items: StepItemConfig[];
}
export interface ComponentConfig extends FormItem {
type: 'component';
id: string;
extend: any;
display: any;
}
export type ChildConfig =
| FormItem
| TabConfig
| RowConfig
| FieldsetConfig
| PanelConfig
| TableConfig
| GroupListConfig
| StepConfig
| DisplayConfig
| TextConfig
| HiddenConfig
| LinkConfig
| DaterangeConfig
| SelectConfig
| CascaderConfig
| HtmlField
| DateConfig
| ColorPickConfig
| TimeConfig
| DateTimeConfig
| CheckboxConfig
| SwitchConfig
| RadioGroupConfig
| TextareaConfig
| DynamicFieldConfig
| ComponentConfig;
export type FormConfig = (ChildConfig & { [key: string]: any })[];
export type FormValue = Record<string | number, any>;