如何让ToggleControl Gutenberg组件在PHP块中工作

时间:2019-09-04 作者:NextGenThemes

我的一切都基于此:https://gist.github.com/pento/cf38fd73ce0f13fcf0f0ae7d6c4b685d 只是切换没有得到切换的视觉表示。有关于这个的教程吗?我发现的一切都是复杂的“真实的”阻止了大量的进口,而这种react风格返回了我现在不需要的东西。我只是想知道如何让开关工作。

PHP

<?php
function register_gb_block() {

    // ...

    register_assets();
    register(
        [
            \'handle\' => \'arve-block\',
            \'src\'    => plugins_url( \'dist/js/test-block.js\', PLUGIN_FILE ),
            \'deps\'   => [ \'wp-blocks\', \'wp-element\', \'wp-components\', \'wp-editor\' ],
            \'ver\'    => ver( VERSION, \'dist/js/test-block.js\', PLUGIN_FILE ),
            \'footer\' => false
        ]
    );

    wp_localize_script( \'arve-block\', \'ARVEsettings\', $sc_settings );

    // Register our block, and explicitly define the attributes we accept.
    register_block_type(
        \'nextgenthemes/arve-block\',
        [
            \'attributes\'      => array(
                \'foo\' => array(
                    \'type\' => \'string\',
                ),
                \'toggle\' => array(
                    \'type\' => \'boolean\',
                ),
            ),
            \'editor_script\'   => \'arve-block\',
            \'editor_style\'    => \'advanced-responsive-video-embedder\',
            \'render_callback\' => __NAMESPACE__ . \'\\shortcode\'
        ]
    );
}
Theregister 只是一个包装wp_register_script

JS公司

// License: GPLv2+ based on: https://gist.github.com/pento/cf38fd73ce0f13fcf0f0ae7d6c4b685d

const el = wp.element.createElement,
    registerBlockType = wp.blocks.registerBlockType,
    ServerSideRender = wp.components.ServerSideRender,
    TextControl = wp.components.TextControl,
    ToggleControl = wp.components.ToggleControl,
    InspectorControls = wp.editor.InspectorControls;

/*
 * Here\'s where we register the block in JavaScript.
 *
 * It\'s not yet possible to register a block entirely without JavaScript, but
 * that is something I\'d love to see happen. This is a barebones example
 * of registering the block, and giving the basic ability to edit the block
 * attributes. (In this case, there\'s only one attribute, \'foo\'.)
 */
registerBlockType( \'nextgenthemes/arve-block\', {
    title: \'PHP Block\',
    icon: \'megaphone\',
    category: \'widgets\',

    /*
     * In most other blocks, you\'d see an \'attributes\' property being defined here.
     * We\'ve defined attributes in the PHP, that information is automatically sent
     * to the block editor, so we don\'t need to redefine it here.
     */

    edit: ( props ) => {
        return [
            /*
             * The ServerSideRender element uses the REST API to automatically call
             * php_block_render() in your PHP code whenever it needs to get an updated
             * view of the block.
             */
            el( ServerSideRender, {
                block: \'nextgenthemes/arve-block\',
                attributes: props.attributes,
            } ),
            /*
             * InspectorControls lets you add controls to the Block sidebar. In this case,
             * we\'re adding a TextControl, which lets us edit the \'foo\' attribute (which
             * we defined in the PHP). The onChange property is a little bit of magic to tell
             * the block editor to update the value of our \'foo\' property, and to re-render
             * the block.
             */
            el( InspectorControls, {},
                el(
                    TextControl,
                    {
                        label: \'Foo\',
                        value: props.attributes.foo,
                        onChange: ( value ) => {
                            props.setAttributes( { foo: value } );
                        },
                    }
                ),
                el(
                    ToggleControl,
                    {
                        label: \'Toogle\',
                        value: props.attributes.toggle,
                        onChange: ( value ) => {
                            props.setAttributes( { toggle: value } );
                        },
                    }
                ),
            ),
        ];
    },

    // We\'re going to be rendering in PHP, so save() can just return null.
    save: () => {
        return null;
    },
} );

1 个回复
最合适的回答,由SO网友:Alvaro 整理而成

问题是ToggleControl 您对值使用了错误的道具。应该是这样的checked 而不是value.

同时使用blockEditor 包而不是editor 对于InspectorControls 因为它将被弃用。

<小时>

el(
    ToggleControl,
    {
        label: \'Toogle\',
        checked: props.attributes.toggle, // here
        onChange: ( value ) => {
            props.setAttributes( { toggle: value } );
        },
    }
)
它虽然不能正确显示值,但仍能正确更新值的原因是组件ToggleControl使用的状态在组件之外。此状态由属性+设置属性表示,正在使用正确更新setAttributes. 但是,它没有正确显示为道具checked 未显示更新值。这意味着checked 将使用其默认值false.

相关推荐

带有高级自定义域的JavaScript选项卡库

我在我的网站上使用高级自定义字段,我想以一种体面、优雅的方式显示我的图库字段的图片。所以我在考虑做这样的事情:https://www.w3schools.com/howto/howto_js_tab_img_gallery.asp我的主要问题是,我不知道如何将php变量放到javascript代码中。你能帮忙吗?w3schools示例中的相同css和相同脚本,这是我的代码:<?php $images = get_field(\'extra_photos\');