Skip to content

Commit 570dd57

Browse files
committed
feat(lib): ui配置支持 ui:xxx 配置表达式
re #19
1 parent 7c20bb8 commit 570dd57

File tree

10 files changed

+65
-33
lines changed

10 files changed

+65
-33
lines changed

packages/demo/src/schema-generator/views/editor/common/editorData.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ export function componentList2JsonSchema(componentList) {
189189
const baseObj = genBaseObj();
190190

191191
let parentObj = baseObj;
192-
let stack = [{ $$parentFlag: parentObj }, ...componentList];
192+
let queue = [{ $$parentFlag: parentObj }, ...componentList];
193193

194194
const hasChild = data => Array.isArray(data.childList) && data.childList.length > 0;
195195

196-
// 广度,同时标记父节点
197-
while (stack.length) {
198-
// 出栈
199-
const item = stack.shift();
196+
// 队列广度,同时标记父节点
197+
while (queue.length) {
198+
// 出队
199+
const item = queue.shift();
200200

201201
// 标记节点 切换parent
202202
if (item.$$parentFlag) {
@@ -208,9 +208,9 @@ export function componentList2JsonSchema(componentList) {
208208
...uiSchema
209209
};
210210

211-
// 入栈
211+
// 入队
212212
if (hasChild(item)) {
213-
stack = [...stack, { $$parentFlag: curSchema }, ...item.childList];
213+
queue = [...queue, { $$parentFlag: curSchema }, ...item.childList];
214214
}
215215

216216
// 连接数据

packages/lib/src/JsonSchemaForm/common/formUtils.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,22 @@ export function getUserUiOptions({
113113
}) {
114114
// 支持 uiSchema配置在 schema文件中
115115
return Object.assign({}, ...[schema, uiSchema].map(itemSchema => Object.keys(itemSchema)
116-
.filter(key => key.indexOf('ui:') === 0)
117116
.reduce((options, key) => {
118117
const value = itemSchema[key];
119118
// options 内外合并
120119
if (key === 'ui:options' && isObject(value)) {
121120
return { ...options, ...value };
122121
}
123122

124-
// 只对 ui:xxx 配置形式支持表达式
125-
return {
126-
...options,
127-
[key.substring(3)]: curNodePath === undefined ? value : handleExpression(rootFormData, curNodePath, value, () => value)
128-
};
123+
if (key.indexOf('ui:') === 0) {
124+
// 只对 ui:xxx 配置形式支持表达式
125+
return {
126+
...options,
127+
[key.substring(3)]: curNodePath === undefined ? value : handleExpression(rootFormData, curNodePath, value, () => value)
128+
};
129+
}
130+
131+
return options;
129132
}, {})));
130133
}
131134

@@ -192,11 +195,15 @@ export function getUiOptions({
192195
// 处理成 Widget 组件需要的格式
193196
export function getWidgetConfig({
194197
schema = {},
195-
uiSchema = {}
198+
uiSchema = {},
199+
curNodePath,
200+
rootFormData,
196201
}, fallback = null) {
197202
const uiOptions = getUiOptions({
198203
schema,
199-
uiSchema
204+
uiSchema,
205+
curNodePath,
206+
rootFormData,
200207
});
201208

202209
// 没有配置 Widget ,各个Field组件根据类型判断
@@ -247,14 +254,18 @@ export function getUserErrOptions({
247254
errorSchema = {}
248255
}) {
249256
return Object.assign({}, ...[schema, uiSchema, errorSchema].map(itemSchema => Object.keys(itemSchema)
250-
.filter(key => key.indexOf('err:') === 0)
251257
.reduce((options, key) => {
252258
const value = itemSchema[key];
253259
// options 内外合并
254260
if (key === 'err:options' && isObject(value)) {
255261
return { ...options, ...value };
256262
}
257-
return { ...options, [key.substring(4)]: value };
263+
264+
if (key.indexOf('err:') === 0) {
265+
return { ...options, [key.substring(4)]: value };
266+
}
267+
268+
return options;
258269
}, {})));
259270
}
260271

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldDateRange.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export default {
1212
props: vueProps,
1313
functional: true,
1414
render(h, context) {
15-
const { schema, uiSchema } = context.props;
15+
const {
16+
schema, uiSchema, curNodePath, rootFormData
17+
} = context.props;
1618
const widgetConfig = getWidgetConfig({
1719
schema,
1820
uiSchema: {
1921
'ui:widget': WIDGET_MAP.formats[schema.format],
20-
...uiSchema
21-
}
22+
...uiSchema,
23+
},
24+
curNodePath,
25+
rootFormData
2226
});
2327

2428
return h(

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldMultiSelect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export default {
3030

3131
const widgetConfig = getWidgetConfig({
3232
schema,
33-
uiSchema
33+
uiSchema,
34+
curNodePath,
35+
rootFormData
3436
}, () => ({
3537
widget: WIDGET_MAP.common.checkboxGroup
3638
}));

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldNormal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
},
2424
render(h, context) {
2525
const {
26-
schema, uiSchema, curNodePath, itemsFormData, errorSchema
26+
schema, uiSchema, curNodePath, rootFormData, itemsFormData, errorSchema
2727
} = context.props;
2828

2929
const {
@@ -40,7 +40,9 @@ export default {
4040
fieldStyle,
4141
} = getUiOptions({
4242
schema,
43-
uiSchema
43+
uiSchema,
44+
curNodePath,
45+
rootFormData,
4446
});
4547

4648
const arrayItemsVNodeList = itemsFormData.map((item, index) => {

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldTuple.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default {
5757
if (!Array.isArray(this.itemsFormData)) return false;
5858

5959
const {
60-
schema, uiSchema, errorSchema
60+
schema, uiSchema, errorSchema, curNodePath
6161
} = this.$props;
6262

6363
const {
@@ -75,7 +75,7 @@ export default {
7575
} = getUiOptions({
7676
schema,
7777
uiSchema,
78-
curNodePath: this.curNodePath,
78+
curNodePath,
7979
rootFormData: this.rootFormData,
8080
});
8181

@@ -92,7 +92,7 @@ export default {
9292
schema: schema.items[index],
9393
uiSchema: uiSchema.items ? uiSchema.items[index] : {},
9494
errorSchema: errorSchema.items ? errorSchema.items[index] : {},
95-
curNodePath: computedCurPath(this.curNodePath, index)
95+
curNodePath: computedCurPath(curNodePath, index)
9696
}
9797
}
9898
));

packages/lib/src/JsonSchemaForm/fields/BooleanField/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export default {
2525

2626
const widgetConfig = getWidgetConfig({
2727
schema,
28-
uiSchema
28+
uiSchema,
29+
curNodePath,
30+
rootFormData
2931
}, () => ({
3032
widget: WIDGET_MAP.types.boolean
3133
}));

packages/lib/src/JsonSchemaForm/fields/ObjectField/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export default {
6060
title, description, showTitle, showDescription, order, fieldClass, fieldAttrs, fieldStyle, onlyShowIfDependent
6161
} = getUiOptions({
6262
schema,
63-
uiSchema
63+
uiSchema,
64+
curNodePath,
65+
rootFormData
6466
});
6567

6668
const properties = Object.keys(schema.properties || {});

packages/lib/src/JsonSchemaForm/fields/StringField/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export default {
2121

2222
const widgetConfig = getWidgetConfig({
2323
schema,
24-
uiSchema
24+
uiSchema,
25+
curNodePath,
26+
rootFormData
2527
}, () => {
2628
const isNumber = schema.type === 'number' || schema.type === 'integer';
2729

packages/lib/src/JsonSchemaForm/fields/combiningSchemas/SelectLinkageField/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export default {
5454
// 下拉选项参数
5555
const selectWidgetConfig = getWidgetConfig({
5656
schema: this.schema[`${this.combiningType}Select`] || {}, // 扩展 oneOfSelect,anyOfSelect字段
57-
uiSchema: this.uiSchema[`${this.combiningType}Select`] || {} // 通过 uiSchema['oneOf'] 配置ui信息
57+
uiSchema: this.uiSchema[`${this.combiningType}Select`] || {}, // 通过 uiSchema['oneOf'] 配置ui信息
58+
curNodePath: this.curNodePath,
59+
rootFormData: this.rootFormData,
5860
}, () => ({
5961
// 枚举参数
6062
widget: 'SelectWidget'
@@ -72,7 +74,9 @@ export default {
7274
const curUiOptions = getUiOptions({
7375
schema: option,
7476
uiSchema: uiSchemaSelectList[index],
75-
containsSpec: false
77+
containsSpec: false,
78+
// curNodePath: this.curNodePath,
79+
// rootFormData: this.rootFormData,
7680
});
7781
return {
7882
label: curUiOptions.title || `选项 ${index + 1}`,
@@ -141,7 +145,8 @@ export default {
141145
}
142146
},
143147
render(h) {
144-
const pathClassName = nodePath2ClassName(this.$props.curNodePath);
148+
const { curNodePath } = this.$props;
149+
const pathClassName = nodePath2ClassName(curNodePath);
145150

146151
// object 需要保持原有属性,如果存在原有属性这里单独渲染
147152
let originVnode = null;
@@ -188,7 +193,9 @@ export default {
188193
const userUiOptions = filterObject(getUiOptions({
189194
schema: this.schema,
190195
uiSchema: this.uiSchema,
191-
containsSpec: false
196+
containsSpec: false,
197+
curNodePath,
198+
rootFormData: this.rootFormData,
192199
}), key => (key === this.combiningType ? undefined : `ui:${key}`));
193200

194201
const userErrOptions = filterObject(getUserErrOptions({

0 commit comments

Comments
 (0)