Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/plugins/block-bindings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
} ) );
},
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/plugins/server-side-rendered-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
},
},
},
},
Expand All @@ -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( '🌈' );
} );
} );
} );
Loading