最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gutenberg - dynamically created element: how to set onChange?

programmeradmin1浏览0评论

having blocks with many attribues, I'm trying to optimize the code to write things just once.

Because of this I created a PHP array used for default attributes in register_block_type()

I pass it into the javascript block part and I'm trying to dynamically create block elements. Everything works.. except the onChange callback.

// defaults == arraay containing attributes and their data
var controls = [];
$.each(defaults, function(key, val) {
    var type = SelectControl; 

    // key == attribute name
    controls.push(
        createElement( type, {
            value: attributes.key,
            label: val.label,
            onChange: function(e) {
                props.setAttributes({ key : event.target.value});
            },
            options: [...]
        })
    );
});

If I change field values, I get

Error loading block: Invalid parameter(s): attributes

While everything's ok using the precise attribute name (attName in this snippet)

onChange: function(e) {
   props.setAttributes({ attName : event.target.value});
},

How can I use the dynamic "key" variable?

发布评论

评论列表(0)

  1. 暂无评论