Skip to content

Commit

Permalink
Remove validation in attribute binder (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini authored Aug 30, 2024
1 parent 7fd8161 commit d5df784
Showing 1 changed file with 2 additions and 75 deletions.
77 changes: 2 additions & 75 deletions includes/manager/register-attribute-binder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addFilter } from '@wordpress/hooks';
import { useCallback, useMemo, useEffect, useState } from '@wordpress/element';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { createHigherOrderComponent } from '@wordpress/compose';
import { InspectorControls } from '@wordpress/block-editor';
Expand All @@ -14,29 +14,19 @@ import {
FlexBlock,
FlexItem,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
import { useEntityProp } from '@wordpress/core-data';

import ManageBindings from './_manage-bindings';

import SUPPORTED_BLOCK_ATTRIBUTES from './_supported-attributes';

const ErrorMessage = ( { children } ) => {
return (
<span style={ { color: 'var(--wp--preset--color--vivid-red)' } }>
{ children }
</span>
);
};

const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
const { getBlockType } = useSelect( blocksStore );
const { getBlockParentsByBlockName, getBlocksByClientId } =
useSelect( 'core/block-editor' );
const { lockPostSaving, unlockPostSaving } = useDispatch( editorStore );
const [ editingBoundAttribute, setEditingBoundAttribute ] =
useState( null );

Expand All @@ -57,12 +47,6 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
( field ) => field.slug === attributes.metadata?.slug
);

const getBinding = useCallback(
( attribute ) =>
attributes.metadata?.[ window.BINDINGS_KEY ]?.[ attribute ],
[ attributes.metadata ]
);

const removeBindings = useCallback( () => {
const newAttributes = {
metadata: {
Expand Down Expand Up @@ -137,63 +121,6 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
[ attributes.metadata, setAttributes, supportedAttributes ]
);

const validations = useMemo( () => {
const metadata = attributes.metadata ?? {};
const bindings = metadata[ window.BINDINGS_KEY ] ?? {};

const _validations = {};

const hasAtLeastOneBinding = Object.keys( bindings ).length > 0;

if (
hasAtLeastOneBinding &&
! metadata[ window.BLOCK_VARIATION_NAME_ATTR ]
) {
_validations[ window.BLOCK_VARIATION_NAME_ATTR ] = (
<ErrorMessage>
{ __( 'Block variation name is required' ) }
</ErrorMessage>
);
}

if (
metadata[ window.BLOCK_VARIATION_NAME_ATTR ] &&
! hasAtLeastOneBinding
) {
_validations[ window.BLOCK_VARIATION_NAME_ATTR ] = (
<ErrorMessage>
{ __( 'Bind at least one attribute' ) }
</ErrorMessage>
);
}

Object.keys( bindings ).forEach( ( attribute ) => {
const field = getBinding( attribute );

if ( field === 'post_content' && name !== 'core/group' ) {
_validations[ attribute ] = (
<ErrorMessage>
{ __(
'Only Group blocks can be bound to post_content'
) }
</ErrorMessage>
);
}
} );

return _validations;
}, [ attributes.metadata, getBinding, name ] );

useEffect( () => {
const hasValidationErrors = Object.keys( validations ).length > 0;

if ( hasValidationErrors ) {
lockPostSaving();
} else {
unlockPostSaving();
}
}, [ lockPostSaving, unlockPostSaving, validations ] );

if ( ! supportedAttributes || parentHasBindings ) {
return <BlockEdit { ...props } />;
}
Expand Down

0 comments on commit d5df784

Please sign in to comment.