X Tutup
Skip to content

Commit 1623e8f

Browse files
twinkle77JackLian
authored andcommitted
1 parent 518b4e0 commit 1623e8f

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

demo/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ preference.set('DataSourcePane', {
3131
type: "boolean",
3232
},
3333
timeout: {
34-
type: "integer",
34+
type: "number",
3535
},
3636
uri: {
3737
type: "string",

packages/plugin-datasource-pane/src/components/DataSourceForm/DataSourceForm.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { generateClassName } from '../../utils/misc';
3434
import { filterXDisplay } from '../../utils/filter-x-display';
3535

3636
import { DataSourceFormProps, DataSourceFormMode } from '../../types';
37+
import { isJSExpression } from '@alilc/lowcode-types';
3738

3839
const SCHEMA = {
3940
type: 'object',
@@ -79,8 +80,8 @@ const SCHEMA = {
7980
},
8081
params: {
8182
title: '请求参数',
82-
type: 'object',
83-
default: {},
83+
type: 'array',
84+
default: [],
8485
'x-decorator-props': {
8586
addonAfter: <ComponentSwitchBtn component="LowcodeExpression" />,
8687
},
@@ -117,9 +118,9 @@ const SCHEMA = {
117118
},
118119
},
119120
headers: {
120-
type: 'object',
121+
type: 'array',
121122
title: '请求头信息',
122-
default: {},
123+
default: [],
123124
'x-decorator-props': {
124125
addonAfter: <ComponentSwitchBtn component="LowcodeExpression" />,
125126
},
@@ -218,9 +219,8 @@ export class DataSourceForm extends PureComponent<DataSourceFormProps> {
218219
deriveInitialData = (dataSource: object = {}) => {
219220
const { dataSourceType } = this.props;
220221
const result: any = _cloneDeep(dataSource);
221-
222222
// TODO
223-
if (_isPlainObject(_get(result, 'options.params'))) {
223+
if (_isPlainObject(_get(result, 'options.params')) && !isJSExpression(_get(result, 'options.params'))) {
224224
result.options.params = Object.keys(result.options.params).reduce(
225225
(acc: any, cur: any) => {
226226
acc.push({
@@ -232,7 +232,7 @@ export class DataSourceForm extends PureComponent<DataSourceFormProps> {
232232
[],
233233
);
234234
}
235-
if (_isPlainObject(_get(result, 'options.headers'))) {
235+
if (_isPlainObject(_get(result, 'options.headers')) && !isJSExpression(_get(result, 'options.headers'))) {
236236
result.options.headers = Object.keys(result.options.headers).reduce(
237237
(acc: any, cur: any) => {
238238
acc.push({

packages/plugin-datasource-pane/src/components/Forms/component-switch-btn.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import _get from 'lodash/get';
55
import { connect, mapProps } from '@formily/react';
66
import cn from 'classnames';
77
import { generateClassName } from '../../utils/misc';
8+
import { Field } from '@formily/core'
9+
import { isJSExpression, JSExpression } from '@alilc/lowcode-types';
810

911
export interface ComponentSwitchBtnCompProps {
1012
className?: string;
1113
style?: React.CSSProperties;
1214
component: string;
1315
originalComponent: string;
1416
setComponent: (component: string) => void;
17+
field: Field
1518
}
1619

1720
export interface ComponentSwitchBtnCompState {
@@ -34,17 +37,62 @@ class ComponentSwitchBtnCompComp extends PureComponent<
3437

3538
componentDidMount() {
3639
this.originalComponent = this.props.originalComponent;
40+
41+
// 表单回调的时候,如果初始值是 expression,那需要切换组件
42+
if (isJSExpression(this.props.field.value)) {
43+
this.props.setComponent('LowcodeExpression')
44+
this.setState({ currentComponent: 'LowcodeExpression' });
45+
}
3746
}
3847

3948
handleSwitch = () => {
49+
const { field, setComponent } = this.props
50+
4051
let nextComponent = null;
4152
if (this.state.currentComponent === this.originalComponent) {
4253
nextComponent = this.props.component;
4354
} else {
4455
nextComponent = this.originalComponent;
4556
}
46-
this.setState({ currentComponent: nextComponent });
47-
this.props.setComponent?.(nextComponent);
57+
58+
let nextValue: number | boolean | string | JSExpression | Array<any> = field.value
59+
60+
switch(nextComponent) {
61+
case 'Switch':
62+
// expression 转 boolean
63+
if (isJSExpression(nextValue)) {
64+
nextValue = (nextValue && nextValue.value === 'true') || false;
65+
}
66+
break;
67+
case 'NumberPicker':
68+
// expression 转 number
69+
if (isJSExpression(nextValue)) {
70+
const val = +(nextValue && nextValue.value)
71+
nextValue = isNaN(val) ? 0 : val
72+
}
73+
break;
74+
case 'ArrayItems':
75+
// expression 转 array
76+
if (isJSExpression(nextValue)) {
77+
nextValue = []
78+
}
79+
break;
80+
case 'LowcodeExpression':
81+
// 普通组件转 array
82+
nextValue = {
83+
type: 'JSExpression',
84+
value: nextValue + '',
85+
}
86+
break;
87+
default: // 默认 expression 转 string (Input、Select 组件走这)
88+
if (isJSExpression(nextValue)) {
89+
nextValue = (nextValue && nextValue.value) || ''
90+
}
91+
}
92+
93+
this.setState({ currentComponent: nextComponent! });
94+
field.setValue(nextValue)
95+
setComponent?.(nextComponent!);
4896
};
4997

5098
render() {
@@ -66,6 +114,7 @@ export const ComponentSwitchBtn = connect(
66114
ComponentSwitchBtnCompComp,
67115
mapProps((props, field) => {
68116
return {
117+
field,
69118
setComponent: field.setComponent,
70119
originalComponent: _get(field, 'component[0]'),
71120
};

0 commit comments

Comments
 (0)
X Tutup