$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Gutenberg Block checkbox check-unckeck does not work|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Gutenberg Block checkbox check-unckeck does not work

matteradmin8PV0评论

I have added the custom Gutenberg block for my shortcode, but in checkbox control check uncheck does not work, please check code done as below:

el( components.CheckboxControl, {
    label: i18n.__( 'Display it?', 'txt-domain' ),
    checked: props.attributes.myAttr,
    onChange: function( val ) {
        if( val ) {
            props.setAttributes({ myAttr: '1' })
        } else {
            props.setAttributes({ myAttr: '0' })
        }
    }
} ),

if I remove the checked: props.attributes.myAttr, then it works fine but here, checkbox does not stay checked or uncheck. by the way it saves the value.

I have added the custom Gutenberg block for my shortcode, but in checkbox control check uncheck does not work, please check code done as below:

el( components.CheckboxControl, {
    label: i18n.__( 'Display it?', 'txt-domain' ),
    checked: props.attributes.myAttr,
    onChange: function( val ) {
        if( val ) {
            props.setAttributes({ myAttr: '1' })
        } else {
            props.setAttributes({ myAttr: '0' })
        }
    }
} ),

if I remove the checked: props.attributes.myAttr, then it works fine but here, checkbox does not stay checked or uncheck. by the way it saves the value.

Share Improve this question edited Feb 16, 2019 at 11:50 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 16, 2019 at 11:19 Jaydip NimavatJaydip Nimavat 2244 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The property checked expects boolean value. (CheckboxControl)
How did you define myAttr in attributes? If you set myAttr to '1' or '0' it is both true.
Try empty instead of '0' props.setAttributes({ myAttr: '' }) or false.

Try using the withState wrapper like in the example from the docs : https://wordpress/gutenberg/handbook/designers-developers/developers/components/checkbox-control/

Post a comment

comment list (0)

  1. No comments so far