Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: onChange for InnerBlocks? #11271

Closed
raquelmsmith opened this issue Oct 30, 2018 · 8 comments
Closed

Question: onChange for InnerBlocks? #11271

raquelmsmith opened this issue Oct 30, 2018 · 8 comments
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P [Type] Enhancement A suggestion for improvement. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@raquelmsmith
Copy link
Contributor

I have a nested (parent) block with a number of items inside. When the content changes inside those innerblocks, I want to run a function. I don't see an onChange prop for InnerBlocks, and when I tried it didn't work. Any ideas how to go about this?

@earnjam earnjam added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Oct 30, 2018
@manake
Copy link

manake commented Oct 31, 2018

I just mentioned this in #6751 too.

And also saving InnerBlocks as a value of an array could be useful. I can do this with RichText but not with InnerBlocks.

I don't know if either of these are technically feasible though.

@raquelmsmith
Copy link
Contributor Author

raquelmsmith commented Nov 1, 2018

@earnjam I think this should be changed to feature request for Nested / Inner Blocks as it doesn't seem to currently be possible?

@earnjam earnjam added [Type] Enhancement A suggestion for improvement. [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P and removed [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Nov 2, 2018
@noel-schenk
Copy link

noel-schenk commented Nov 19, 2018

@scottblackburn
Copy link

@raquelmsmith did you ever figure out a solution for this? I would like to use InnerBlocks.Content from the parent block to generate some schema.

@youknowriad
Copy link
Contributor

If you want to retrieve the list of blocks and use it elsewhere, see my reply here #10745 (comment)

If you want to check whether any of the inner blocks change over time to do something, I think your best bet here is to use the data module and the getBlocks selector using the clientId prop passed to the edit function of your block. if you use withSelect you'd receive the updated list of inner blocks on each render.

withSelect(
	( select, { clientId } ) => {
		return {
			innerBlocks: select( 'core/block-editor' ).getBlocks( clientId )
		};
	}
)( MyBlockEditFunction )

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Apr 22, 2019
@scottblackburn
Copy link

@youknowriad thanks for this, just noticed a minor issue that the select should be 'editor' rather than 'block-editor' but I can confirm this works.

@raquelmsmith this should get you close to what you need, if you use this edit for you parent block you should see a log of the innerBlocks.

edit: withSelect( ( select, blockData ) => {
     return {
       innerBlocks: select( 'core/editor' ).getBlocks( blockData.clientId )
     };
   } )( ( { innerBlocks, className } ) => {
     console.log('inner blocks content', innerBlocks);
     return <div className={className}>
          <InnerBlocks template={TEMPLATE} />
        </div>
 } ),

@asretux
Copy link

asretux commented Apr 26, 2019

@scottblackburn 'block-editor' is the new package extracted from the existing 'editor' package. It will be available in the WordPress 5.2 Core Gutenberg version: https://make.wordpress.org/core/2019/04/09/the-block-editor-javascript-module-in-5-2/

@roguewdesign
Copy link

roguewdesign commented Jul 31, 2019

Hi there, just looking for an update or possible solution for this. I took a look at the 'block-editor' mentioned by @scottblackburn, but didn't have any luck.

I know the code provided is incorrect, but here is what I'm trying to achieve:

  • Create a Meta Box Block that allows the user to create a table (['core/table'] block).
  • The meta data that is saved would then be the actual table block html.
  • This would allow the user to easily create a table which we could then place anywhere within a single-{custom-post-type}.php template file.
  • The reason for using meta and not a Block Template for the post type, is so we can include schema.org details in the template as well as customize the layout in the template file, vs a block template assigned to the custom post type.

I was able to successfully create and save Post Meta using a custom block following this tutorial:

https://developer.wordpress.org/block-editor/tutorials/metabox/meta-block-1-intro/

However, I have not been able to save the InnerBlocks content to the meta field.

You'll notice I've include 2 different methods for trying to save the content to the meta field. The InnerBlocks has been used with a restricted template to ['core/table'], as I couldn't figure out how to add just a Table block.

Obviously, I am also quite new at developing with Blocks :D

( function( blocks, editor, i18n, element, components, _ ) {
	var el = wp.element.createElement;
	const { InnerBlocks } = wp.blockEditor;

	var ALLOWED_BLOCKS = [ 'core/table' ];
	var TEMPLATE = [
		[ 'core/table' ]
	];

	blocks.registerBlockType( 'rogue-theme-custom-blocks/rogue-product-specifications-block', {
		title: i18n.__( 'Product Specifications', 'rogue-theme-gutenberg-blocks' ),
		icon: 'index-card',
		category: 'layout',
		attributes: {
            blockValue: {
                type: 'string',
                source: 'meta',
                meta: 'sm_product_specifications_meta_block_field'
            }
        },
		edit: function( props ) {
			var attributes = props.attributes;
			var setAttributes = props.setAttributes;

			function updateBlockValue( blockValue ) {
                setAttributes({ blockValue:  InnerBlocks.Content});
            }

			return (
				el( 'div', { className: 'product-specifications ' + props.className },
					el( InnerBlocks, {
						value: attributes.blockValue,
						onUpdate: updateBlockValue,
						allowedBlocks: ALLOWED_BLOCKS,
						template: TEMPLATE,
						templateLock: true
					} ),
				)
			)
		},
		save: function( props ) {
			props.setAttributes({blockValue: InnerBlocks.Content});
			return null;
		},
	} );

} )(
	window.wp.blocks,
	window.wp.editor,
	window.wp.i18n,
	window.wp.element,
	window.wp.components,
	window._,
);

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P [Type] Enhancement A suggestion for improvement. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

8 participants