将选择控制值传递给块

时间:2019-05-03 作者:Astrid

我创建了一个gutenberg块和一个具有一些类名的select控件。

选择控件位于侧栏中自己的面板中。

我的问题是当我尝试调用select控件值/大小时。我所得到的只是未定义的。

我哪里出错了?在编辑/保存块时,如何获取MySelectControls值并存储该值?


//  Import CSS.
import \'./style.scss\';
import \'./editor.scss\';

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { RichText, InspectorControls } = wp.editor;
const { Fragment } = wp.element;
const { Panel, PanelBody, PanelRow, SelectControl } = wp.components;
const { createHigherOrderComponent, withState } = wp.compose;
const { createHooks, addFilter, removeFilter, applyFilters  } = wp.hooks;



// Create my select control
const MySelectControl = withState( {
    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 - Render it in a panel
const withInspectorControls =  createHigherOrderComponent( ( BlockEdit ) => {

    return (props) => {
        return (


            <Fragment>
            <BlockEdit { ...props } />
            <InspectorControls>

            <PanelBody
            title="My Block Settings"
            icon="welcome-widgets-menus"
            initialOpen={ true }
            >
            <PanelRow>

            <MySelectControl  /> // get this value

            </PanelRow>
            </PanelBody>




            </InspectorControls>

            </Fragment>
        );
    };
}, "withInspectorControl" );

wp.hooks.addFilter( \'editor.BlockEdit\', \'cgb/block-display-heading\', withInspectorControls );





function handleSize(){

    return MySelectControl.size; // get the value

}


const sizeH = handleSize(); // store the value

registerBlockType( \'cgb/block-display-heading\', {

    title: __( \'display-heading - CGB Block\' ),
    icon: \'shield\',
    category: \'common\',
    keywords: [
        __( \'Display Heading\' ),
        __( \'CGB Example\' ),
        __( \'create-guten-block\' ),
    ],
    attributes: {
        content: {
            type: \'array\',
            source: \'children\',
            selector: \'h1\',
        }
    },



    edit: function( props ) {

        console.log(sizeH);

        const { attributes: { content }, setAttributes, className, size } = props;

        const onChangeContent = ( newContent ) => {
            setAttributes( { content: newContent } );

        };

        return (

            <div class="display_wrapper">
            <RichText
            tagName="h1"
            className={ className + \' \' + sizeH } // display the value here
            onChange={ onChangeContent }
            value={ content }
            placeholder="Enter text..."
            />
            </div>
        );
    },

    save: function( props ) {

        return (
            <div class="display_wrapper">
            <RichText.Content tagName="h1" className={`testing ` + sizeH } value={ props.attributes.content } /> // and here!
            </div>
        );
    },
} );

1 个回复
SO网友:Astrid

我通过这样做解决了这个问题:-

在编辑功能中创建选择控件

{

  <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 ) => props.setAttributes( { size: size } ) }
      />

}
创建属性(大小)更改选择控件以更改大小属性,将道具传递给保存功能道具。属性。尺寸(L)

相关推荐

使用JavaScript的WP REST API发布状态

我在WP REST API中弄脏了我的手。我已经阅读了一些教程,并对使用JavaScript创建新帖子提出了一个问题。在里面this 教程,帖子var status = \'draft\'; (请参见代码)。So I am just worried that won\'t anyone able to hack that status?jQuery( document ).ready( function ( $ ) { $( \'#post-submission-form\' ).on(