Skip to content

Commit

Permalink
Meta Box Tutorial: Default to ESNext (#22748)
Browse files Browse the repository at this point in the history
Minor change switching order code toggle so ESNext is first.

Per #22151 and #17873
  • Loading branch information
mkaz committed Jun 4, 2020
1 parent bee8d1a commit 8efd443
Showing 1 changed file with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ The hook `useEntityProp` can be used by the blocks to get or change meta values.
Add this code to your JavaScript file (this tutorial will call the file `myguten.js`):

{% codetabs %}
{% ESNext %}
```js
import { registerBlockType } from '@wordpress/blocks';
import { TextControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';

registerBlockType( 'myguten/meta-block', {
title: 'Meta Block',
icon: 'smiley',
category: 'text',

edit( { className, setAttributes, attributes } ) {
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
const [ meta, setMeta ] = useEntityProp(
'postType',
postType,
'meta'
);
const metaFieldValue = meta['myguten_meta_block_field'];
function updateMetaValue( newValue ) {
setMeta( { ...meta, 'myguten_meta_block_field': newValue } );
}

return (
<div className={ className }>
<TextControl
label="Meta Block Field"
value={ metaFieldValue }
onChange={ updateMetaValue }
/>
</div>
);
},

// No information saved to the block
// Data is saved to post meta via the hook
save() {
return null;
},
} );
```
{% ES5 %}
```js
( function( wp ) {
Expand Down Expand Up @@ -72,51 +117,6 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten
} );
} )( window.wp );
```
{% ESNext %}
```js
import { registerBlockType } from '@wordpress/blocks';
import { TextControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';

registerBlockType( 'myguten/meta-block', {
title: 'Meta Block',
icon: 'smiley',
category: 'text',

edit( { className, setAttributes, attributes } ) {
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
const [ meta, setMeta ] = useEntityProp(
'postType',
postType,
'meta'
);
const metaFieldValue = meta['myguten_meta_block_field'];
function updateMetaValue( newValue ) {
setMeta( { ...meta, 'myguten_meta_block_field': newValue } );
}

return (
<div className={ className }>
<TextControl
label="Meta Block Field"
value={ metaFieldValue }
onChange={ updateMetaValue }
/>
</div>
);
},

// No information saved to the block
// Data is saved to post meta via the hook
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`, `wp.components`, `wp.data`, and `wp.coreData`. Each of these need to be included in the array of dependencies. Update the `myguten-meta-block.php` file adding the enqueue function:
Expand Down

0 comments on commit 8efd443

Please sign in to comment.