33 */
44
55import vueProps from '../props' ;
6-
76import { orderProperties , getUiOptions } from '../../common/formUtils' ;
8- import { computedCurPath } from '../../common/vueUtils' ;
9-
7+ import { computedCurPath , getPathVal } from '../../common/vueUtils' ;
8+ import { isObject } from '../../common/utils' ;
109import FieldGroupWrap from '../../fieldComponents/FieldGroupWrap' ;
1110import Widget from '../../fieldComponents/Widget' ;
1211
@@ -15,26 +14,50 @@ import SchemaField from '../SchemaField';
1514
1615export default {
1716 name : 'ObjectField' ,
18- props : {
19- ...vueProps
20- } ,
21- methods : {
22- isRequired ( name ) {
23- const schema = this . schema ;
24- return Array . isArray ( schema . required ) && ! ! ~ schema . required . indexOf ( name ) ;
25- }
26- } ,
27- render ( h ) {
28- const self = this ;
29- const props = this . $props ;
17+ functional : true ,
18+ props : vueProps ,
19+ render ( h , context ) {
3020 const {
3121 schema,
3222 uiSchema,
3323 errorSchema,
34- } = props ;
24+ needValidFieldGroup,
25+ curNodePath,
26+ rootFormData
27+ } = context . props ;
28+
29+ // required
30+ const isRequired = name => Array . isArray ( schema . required ) && ! ! ~ schema . required . indexOf ( name ) ;
31+
32+ // 存在 dependencies 配置,需要当前属性是否存在依赖关系 和当前属性是否正在被依赖
33+ // tip: 判断依赖关系需要使用了 formData 的值来做判断,所以当用户输入的时候会触发整个对象树重新渲染
34+ // TODO: 每个属性都需要单独来遍历 dependencies 属性可以优化一点点点点点(可通过 key value 反转值加个缓存来计算)
35+ const isDependOn = ( name ) => {
36+ let isDependency = false ; // 是否是一个被依赖项
37+ let curDependent = false ; // 当前是否触发依赖
38+
39+ if ( isObject ( schema . dependencies ) ) {
40+ curDependent = Object . entries ( schema . dependencies ) . some ( ( [ key , value ] ) => {
41+
42+ // 是否和当前属性存在依赖关系
43+ const tempDependency = ! ! ( Array . isArray ( value ) && ~ value . indexOf ( name ) ) ;
44+
45+ // 是否是一个被依赖项
46+ isDependency = isDependency || tempDependency ;
47+
48+ // 当前需要依赖
49+ return tempDependency && getPathVal ( rootFormData , curNodePath ) [ key ] !== undefined ;
50+ } ) ;
51+ }
52+
53+ return {
54+ isDependency,
55+ curDependent
56+ } ;
57+ } ;
3558
3659 const {
37- title, description, showTitle, showDescription, order, fieldClass, fieldAttrs, fieldStyle
60+ title, description, showTitle, showDescription, order, fieldClass, fieldAttrs, fieldStyle, onlyShowIfDependent
3861 } = getUiOptions ( {
3962 schema,
4063 uiSchema
@@ -44,19 +67,26 @@ export default {
4467 const orderedProperties = orderProperties ( properties , order ) ;
4568
4669 // 递归参数
47- const propertiesVNodeList = orderedProperties . map ( name => h (
48- SchemaField ,
49- {
50- props : {
51- ...props ,
52- schema : schema . properties [ name ] ,
53- uiSchema : uiSchema [ name ] ,
54- errorSchema : errorSchema [ name ] ,
55- required : self . isRequired ( name ) ,
56- curNodePath : computedCurPath ( props . curNodePath , name )
70+ const propertiesVNodeList = orderedProperties . map ( ( name ) => {
71+ const required = isRequired ( name ) ;
72+ const { isDependency, curDependent } = isDependOn ( name ) ;
73+
74+ return h (
75+ // onlyShowWhenDependent 只渲染被依赖的属性
76+ ( isDependency && onlyShowIfDependent && ! curDependent ) ? null : SchemaField ,
77+ {
78+ key : name ,
79+ props : {
80+ ...context . props ,
81+ schema : schema . properties [ name ] ,
82+ uiSchema : uiSchema [ name ] ,
83+ errorSchema : errorSchema [ name ] ,
84+ required : required || curDependent ,
85+ curNodePath : computedCurPath ( curNodePath , name )
86+ }
5787 }
58- }
59- ) ) ;
88+ ) ;
89+ } ) ;
6090
6191 return h (
6292 FieldGroupWrap ,
@@ -67,7 +97,7 @@ export default {
6797 showTitle,
6898 showDescription
6999 } ,
70- class : fieldClass ,
100+ class : { ... context . data . class , ... fieldClass } ,
71101 attrs : fieldAttrs ,
72102 style : fieldStyle
73103 } ,
@@ -81,23 +111,23 @@ export default {
81111 ...propertiesVNodeList ,
82112
83113 // 插入一个Widget,校验 object组 - minProperties. maxProperties. oneOf 等需要外层校验的数据
84- this . needValidFieldGroup ? h ( Widget , {
114+ needValidFieldGroup ? h ( Widget , {
85115 class : {
86116 validateWidget : true ,
87117 'validateWidget-object' : true
88118 } ,
89119 props : {
90- schema : Object . entries ( self . $props . schema ) . reduce ( ( preVal , [ key , value ] ) => {
120+ schema : Object . entries ( schema ) . reduce ( ( preVal , [ key , value ] ) => {
91121 if (
92- self . $props . schema . additionalProperties === false
122+ schema . additionalProperties === false
93123 || ! [ 'properties' , 'id' , '$id' ] . includes ( key )
94124 ) preVal [ key ] = value ;
95125 return preVal ;
96126 } , { } ) ,
97127 uiSchema,
98128 errorSchema,
99- curNodePath : props . curNodePath ,
100- rootFormData : props . rootFormData
129+ curNodePath,
130+ rootFormData
101131 }
102132 } ) : null
103133 ]
0 commit comments