7
7
8
8
import React , { Component } from "react" ;
9
9
import PropTypes from "prop-types" ;
10
- import { DiscoverFieldsSection } from "./DiscoverFieldsSection" ;
10
+ import { default as DiscoverFieldsSection } from "./DiscoverFieldsSection" ;
11
11
import { AccordionField } from "../../AccordionField" ;
12
- import { loadWidgetsFromConfig } from "../loader" ;
12
+ import { importWidget , loadWidgetsFromConfig } from "../loader" ;
13
13
import { Container } from "semantic-ui-react" ;
14
14
15
15
export class CustomFields extends Component {
@@ -27,16 +27,18 @@ export class CustomFields extends Component {
27
27
try {
28
28
const { sectionsConfig, discoverFieldsConfig } =
29
29
await this . loadCustomFieldsWidgets ( ) ;
30
- const sections = sectionsConfig . map ( ( sectionCfg ) => {
31
- const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
32
- return { ...sectionCfg , paths } ;
33
- } ) ;
30
+ const sections = await Promise . all (
31
+ sectionsConfig . map ( async ( sectionCfg ) => {
32
+ const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
33
+ const widget = await this . loadSectionWidget ( sectionCfg ) ;
34
+ return { ...sectionCfg , paths, widget } ;
35
+ } )
36
+ ) . then ( ( values ) => values ) ;
34
37
35
38
const discoverFieldsSections = discoverFieldsConfig . map ( ( sectionCfg ) => {
36
39
const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
37
40
return { ...sectionCfg , paths } ;
38
41
} ) ;
39
-
40
42
this . setState ( {
41
43
sections : sections ,
42
44
discoverFieldsSections : discoverFieldsSections ,
@@ -46,6 +48,26 @@ export class CustomFields extends Component {
46
48
}
47
49
} ;
48
50
51
+ async loadSectionWidget ( sectionCfg ) {
52
+ const { templateLoaders, record, includesPaths, fieldPathPrefix } = this . props ;
53
+ const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
54
+ if ( sectionCfg . ui_widget ) {
55
+ return await importWidget (
56
+ templateLoaders ,
57
+ {
58
+ ...sectionCfg ,
59
+ fieldPath : undefined ,
60
+ record,
61
+ props : {
62
+ includesPaths : paths ,
63
+ children : sectionCfg ,
64
+ } ,
65
+ } ,
66
+ false
67
+ ) ;
68
+ }
69
+ }
70
+
49
71
async loadCustomFieldsWidgets ( ) {
50
72
const { config, fieldPathPrefix, templateLoaders, record } = this . props ;
51
73
@@ -75,6 +97,7 @@ export class CustomFields extends Component {
75
97
render ( ) {
76
98
const { sections, discoverFieldsSections } = this . state ;
77
99
const { templateLoaders, record } = this . props ;
100
+
78
101
return (
79
102
< >
80
103
{ sections &&
@@ -84,18 +107,21 @@ export class CustomFields extends Component {
84
107
paths,
85
108
displaySection = true ,
86
109
section : sectionName ,
110
+ widget,
87
111
id : sectionId ,
88
112
} = section ;
113
+ const active = section . active !== undefined ? section . active : true ;
114
+ const Element = widget !== undefined ? widget : AccordionField ;
89
115
return displaySection ? (
90
- < AccordionField
116
+ < Element
91
117
key = { `section-${ sectionName } ` }
92
118
includesPaths = { paths }
93
119
label = { sectionName }
94
- active
120
+ active = { active }
95
121
id = { sectionId }
96
122
>
97
123
{ fields }
98
- </ AccordionField >
124
+ </ Element >
99
125
) : (
100
126
< Container key = "custom-fields-section" > { fields } </ Container >
101
127
) ;
@@ -117,6 +143,7 @@ CustomFields.propTypes = {
117
143
PropTypes . shape ( {
118
144
section : PropTypes . string . isRequired ,
119
145
displaySection : PropTypes . bool ,
146
+ ui_widget : PropTypes . string ,
120
147
fields : PropTypes . arrayOf (
121
148
PropTypes . shape ( {
122
149
field : PropTypes . string . isRequired ,
0 commit comments