7
7
8
8
import React , { Component } from "react" ;
9
9
import PropTypes from "prop-types" ;
10
+ import { ComposeFields } from "./ComposeFields" ;
10
11
import { AccordionField } from "../../AccordionField" ;
11
12
import { loadWidgetsFromConfig } from "../loader" ;
12
13
13
14
export class CustomFields extends Component {
14
- state = { sections : [ ] } ;
15
+ constructor ( props ) {
16
+ super ( props ) ;
17
+ this . state = { sections : undefined , composeSections : undefined } ;
18
+ }
15
19
16
20
componentDidMount ( ) {
21
+ this . populateConfig ( ) ;
22
+ }
23
+
24
+ populateConfig = async ( ) => {
17
25
const { includesPaths, fieldPathPrefix } = this . props ;
18
- // use of `Promise.then()` as eslint is giving an error when calling setState() directly
19
- // in the componentDidMount() method
20
- this . loadCustomFieldsWidgets ( )
21
- . then ( ( sections ) => {
22
- sections = sections . map ( ( sectionCfg ) => {
23
- const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
24
- return { ...sectionCfg , paths } ;
25
- } ) ;
26
- this . setState ( { sections } ) ;
27
- } )
28
- . catch ( ( error ) => {
29
- console . error ( "Couldn't load custom fields widgets." , error ) ;
26
+ try {
27
+ const { sectionsConfig, composeSectionConfig } =
28
+ await this . loadCustomFieldsWidgets ( ) ;
29
+ const sections = sectionsConfig . map ( ( sectionCfg ) => {
30
+ const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
31
+ return { ...sectionCfg , paths } ;
30
32
} ) ;
31
- }
33
+
34
+ const composeSections = composeSectionConfig . map ( ( sectionCfg ) => {
35
+ const paths = includesPaths ( sectionCfg . fields , fieldPathPrefix ) ;
36
+ return { ...sectionCfg , paths } ;
37
+ } ) ;
38
+
39
+ this . setState ( { sections : sections , composeSections : composeSections } ) ;
40
+ } catch ( error ) {
41
+ console . error ( "Couldn't load custom fields widgets." , error ) ;
42
+ }
43
+ } ;
32
44
33
45
async loadCustomFieldsWidgets ( ) {
34
- const { config, fieldPathPrefix, templateLoaders } = this . props ;
46
+ const { config, fieldPathPrefix, templateLoaders, record } = this . props ;
35
47
36
48
const sections = [ ] ;
49
+ const composeFieldSections = [ ] ;
37
50
for ( const sectionCfg of config ) {
38
51
// Path to end user's folder defining custom fields ui widgets
39
52
const fields = await loadWidgetsFromConfig ( {
40
53
templateLoaders : templateLoaders ,
41
54
fieldPathPrefix : fieldPathPrefix ,
42
55
fields : sectionCfg . fields ,
56
+ record : record ,
43
57
} ) ;
44
- sections . push ( { ...sectionCfg , fields } ) ;
58
+ if ( sectionCfg . compose_fields ) {
59
+ composeFieldSections . push ( {
60
+ ...sectionCfg ,
61
+ fields : fields ,
62
+ fieldsConfig : sectionCfg . fields ,
63
+ } ) ;
64
+ } else {
65
+ sections . push ( { ...sectionCfg , fields } ) ;
66
+ }
45
67
}
46
- return sections ;
68
+ return { sectionsConfig : sections , composeSectionConfig : composeFieldSections } ;
47
69
}
48
70
49
71
render ( ) {
50
- const { sections } = this . state ;
72
+ const { sections, composeSections } = this . state ;
73
+ const { templateLoaders, record } = this . props ;
51
74
return (
52
75
< >
53
- { sections . map ( ( { section, fields, paths } ) => (
54
- < AccordionField key = { section } includesPaths = { paths } label = { section } active >
55
- { fields }
56
- </ AccordionField >
57
- ) ) }
76
+ { sections &&
77
+ sections . map ( ( { fields, paths, ...sectionConfig } ) => (
78
+ < AccordionField
79
+ key = { sectionConfig . section }
80
+ includesPaths = { paths }
81
+ label = { sectionConfig . section }
82
+ active
83
+ >
84
+ { fields }
85
+ </ AccordionField >
86
+ ) ) }
87
+ { composeSections && composeSections && (
88
+ < ComposeFields
89
+ templateLoaders = { templateLoaders }
90
+ composeSections = { composeSections }
91
+ record = { record }
92
+ />
93
+ ) }
58
94
</ >
59
95
) ;
60
96
}
@@ -76,6 +112,7 @@ CustomFields.propTypes = {
76
112
templateLoaders : PropTypes . array . isRequired ,
77
113
fieldPathPrefix : PropTypes . string . isRequired ,
78
114
includesPaths : PropTypes . func ,
115
+ record : PropTypes . object . isRequired ,
79
116
} ;
80
117
81
118
CustomFields . defaultProps = {
0 commit comments