Last active
November 21, 2018 14:51
-
-
Save royboy789/be20230a1f6ab17da02b66e26367e5fb to your computer and use it in GitHub Desktop.
Learn Gutenberg es5 tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( 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 | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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