Skip to content

Commit

Permalink
Merge branch 'fix/pattern-overrides-checkbox' into add/block-bindings…
Browse files Browse the repository at this point in the history
…-php-registration-mechanisms
  • Loading branch information
artemiomorales committed Jan 5, 2024
2 parents 176da0a + f6b59db commit 7fb84db
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions packages/patterns/src/components/partial-syncing-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,34 @@ import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';

function PartialSyncingControls( { name, attributes, setAttributes } ) {
const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
const attributeSources = Object.keys( syncedAttributes ).map(
( attributeName ) =>
attributes.metadata?.bindings?.[ attributeName ]?.source?.name
);
const isConnectedToOtherSources = attributeSources.every(
( source ) => source && source !== 'pattern_attributes'
);

// Render nothing if all supported attributes are connected to other sources.
if ( isConnectedToOtherSources ) {
return null;
}

function updateBindings( isChecked ) {
let updatedBindings = {
...attributes?.metadata?.bindings,
};

function updateBindings( attributeName, isChecked ) {
if ( ! isChecked ) {
let updatedBindings = {
...attributes?.metadata?.bindings,
[ attributeName ]: undefined,
};
if ( Object.keys( updatedBindings ).length === 1 ) {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if (
updatedBindings[ attributeName ]?.source?.name ===
'pattern_attributes'
) {
delete updatedBindings[ attributeName ];
}
}
if ( ! Object.keys( updatedBindings ).length ) {
updatedBindings = undefined;
}
setAttributes( {
Expand All @@ -36,10 +56,15 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
return;
}

const updatedBindings = {
...attributes?.metadata?.bindings,
[ attributeName ]: { source: { name: 'pattern_attributes' } },
};
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if ( ! updatedBindings[ attributeName ] ) {
updatedBindings[ attributeName ] = {
source: {
name: 'pattern_attributes',
},
};
}
}

if ( typeof attributes.metadata?.id === 'string' ) {
setAttributes( {
Expand All @@ -65,25 +90,18 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
<InspectorControls group="advanced">
<BaseControl __nextHasNoMarginBottom>
<BaseControl.VisualLabel>
{ __( 'Synced attributes' ) }
{ __( 'Pattern overrides' ) }
</BaseControl.VisualLabel>
{ Object.entries( syncedAttributes ).map(
( [ attributeName, label ] ) => (
<CheckboxControl
key={ attributeName }
__nextHasNoMarginBottom
label={ label }
checked={
attributes?.metadata?.bindings?.[
attributeName
]?.source?.name === 'pattern_attributes'
}
onChange={ ( isChecked ) => {
updateBindings( attributeName, isChecked );
} }
/>
)
) }
<CheckboxControl
__nextHasNoMarginBottom
label={ __( 'Allow instance overrides' ) }
checked={ attributeSources.some(
( source ) => source === 'pattern_attributes'
) }
onChange={ ( isChecked ) => {
updateBindings( isChecked );
} }
/>
</BaseControl>
</InspectorControls>
);
Expand Down

0 comments on commit 7fb84db

Please sign in to comment.