forked from alibaba/lowcode-engine-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
159 lines (147 loc) · 4.95 KB
/
index.tsx
File metadata and controls
159 lines (147 loc) · 4.95 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
import * as React from 'react';
import { useState, ErrorInfo, useMemo } from 'react';
import { Radio, Select, Box } from '@alifd/next';
import { SettingTarget, CustomView } from '@felce/lowcode-types';
import './index.scss';
import { BehaviorAction } from './types';
import { linkBehaviorAction } from './actions/link-action';
import { dialogBehaviorAction } from './actions/dialog-action';
import { messageBehaviorAction } from './actions/message-action';
import { tooltipBehaviorAction } from './actions/tooltip-action';
interface BehaviorSetterProps {
field?: SettingTarget;
// 兼容 vision engine
prop?: SettingTarget;
onChange: Function;
actions: string[];
type?: 'dialog' | 'link' | undefined;
value: Record<string, any>;
url?: 'string';
responseFormatter?: Function;
extraBehaviorActions?: BehaviorAction[];
extendedOptions?: Record<string, any>;
enableMessageAction?: boolean;
enableTooltipAction?: boolean;
}
const defaultActionMap = {
dialog: dialogBehaviorAction,
link: linkBehaviorAction,
tooltip: tooltipBehaviorAction,
message: messageBehaviorAction,
};
const BehaviorSetter: CustomView = ({
value: behaviors = {},
onChange,
field: propsField,
prop,
actions,
type: propsType,
extraBehaviorActions: propsBehaviorActions = [],
extendedOptions = {},
url,
responseFormatter,
enableMessageAction,
enableTooltipAction,
}: BehaviorSetterProps) => {
const field = propsField || prop;
const [currentEvent, setCurrentEvent] = useState(actions[0]);
/** 当前的行为集合 */
const currentBehavior = behaviors[currentEvent] || {};
const currentBehaviorType = currentBehavior.type || propsType || 'dialog';
const defaultActions: any = [];
defaultActions.push(defaultActionMap.dialog, defaultActionMap.link);
if (enableMessageAction) {
defaultActions.push(defaultActionMap.message);
}
if (enableTooltipAction) {
defaultActions.push(defaultActionMap.tooltip);
}
const behaviorActions = useMemo(
() => [...defaultActions, ...propsBehaviorActions],
[propsBehaviorActions],
);
const updateCurrentEventBehavior = (type: string, newVal: any) => {
onChange({
...behaviors,
[currentEvent]: {
type,
[type]: newVal,
},
});
};
const behaviorOption = behaviorActions.find((vo) => vo.name === currentBehaviorType);
return (
<div>
<Box direction="row" align="center" className="behavior-item">
<Box style={{ width: 70 }}>事件列表</Box>
<Box className="behavior-radio">
<Select
size="small"
dataSource={actions.map((action) => ({
label: action,
value: action,
}))}
value={currentEvent}
onChange={(eventName) => setCurrentEvent(eventName)}
/>
</Box>
</Box>
<Box direction="row" align="center" className="behavior-item">
<Box style={{ width: 70 }}>操作类型</Box>
<Box className="behavior-radio">
<Radio.Group
size="small"
dataSource={behaviorActions.map((vo) => ({
value: vo.name,
label: vo.title,
disabled: propsType && vo.name !== propsType,
}))}
value={currentBehaviorType}
shape="button"
onChange={(behaviorType: string) => updateCurrentEventBehavior(behaviorType, undefined)}
/>
</Box>
</Box>
<ErrorBoundary>
{behaviorOption &&
behaviorOption.render({
field,
value: currentBehavior[currentBehaviorType],
onChange: (behaviorValue: Record<string, any>) => {
field.parent.setPropValue(
currentEvent,
behaviorOption.toActionValue(behaviorValue, extendedOptions[currentBehaviorType]),
);
updateCurrentEventBehavior(currentBehaviorType, behaviorValue);
},
options:
currentBehaviorType === 'link'
? {
...extendedOptions[currentBehaviorType],
url,
responseFormatter,
}
: extendedOptions[currentBehaviorType],
})}
</ErrorBoundary>
</div>
);
};
export default BehaviorSetter;
// eslint-disable-next-line @iceworks/best-practices/recommend-functional-component
class ErrorBoundary extends React.Component {
// 更新 state 使下一次渲染能够显示降级后的 UI
static getDerivedStateFromError = (error: any) => ({ hasError: true, error });
state = { hasError: false, error: undefined as Error };
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// 你同样可以将错误日志上报给服务器
console.error(error, errorInfo);
}
render() {
if (this.state.hasError) {
// 你可以自定义降级后的 UI 并渲染
return <div>渲染异常: {this.state.error?.message || this.state.error || ''}</div>;
}
return this.props.children;
}
}