Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@royboy789
Last active November 21, 2018 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save royboy789/be20230a1f6ab17da02b66e26367e5fb to your computer and use it in GitHub Desktop.
Save royboy789/be20230a1f6ab17da02b66e26367e5fb to your computer and use it in GitHub Desktop.
Learn Gutenberg es5 tutorial
( function( wp ) {
var el = wp.element.createElement;
var __ = wp.i18n.__;
var myComponents = window.myComponents;
var Input = myComponents.inputComponent;
wp.blocks.registerBlockType( 'learn-gutenberg/ex2-plainjs', {
title: __( 'Learn Gutenberg Example 2: Plain JS', 'learn-gutenberg' ),
category: 'widgets',
supportHTML: false,
attributes: {
who: {
type: 'string',
selector: 'p',
attribute: 'who',
},
},
edit: function( props ) {
var attributes = props.attributes,
setAttributes= props.setAttributes,
className = props.className,
focus = props.focus,
id = props.id;
//set default
if( ! attributes.hasOwnProperty( 'who' ) ){
setAttributes( { who: __( 'Roy', 'ex2-plainjs' ) } )
}
//Change event for form input
var whoChange = function (event){
setAttributes( { who:event.target.value} );
};
//Create block UI using WordPress createElement
return el(
'div',
{ className: className },
[
el(
'p',
{
who: attributes.who
},
__( 'Who', 'ex2-plainjs' ) + ': ' + attributes.who
),
el(
'div',
{
},
[
el(
'label',
{
htmlFor: id + '-control'
},
__( 'Who', 'ex2-plainjs' )
),
Input( id, attributes, whoChange ),
]
),
]
);
},
save: function( props, className) {
var attributes = props.attributes;
var who = attributes.who || 'no one at all';
return el(
'p',
{
className:className,
who: attributes.who
},
__( 'Who', 'ex2-plainjs' ) + ': ' + who,
);
}
} );
} )(
window.wp
);
var myComponents = myComponents || {};
var el = wp.element.createElement;
var __ = wp.i18n.__;
myComponents.inputComponent = function( id, attributes, changeCallback ) {
return el(
'input',
{
id: id + '-control',
value: attributes.who,
onChange: changeCallback
}
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment