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

Issue with InnerBlock's template prop in version > 5.4.4 #27897

Closed
WYKDev opened this issue Dec 25, 2020 · 1 comment · Fixed by #28007
Closed

Issue with InnerBlock's template prop in version > 5.4.4 #27897

WYKDev opened this issue Dec 25, 2020 · 1 comment · Fixed by #28007
Assignees
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P [Status] In Progress Tracking issues with work in progress [Type] Regression Related to a regression in the latest release

Comments

@WYKDev
Copy link

WYKDev commented Dec 25, 2020

Describe the bug
I am running into an issue with InnerBlock's template prop. When I increase or decrease count, it doesn't change. Only when I refresh the editor the change maybe apply. This happens in the version > 5.4.4. Version 5.4.4 was fine without this issue.

Screen Recorded
innerblock bug

Below is a simple reproduction of code.

   // Import WP Gutenberg Components
   const { InnerBlocks, InspectorControls } = wp.blockEditor
   const { Component } = wp.element
   const { 
      PanelBody, RangeControl
   } = wp.components


   const { __ } = wp.i18n;


   const getTemplates = (items, block) => {
      // Tempporary store
      const store = [];
      // Slot each template into the store 
      for(let i=0; i<items; i++) store[i] = [block];

      return store;
   };


   const IB_OPTIONS = {
      templateLock: 'all'
   };


   class PanelColumns extends Component {
      
      constructor() {
         super(...arguments);
      }

      render() {
         const {
            PanelBody, RangeControl,
            attributes, setAttributes,
         } = this.props;

         const { items } = attributes;

         const Items = <RangeControl
            label={ __('Number of items in the section') }
            value={ items }
            onChange={ items => setAttributes( { items } ) }
            min={ 1 }
            max={ 12 }
         />

         // Debug Logger
         // console.log(attributes);
         
         return (
            <PanelBody title={ __('Columns') } initialOpen={ true }>
               
               { Items }
            </PanelBody>
         );
      }
   };


   export default function ({ clientId, attributes, className, setAttributes }) {
      
      const {
         items
      } = attributes

      // Common options needed for each Panel Control on right side of the editor
      const PanelCommonOptions = { attributes, setAttributes, PanelBody }

      // Save the InnerBlocks in a new variable
      const innerBlockContent = <InnerBlocks { ...IB_OPTIONS } template={ getTemplates(items, 'core/image') } />
      console.log(innerBlockContent)
      return (
         <>
            <InspectorControls>
               
               <PanelColumns {...{ RangeControl, ...PanelCommonOptions }} />

            </InspectorControls>

            <div className={ className } >
               { innerBlockContent }
            </div>
         </>
      )
   }
@youknowriad youknowriad added [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P Needs Technical Feedback Needs testing from a developer perspective. Needs Testing Needs further testing to be confirmed. labels Jan 1, 2021
@talldan talldan added [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended [Type] Regression Related to a regression in the latest release and removed Needs Technical Feedback Needs testing from a developer perspective. Needs Testing Needs further testing to be confirmed. [Type] Bug An existing feature does not function as intended labels Jan 6, 2021
@talldan
Copy link
Contributor

talldan commented Jan 6, 2021

There's a related issue (#16955), but the key difference here is that your example shows templateLock set to all.

When template lock is set that way, the template synchronises more regularly and should update the existing content:

// Only synchronize innerBlocks with template if innerBlocks are empty or
// a locking all exists directly on the block.
if ( innerBlocks.length === 0 || templateLock === 'all' ) {

The bug is that the react hook that updates the template doesn't have the template variable listed in its dependencies, so isn't triggered when that value changes:

Prior to this being rewritten to a React hook, this functionality used the componentDidUpdate lifecycle method, so didn't have a concept of dependencies.

I have a fix in progress (it's a one-liner fix but I'll try to add some tests to prevent future regressions.)

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 [Status] In Progress Tracking issues with work in progress [Type] Regression Related to a regression in the latest release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants