@@ -92,7 +92,9 @@ export function hasRepeat(list) {
92
92
// ----------------- schema 相关
93
93
94
94
// 合并propsSchema和UISchema。由于两者的逻辑相关性,合并为一个大schema能简化内部处理
95
- export function combineSchema ( propsSchema , uiSchema ) {
95
+ export function combineSchema ( _propsSchema , _uiSchema ) {
96
+ const propsSchema = clone ( _propsSchema ) ;
97
+ const uiSchema = clone ( _uiSchema ) ;
96
98
const propList = getChildren ( propsSchema ) ;
97
99
const newList = propList . map ( p => {
98
100
const { name } = p ;
@@ -212,6 +214,7 @@ export function isFunction(func) {
212
214
}
213
215
214
216
// 判断schema中是否有属性值是函数表达式
217
+ // TODO: 这个util,没有考虑schema是array的情况,不过目前没有被用到
215
218
export function isFunctionSchema ( schema ) {
216
219
return Object . keys ( schema ) . some ( key => {
217
220
if ( typeof schema [ key ] === 'function' ) {
@@ -295,3 +298,26 @@ function isKey(value, object) {
295
298
( object != null && value in Object ( object ) )
296
299
) ;
297
300
}
301
+
302
+ // 将schema内所有的 {{ ... }} 转换为正常函数,我们试着一次性在外部做好这件事,而不是在内部每次去计算,优化性能
303
+
304
+ export function funcfySchema ( schema ) {
305
+ let _schema = clone ( schema ) ;
306
+ if ( isObj ( _schema ) ) {
307
+ Object . keys ( _schema ) . forEach ( key => {
308
+ _schema [ key ] = funcfySchema ( _schema [ key ] ) ;
309
+ } ) ;
310
+ } else if ( Array . isArray ( _schema ) ) {
311
+ _schema = _schema . map ( item => funcfySchema ( item ) ) ;
312
+ } else {
313
+ const funcBody = isFunction ( schema ) ;
314
+ if ( typeof funcBody === 'string' ) {
315
+ try {
316
+ _schema = new Function ( 'formData' , 'rootValue' , `return ${ funcBody } ` ) ;
317
+ } catch ( error ) {
318
+ console . error ( 'funcfySchema' , error ) ;
319
+ }
320
+ }
321
+ }
322
+ return _schema ;
323
+ }
0 commit comments