1
+ import _isEmpty from "lodash/isEmpty" ;
1
2
import React , { Component } from "react" ;
2
3
import PropTypes from "prop-types" ;
3
4
import { Divider } from "semantic-ui-react" ;
@@ -8,9 +9,11 @@ import { Extensions } from "./Extensions";
8
9
export class ComposeFields extends Component {
9
10
constructor ( props ) {
10
11
super ( props ) ;
11
- const { composeSections } = this . props ;
12
-
13
- this . state = { sections : composeSections , tempFields : [ ] } ;
12
+ const { composeSections, record } = props ;
13
+ const filled = Object . keys ( record . custom_fields ) . map (
14
+ ( key ) => `custom_fields.${ key } `
15
+ ) ;
16
+ this . state = { sections : composeSections , tempFields : [ ] , recordFields : filled } ;
14
17
this . fieldsCfg = this . getFieldsConfig ( composeSections ) ;
15
18
this . sectionsList = composeSections . map ( ( section ) => section . section ) ;
16
19
}
@@ -29,15 +32,13 @@ export class ComposeFields extends Component {
29
32
30
33
getFieldsWithValues = ( sectionFields ) => {
31
34
const { record } = this . props ;
32
- const { tempFields } = this . state ;
35
+ const { tempFields, recordFields } = this . state ;
33
36
const filledFields = [ ] ;
37
+ if ( ! record . custom_fields ) {
38
+ return [ ] ;
39
+ }
34
40
for ( const field of sectionFields ) {
35
- if (
36
- Object . keys ( record . custom_fields ) . includes (
37
- field . key . replace ( "custom_fields." , "" )
38
- ) ||
39
- tempFields . includes ( field )
40
- ) {
41
+ if ( recordFields . includes ( field . key ) || tempFields . includes ( field ) ) {
41
42
filledFields . push ( field ) ;
42
43
}
43
44
}
@@ -53,44 +54,61 @@ export class ComposeFields extends Component {
53
54
}
54
55
} ;
55
56
56
- addFieldCallback = ( field ) => {
57
+ addFieldCallback = ( fields ) => {
57
58
const { sections : prevSections , tempFields : prevTempFields } = this . state ;
59
+
58
60
const sections = [ ...prevSections ] ;
59
- const sectionToUpdate = this . getSectionOfField ( field ) ;
60
- for ( const section of sections ) {
61
- if ( section . section === sectionToUpdate ) {
62
- section [ "fields" ] = [ ...section . fields , field ] . sort ( ( a , b ) =>
63
- a . key . localeCompare ( b . key )
64
- ) ;
61
+ for ( const field of fields ) {
62
+ const sectionToUpdate = this . getSectionOfField ( field ) ;
63
+ for ( const section of sections ) {
64
+ if ( section . section === sectionToUpdate ) {
65
+ section [ "fields" ] = [ ...section . fields , field ] . sort ( ( a , b ) =>
66
+ a . key . localeCompare ( b . key )
67
+ ) ;
68
+ }
65
69
}
66
70
}
67
- this . setState ( { sections : [ ...sections ] , tempFields : [ ...prevTempFields , field ] } ) ;
71
+ this . setState ( {
72
+ sections : [ ...sections ] ,
73
+ tempFields : [ ...prevTempFields , ...fields ] ,
74
+ } ) ;
68
75
} ;
69
76
70
77
render ( ) {
71
78
const { templateLoaders, record } = this . props ;
72
- const { sections } = this . state ;
79
+ const { sections, tempFields, recordFields } = this . state ;
80
+ const existingFields = [
81
+ ...Object . entries ( tempFields ) . map ( ( [ key , value ] ) => value . key ) ,
82
+ ...recordFields ,
83
+ ] ;
73
84
74
85
return (
75
86
< AccordionField key = "compose fields" label = "Domain specific fields" active >
76
- { sections . map ( ( { fields, paths, ...sectionConfig } ) => (
77
- < div key = { sectionConfig . section } className = "rel-mb-2" >
78
- < FieldLabel
79
- htmlFor = { sectionConfig . section }
80
- icon = { sectionConfig . icon }
81
- label = { sectionConfig . section }
82
- />
83
- < Divider fitted className = "rel-mb-1" />
84
- < div className = "rel-ml-1" > { this . getFieldsWithValues ( fields ) } </ div >
85
- </ div >
86
- ) ) }
87
+ { sections . map ( ( { fields, paths, ...sectionConfig } ) => {
88
+ const recordCustomFields = this . getFieldsWithValues ( fields ) ;
89
+ if ( _isEmpty ( recordCustomFields ) ) {
90
+ return undefined ;
91
+ }
92
+ return (
93
+ < div key = { sectionConfig . section } className = "rel-mb-2" >
94
+ < FieldLabel
95
+ htmlFor = { sectionConfig . section }
96
+ icon = { sectionConfig . icon }
97
+ label = { sectionConfig . section }
98
+ />
99
+ < Divider fitted className = "rel-mb-1" />
100
+ < div className = "rel-ml-1" > { recordCustomFields } </ div >
101
+ </ div >
102
+ ) ;
103
+ } ) }
87
104
< Extensions
88
105
fieldPath = "custom_fields"
89
106
{ ...this . fieldsCfg }
90
107
templateLoaders = { templateLoaders }
91
108
addFieldCallback = { this . addFieldCallback }
92
109
sections = { this . sectionsList }
93
110
record = { record }
111
+ existingFields = { existingFields }
94
112
/>
95
113
</ AccordionField >
96
114
) ;
0 commit comments