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?