var el = wp.element.createElement
var withSelect = wp.data.withSelect
wp.blocks.registerBlockType('voucher/shortcode', {
title: 'Voucher Shortcode',
icon: 'smiley',
category: 'common',
attributes: {
category: {
type: 'string',
}
},
edit: withSelect( function( select ) {
var pages = select('core').getEntityRecords('postType', 'voucher', { per_page: -1 });
return {
posts: pages
};
} )
(function(props) {
if ( ! props.posts ) {
return "Loading...";
}
if ( props.posts.length === 0 ) {
return "No posts";
}
var className = props.className;
var post = props.posts[ 0 ];
var options = [];
var option = el(
'option',
{ value: '' },
__('Choose')
);
options.push(option);
for (var i = 0; i < props.posts.length; i++) {
option = el(
'option',
{ value: props.posts[i].id },
props.posts[i].title.raw
);
options.push(option);
}
return [
el('select', {
onChange: onSelectVoucher,
},
options
)
]
}),
save: function() {
return null;
} })
You can try this for creating a dynamic select field with your CPTs voucher.
The event-function onChange: onSelectVoucher
is up to you what should happen.