Skip to content

Commit d5df784

Browse files
authored
Remove validation in attribute binder (#82)
1 parent 7fd8161 commit d5df784

File tree

1 file changed

+2
-75
lines changed

1 file changed

+2
-75
lines changed

includes/manager/register-attribute-binder.js

+2-75
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addFilter } from '@wordpress/hooks';
2-
import { useCallback, useMemo, useEffect, useState } from '@wordpress/element';
2+
import { useCallback, useMemo, useState } from '@wordpress/element';
33
import { __ } from '@wordpress/i18n';
44
import { createHigherOrderComponent } from '@wordpress/compose';
55
import { InspectorControls } from '@wordpress/block-editor';
@@ -14,29 +14,19 @@ import {
1414
FlexBlock,
1515
FlexItem,
1616
} from '@wordpress/components';
17-
import { useSelect, useDispatch } from '@wordpress/data';
18-
import { store as editorStore } from '@wordpress/editor';
17+
import { useSelect } from '@wordpress/data';
1918
import { store as blocksStore } from '@wordpress/blocks';
2019
import { useEntityProp } from '@wordpress/core-data';
2120

2221
import ManageBindings from './_manage-bindings';
2322

2423
import SUPPORTED_BLOCK_ATTRIBUTES from './_supported-attributes';
2524

26-
const ErrorMessage = ( { children } ) => {
27-
return (
28-
<span style={ { color: 'var(--wp--preset--color--vivid-red)' } }>
29-
{ children }
30-
</span>
31-
);
32-
};
33-
3425
const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
3526
return ( props ) => {
3627
const { getBlockType } = useSelect( blocksStore );
3728
const { getBlockParentsByBlockName, getBlocksByClientId } =
3829
useSelect( 'core/block-editor' );
39-
const { lockPostSaving, unlockPostSaving } = useDispatch( editorStore );
4030
const [ editingBoundAttribute, setEditingBoundAttribute ] =
4131
useState( null );
4232

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

60-
const getBinding = useCallback(
61-
( attribute ) =>
62-
attributes.metadata?.[ window.BINDINGS_KEY ]?.[ attribute ],
63-
[ attributes.metadata ]
64-
);
65-
6650
const removeBindings = useCallback( () => {
6751
const newAttributes = {
6852
metadata: {
@@ -137,63 +121,6 @@ const withAttributeBinder = createHigherOrderComponent( ( BlockEdit ) => {
137121
[ attributes.metadata, setAttributes, supportedAttributes ]
138122
);
139123

140-
const validations = useMemo( () => {
141-
const metadata = attributes.metadata ?? {};
142-
const bindings = metadata[ window.BINDINGS_KEY ] ?? {};
143-
144-
const _validations = {};
145-
146-
const hasAtLeastOneBinding = Object.keys( bindings ).length > 0;
147-
148-
if (
149-
hasAtLeastOneBinding &&
150-
! metadata[ window.BLOCK_VARIATION_NAME_ATTR ]
151-
) {
152-
_validations[ window.BLOCK_VARIATION_NAME_ATTR ] = (
153-
<ErrorMessage>
154-
{ __( 'Block variation name is required' ) }
155-
</ErrorMessage>
156-
);
157-
}
158-
159-
if (
160-
metadata[ window.BLOCK_VARIATION_NAME_ATTR ] &&
161-
! hasAtLeastOneBinding
162-
) {
163-
_validations[ window.BLOCK_VARIATION_NAME_ATTR ] = (
164-
<ErrorMessage>
165-
{ __( 'Bind at least one attribute' ) }
166-
</ErrorMessage>
167-
);
168-
}
169-
170-
Object.keys( bindings ).forEach( ( attribute ) => {
171-
const field = getBinding( attribute );
172-
173-
if ( field === 'post_content' && name !== 'core/group' ) {
174-
_validations[ attribute ] = (
175-
<ErrorMessage>
176-
{ __(
177-
'Only Group blocks can be bound to post_content'
178-
) }
179-
</ErrorMessage>
180-
);
181-
}
182-
} );
183-
184-
return _validations;
185-
}, [ attributes.metadata, getBinding, name ] );
186-
187-
useEffect( () => {
188-
const hasValidationErrors = Object.keys( validations ).length > 0;
189-
190-
if ( hasValidationErrors ) {
191-
lockPostSaving();
192-
} else {
193-
unlockPostSaving();
194-
}
195-
}, [ lockPostSaving, unlockPostSaving, validations ] );
196-
197124
if ( ! supportedAttributes || parentHasBindings ) {
198125
return <BlockEdit { ...props } />;
199126
}

0 commit comments

Comments
 (0)