diff --git a/packages/block-editor/src/components/block-bindings/attribute-control.js b/packages/block-editor/src/components/block-bindings/attribute-control.js index b92ae3abbfe696..7b8d400e9175b8 100644 --- a/packages/block-editor/src/components/block-bindings/attribute-control.js +++ b/packages/block-editor/src/components/block-bindings/attribute-control.js @@ -50,10 +50,10 @@ export default function BlockBindingsAttributeControl( { getBlockType, } = unlock( select( blocksStore ) ); - const _attributeType = - getBlockType( blockName ).attributes?.[ attribute ]?.type; + const _attribute = + getBlockType( blockName ).attributes?.[ attribute ]; const attributeType = - _attributeType === 'rich-text' ? 'string' : _attributeType; + _attribute?.type === 'rich-text' ? 'string' : _attribute?.type; const sourceFields = {}; Object.entries( getAllBlockBindingsSources() ).forEach( @@ -66,7 +66,17 @@ export default function BlockBindingsAttributeControl( { return; } const compatibleFieldsList = fieldsList.filter( - ( field ) => field.type === attributeType + ( field ) => { + if ( field.type !== attributeType ) { + return false; + } + if ( _attribute?.enum ) { + return field?.enum?.every( ( item ) => + _attribute.enum.includes( item ) + ); + } + return true; + } ); if ( compatibleFieldsList.length ) { sourceFields[ sourceName ] = compatibleFieldsList; diff --git a/packages/e2e-tests/plugins/block-bindings.php b/packages/e2e-tests/plugins/block-bindings.php index 1d51c8c6bff15c..14c7c12b57e060 100644 --- a/packages/e2e-tests/plugins/block-bindings.php +++ b/packages/e2e-tests/plugins/block-bindings.php @@ -18,26 +18,32 @@ function gutenberg_test_block_bindings_registration() { $upload_dir = wp_upload_dir(); $testing_url = $upload_dir['url'] . '/1024x768_e2e_test_image_size.jpeg'; $fields_list = array( - 'text_field' => array( + 'text_field' => array( 'label' => 'Text Field Label', 'value' => 'Text Field Value', 'type' => 'string', ), - 'url_field' => array( + 'url_field' => array( 'label' => 'URL Field Label', 'value' => $testing_url, 'type' => 'string', ), - 'empty_field' => array( + 'empty_field' => array( 'label' => 'Empty Field Label', 'value' => '', 'type' => 'string', ), - 'number_custom_field' => array( + 'number_custom_field' => array( 'label' => 'Number Custom Field Label', 'value' => 10.5, 'type' => 'number', ), + 'text_field_with_enum' => array( + 'label' => 'Text Field with Enum Label', + 'value' => '🌈', + 'type' => 'string', + 'enum' => array( '⭐', '❤️', '🌈' ), + ), ); // Enqueue a custom script for the plugin. diff --git a/packages/e2e-tests/plugins/block-bindings/index.js b/packages/e2e-tests/plugins/block-bindings/index.js index d87198c04fe8da..2edcf8d6bb05f9 100644 --- a/packages/e2e-tests/plugins/block-bindings/index.js +++ b/packages/e2e-tests/plugins/block-bindings/index.js @@ -33,6 +33,7 @@ registerBlockBindingsSource( { return Object.entries( fieldsList || {} ).map( ( [ key, field ] ) => ( { label: field.label || key, type: field.type || 'string', + enum: field.enum, args: field.args || { key }, } ) ); }, diff --git a/packages/e2e-tests/plugins/server-side-rendered-block.php b/packages/e2e-tests/plugins/server-side-rendered-block.php index 29962e90798318..28dc70af756080 100644 --- a/packages/e2e-tests/plugins/server-side-rendered-block.php +++ b/packages/e2e-tests/plugins/server-side-rendered-block.php @@ -91,7 +91,7 @@ static function () { add_filter( 'block_bindings_supported_attributes_test/auto-register-with-controls', static function () { - return array( 'title', 'count', 'spacing', 'showEmojis' ); + return array( 'title', 'count', 'spacing', 'showEmojis', 'emoji' ); } ); diff --git a/test/e2e/specs/editor/plugins/server-side-rendered-block.spec.js b/test/e2e/specs/editor/plugins/server-side-rendered-block.spec.js index cf5bbb7bdbd6fa..e1996b6f8660fb 100644 --- a/test/e2e/specs/editor/plugins/server-side-rendered-block.spec.js +++ b/test/e2e/specs/editor/plugins/server-side-rendered-block.spec.js @@ -303,6 +303,10 @@ test.describe( 'PHP-only auto-register blocks', () => { source: 'core/post-meta', args: { key: 'boolean' }, }, + emoji: { + source: 'testing/complete-source', + args: { key: 'text_field_with_enum' }, + }, }, }, }, @@ -318,6 +322,9 @@ test.describe( 'PHP-only auto-register blocks', () => { await expect( page.getByLabel( 'Count' ) ).toHaveValue( '3' ); await expect( page.getByLabel( 'Spacing' ) ).toHaveValue( '0.5' ); await expect( page.getByLabel( 'Show Emojis' ) ).toBeChecked(); + await expect( + page.getByLabel( 'Emoji', { exact: true } ) + ).toHaveValue( '🌈' ); } ); } ); } );