【问题标题】:Saving SelectControl option保存 SelectControl 选项
【发布时间】:2019-09-03 10:52:17
【问题描述】:

我已经成功创建了一个 WordPress 自定义 Gutenberg 块,并创建了一个面板部分来存储我的所有块选项。

但是,我对做出反应非常陌生,并且我已经设法将其搭建在一起。我试图给这个选择一个初始状态,并在更改时保存状态。

我知道我需要对 withState 做一些事情,但我不确定我是否可以看到我的承诺失败了,但我不确定为什么。

// Block Options
const withInspectorControls =  createHigherOrderComponent( ( BlockEdit ) => {
  return (props) => {
    const size = {size:"display-2"};
    return (
      <Fragment>
        <BlockEdit { ...props } />
        <InspectorControls>
          <PanelBody title="Display Block Settings"
            icon="welcome-widgets-menus"
            initialOpen={ true }
          >
            <SelectControl
              label="Display Size"
              value={size}
              options={[
                { label: 'Display 1', value: 'display-1' },
                { label: 'Display 2', value: 'display-2' },
                { label: 'Display 3', value: 'display-3' },
                { label: 'Display 4', value: 'display-4' },
              ]}
              onChange={ ( size ) => { setState( { size:size } ) } } 
            />
          </PanelBody>
        </InspectorControls>
      </Fragment>
    );
  };
}, "withInspectorControl" );

wp.hooks.addFilter( 'editor.BlockEdit', 'display-heading/with-inspector-controls', withInspectorControls );

【问题讨论】:

    标签: wordpress reactjs wordpress-gutenberg


    【解决方案1】:

    你在正确的轨道上。正如你已经提到的,你真的只需要添加withState HOC。这可能如下所示:

    // 1. add the withState import
    import { withState } from '@wordpress/compose';
    
    // 2. wrap your SelectControl with the withState HOC
    const MySelectControl = withState( {
        // this is your state, in this case display-2 would be the default
        size: 'display-2',
    } )( ( { size, setState } ) => (
        <SelectControl
            label="Size"
            value={ size }
            options={ [
                { label: 'Display 1', value: 'display-1' },
                { label: 'Display 2', value: 'display-2' },
                { label: 'Display 3', value: 'display-3' },
                { label: 'Display 4', value: 'display-4' },
            ] }
            onChange={ ( size ) => { setState( { size } ) } }
        />
    ) );
    
    // Block Options
    const withInspectorControls =  createHigherOrderComponent( ( BlockEdit ) => {
      return (props) => {
        return (
          <Fragment>
            <BlockEdit { ...props } />
            <InspectorControls>
              <PanelBody title="Display Block Settings"
                icon="welcome-widgets-menus"
                initialOpen={ true }
              >
                // 3. now you can add your component in your panel
                <MySelectControl />
              </PanelBody>
            </InspectorControls>
          </Fragment>
        );
      };
    }, "withInspectorControl" );
    
    wp.hooks.addFilter( 'editor.BlockEdit', 'display-heading/with-inspector-controls', withInspectorControls );
    

    Reacts 高阶组件一开始可能会让人很困惑。但是如果你熟悉OOP 范式,你可以把它们想象成composition 模式。对于 Gutenberg 开发来说,最重要的事情是它们总是返回一个新组件。这就是为什么我能够像这样使用它&lt;MySelectControl /&gt;

    【讨论】:

    • 感谢@Orlandster 提供如此深入的答案!
    猜你喜欢
    • 2020-08-08
    • 2014-12-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2012-10-27
    • 2011-11-30
    相关资源
    最近更新 更多