forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetter-config.ts
More file actions
83 lines (74 loc) · 2.46 KB
/
setter-config.ts
File metadata and controls
83 lines (74 loc) · 2.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
import { ComponentClass, Component, ComponentType, ReactElement, isValidElement } from 'react';
import { TitleContent } from './title';
import { SettingTarget } from './setting-target';
import { CompositeValue } from './value-type';
function isReactClass(obj: any): obj is ComponentClass<any> {
return obj && obj.prototype && (obj.prototype.isReactComponent || obj.prototype instanceof Component);
}
function isReactComponent(obj: any): obj is ComponentType<any> {
return obj && (isReactClass(obj) || typeof obj === 'function');
}
export type CustomView = ReactElement | ComponentType<any>;
export type DynamicProps = (target: SettingTarget) => Record<string, unknown>;
export type DynamicSetter = (target: SettingTarget) => string | SetterConfig | CustomView;
/**
* 设置器配置
*/
export interface SetterConfig {
// if *string* passed must be a registered Setter Name
/**
* 配置设置器用哪一个 setter
*/
componentName: string | CustomView;
/**
* 传递给 setter 的属性
*
* the props pass to Setter Component
*/
props?: Record<string, unknown> | DynamicProps;
/**
* @deprecated
*/
children?: any;
/**
* 是否必填?
*
* ArraySetter 里有个快捷预览,可以在不打开面板的情况下直接编辑
*/
isRequired?: boolean;
/**
* Setter 的初始值
*
* @todo initialValue 可能要和 defaultValue 二选一
*/
initialValue?: any | ((target: SettingTarget) => any);
// for MixedSetter
/**
* 给 MixedSetter 时切换 Setter 展示用的
*/
title?: TitleContent;
// for MixedSetter check this is available
/**
* 给 MixedSetter 用于判断优先选中哪个
*/
condition?: (target: SettingTarget) => boolean;
/**
* 给 MixedSetter,切换值时声明类型
*
* @todo 物料协议推进
*/
valueType?: CompositeValue[];
// 标识是否为动态setter,默认为true
isDynamic?: boolean;
}
// if *string* passed must be a registered Setter Name, future support blockSchema
export type SetterType = SetterConfig | SetterConfig[] | string | CustomView;
export function isSetterConfig(obj: any): obj is SetterConfig {
return obj && typeof obj === 'object' && 'componentName' in obj && !isCustomView(obj);
}
export function isCustomView(obj: any): obj is CustomView {
return obj && (isValidElement(obj) || isReactComponent(obj));
}
export function isDynamicSetter(obj: any): obj is DynamicSetter {
return obj && typeof obj === 'function' && !isReactClass(obj);
}