diff --git a/docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md b/docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md index 0ad9966b91a12..4f6e54f148ed7 100644 --- a/docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md +++ b/docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md @@ -10,26 +10,28 @@ By specifying the source of the attributes as `meta`, the Block Editor automatic Add this code to your JavaScript file (this tutorial will call the file `myguten.js`): +{% codetabs %} +{% ES5 %} ```js ( function( wp ) { var el = wp.element.createElement; var registerBlockType = wp.blocks.registerBlockType; - var TextField = wp.components.TextControl; + var TextControl = wp.components.TextControl; - registerBlockType("myguten/meta-block", { - title: "Meta Block", - icon: "smiley", - category: "common", + registerBlockType( 'myguten/meta-block', { + title: 'Meta Block', + icon: 'smiley', + category: 'common', attributes: { blockValue: { - type: "string", - source: "meta", - meta: "myguten_meta_block_field" + type: 'string', + source: 'meta', + meta: 'myguten_meta_block_field' } }, - edit: function(props) { + edit: function( props ) { var className = props.className; var setAttributes = props.setAttributes; @@ -37,11 +39,11 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten setAttributes({ blockValue }); } - return el( - "div", + return el( + 'div', { className: className }, - el( TextField, { - label: "Meta Block Field", + el( TextControl, { + label: 'Meta Block Field', value: props.attributes.blockValue, onChange: updateBlockValue } ) @@ -53,9 +55,52 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten save: function() { return null; } - }); -})( window.wp ); + } ); +} )( window.wp ); ``` +{% ESNext %} +```jsx +import { registerBlockType } from '@wordpress/blocks'; +import { TextControl } from '@wordpress/components'; + +registerBlockType( 'myguten/meta-block', { + title: 'Meta Block', + icon: 'smiley', + category: 'common', + + attributes: { + blockValue: { + type: 'string', + source: 'meta', + meta: 'myguten_meta_block_field', + }, + }, + + edit( { className, setAttributes, attributes } ) { + + function updateBlockValue( blockValue ) { + setAttributes( { blockValue } ); + } + + return ( +
+ +
+ ); + }, + + // No information saved to the block + // Data is saved to post meta via attributes + save() { + return null; + } +} ); +``` +{% end %} **Important:** Before you test, you need to enqueue your JavaScript file and its dependencies. Note the WordPress packages used above are `wp.element`, `wp.blocks`, and `wp.components`. Each of these need to be included in the array of dependencies. Update the `myguten-meta-block.php` file adding the enqueue function: