@@ -18,6 +18,7 @@ import apiFetch from '@wordpress/api-fetch';
1818import { useDispatch , useSelect } from '@wordpress/data' ;
1919import { createBlock } from '@wordpress/blocks' ;
2020import Icon from './icon' ;
21+ import { VariationPicker } from './variation-picker' ;
2122
2223const SelectListPlaceholder = ( ) => {
2324 return (
@@ -59,6 +60,7 @@ export const BlockEdit = (props) => {
5960 unsubscribe_link_text,
6061 show_required_indicator = true ,
6162 required_indicator_text,
63+ template = 'default' ,
6264 } = attributes ;
6365
6466 const [ listData , setListData ] = useState ( { } ) ;
@@ -74,7 +76,10 @@ export const BlockEdit = (props) => {
7476 ) ;
7577 const exisingTags = innerBlocks . map ( ( block ) => block ?. attributes ?. tag ) . filter ( Boolean ) ;
7678 const exisingGroups = innerBlocks . map ( ( block ) => block ?. attributes ?. id ) . filter ( Boolean ) ;
77- const visibleFieldsCount = innerBlocks . filter ( ( block ) => block ?. attributes ?. visible ) . length ;
79+ const visibleFields = innerBlocks
80+ . filter ( ( block ) => block ?. attributes ?. visible )
81+ . map ( ( block ) => block ?. attributes ?. tag ) ;
82+ const visibleFieldsCount = visibleFields . length ;
7883
7984 const listOptions = [ ] ;
8085 // Check if selected list is not in the list of available lists.
@@ -116,20 +121,27 @@ export const BlockEdit = (props) => {
116121 tag : field . tag ,
117122 label : field . name ,
118123 type : field . type ,
124+ /**
125+ * Visibility logic:
126+ * 1. If there are visible fields from the previous list, make the field visible if it's visible in the previous list form (Try to keep the same visibility as the previous list form) for the default template also make the field visible if it's required.
127+ * 2. If there are no visible fields from the previous list, make the field visible if it's required or it's public and the visibility setting is on in the global settings.
128+ */
119129 visible :
120- ( field . required ||
121- merge_fields_visibility ?. [ field . tag ] === 'on' ) &&
122- field . public ,
130+ ( template === 'default' && field . required ) ||
131+ ( visibleFields . length > 0 &&
132+ visibleFields . includes ( field . tag ) ) ||
133+ ( visibleFields . length === 0 &&
134+ ( field . required ||
135+ ( merge_fields_visibility ?. [ field . tag ] === 'on' &&
136+ field . public ) ) ) ,
123137 } ) ,
124138 ) || [ ] ;
125139 const listGroupsBlocks =
126140 data ?. interest_groups ?. map ( ( group ) =>
127141 createBlock ( 'mailchimp/mailchimp-audience-group' , {
128142 id : group . id ,
129143 label : group . title ,
130- visible :
131- interest_groups_visibility ?. [ group . id ] === 'on' &&
132- group . type !== 'hidden' ,
144+ visible : false , // Keep the groups hidden by default.
133145 } ) ,
134146 ) || [ ] ;
135147 replaceInnerBlocks ( clientId , [ ...listFieldsBlocks , ...listGroupsBlocks ] , false ) ;
@@ -161,19 +173,14 @@ export const BlockEdit = (props) => {
161173 tag : field . tag ,
162174 label : field . name ,
163175 type : field . type ,
164- visible :
165- ( field . required ||
166- merge_fields_visibility ?. [ field . tag ] === 'on' ) &&
167- field . public ,
176+ visible : template === 'default' && field . required , // Keep newly added fields hidden by default, except for required fields.
168177 } ) ,
169178 ) ;
170179 const newGroupBlocks = newFormGroups . map ( ( group ) =>
171180 createBlock ( 'mailchimp/mailchimp-audience-group' , {
172181 id : group . id ,
173182 label : group . title ,
174- visible :
175- interest_groups_visibility ?. [ group . id ] === 'on' &&
176- group . type !== 'hidden' ,
183+ visible : false , // Keep newly added groups hidden by default.
177184 } ) ,
178185 ) ;
179186
@@ -279,6 +286,11 @@ export const BlockEdit = (props) => {
279286 ) ;
280287 }
281288
289+ // Display the variation picker if there are no innerBlocks.
290+ if ( innerBlocks . length === 0 ) {
291+ return < VariationPicker { ...props } /> ;
292+ }
293+
282294 // Create a template for innerBlocks based on list data and visibility settings.
283295 const templateFields =
284296 listData ?. merge_fields ?. map ( ( field ) => [
@@ -301,7 +313,7 @@ export const BlockEdit = (props) => {
301313 visible : interest_groups_visibility ?. [ group . id ] === 'on' && group . type !== 'hidden' ,
302314 } ,
303315 ] ) || [ ] ;
304- const template = [ ...templateFields , ...templateGroups ] ;
316+ const templateBlocks = [ ...templateFields , ...templateGroups ] ;
305317
306318 return (
307319 < >
@@ -343,7 +355,7 @@ export const BlockEdit = (props) => {
343355 < InnerBlocks
344356 allowedBlocks = { [ 'mailchimp/mailchimp-form-field' ] }
345357 orientation = "vertical"
346- template = { template }
358+ template = { templateBlocks }
347359 templateLock = "insert"
348360 />
349361 { show_required_indicator && (
0 commit comments