Skip to content

Commit

Permalink
Only show binding manager if there are bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Aug 29, 2024
1 parent ccb5117 commit e3ebbfc
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions includes/manager/register-attribute-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
'meta'
);

// Saving the fields as serialized JSON because I was tired of fighting the REST API.
const fields = meta?.fields ? JSON.parse( meta.fields ) : [];
const fields = useMemo( () => {
// Saving the fields as serialized JSON because I was tired of fighting the REST API.
return meta?.fields ? JSON.parse( meta.fields ) : [];
}, [ meta.fields ] );

const { attributes, setAttributes, name } = props;

Expand Down Expand Up @@ -81,7 +83,7 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
} );

setAttributes( newAttributes );
}, [ attributes.metadata, setAttributes ] );
}, [ attributes.metadata, setAttributes, fields, setMeta ] );

const selectedBlockType = getBlockType( name );

Expand Down Expand Up @@ -196,43 +198,40 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
return <BlockEdit { ...props } />;
}

const bindings = attributes?.metadata?.[ window.BINDINGS_KEY ];

return (
<>
<InspectorControls>
<PanelBody title="Attribute Bindings" initialOpen>
{ ! editingBoundAttribute &&
attributes?.metadata?.[
window.BLOCK_VARIATION_NAME_ATTR
] && (
<ItemGroup isBordered isSeparated>
{ supportedAttributes.map(
( attribute ) => (
<Item key={ attribute }>
<Flex>
<FlexBlock>
{ attribute }
</FlexBlock>
{ ! editingBoundAttribute && bindings && (
<ItemGroup isBordered isSeparated>
{ supportedAttributes.map( ( attribute ) => {
return (
<Item key={ attribute }>
<Flex>
<FlexBlock>
{ attribute }
</FlexBlock>
{ bindings[ attribute ] && (
<FlexItem>
<span>
<code>
{
attributes
?.metadata?.[
window
.BINDINGS_KEY
][
bindings[
attribute
]
}
</code>
</span>
</FlexItem>
</Flex>
</Item>
)
) }
</ItemGroup>
) }
) }
</Flex>
</Item>
);
} ) }
</ItemGroup>
) }
{ ! editingBoundAttribute && (
<PanelRow>
<ButtonGroup>
Expand All @@ -246,9 +245,7 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
>
{ __( 'Manage Binding' ) }
</Button>
{ attributes?.metadata?.[
window.BLOCK_VARIATION_NAME_ATTR
] && (
{ bindings && (
<Button
isDestructive
onClick={ removeBindings }
Expand Down

0 comments on commit e3ebbfc

Please sign in to comment.