Skip to content

Instantly share code, notes, and snippets.

@royboy789
Created January 23, 2018 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royboy789/38907ad3d6c05af1706efd0ec42ad8ab to your computer and use it in GitHub Desktop.
Save royboy789/38907ad3d6c05af1706efd0ec42ad8ab to your computer and use it in GitHub Desktop.
Enough React for Gutenberg
import { Input } from './components/Input.React';
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const el = wp.element.createElement;
registerBlockType( 'learn-gutenberg/ex2-react', {
title: __( 'Learn Gutenberg Example 2: React', 'learn-gutenberg' ),
category: 'widgets',
supportHTML: false,
attributes: {
who: {
selector: 'p',
attribute: 'who'
},
salutation: {
selector: 'p',
attribute: 'salutation',
default: 'Hi'
},
bold: {
selector: 'p',
attribute: 'bold'
}
},
edit: ({attributes, setAttributes, className, focus, id}) => {
const inputChangeHandler = ( event ) => {
let newAttr = {};
newAttr[event.target.id] = event.target.value;
setAttributes( newAttr );
};
const changeBold = ( event ) => {
setAttributes({bold: event.target.value});
};
return (
<div className="{ className }">
<div>
<p>{attributes.salutation} {attributes.who}</p>
<p>{attributes.bold}</p>
</div>
<div>
<select onChange={changeBold}>
<option value="true">True</option>
<option value="false">False</option>
</select>
<Input
className={className}
changeHandler={inputChangeHandler}
value={attributes.salutation}
label="Salutation"
id="salutation"
/>
<Input
className={className}
changeHandler={inputChangeHandler}
value={attributes.who}
label="Who"
id="who"
/>
</div>
</div>
);
},
save: function({attributes}) {
if ( 'true' === attributes.bold ) {
return el(
'p',
{
who: attributes.who,
bold: attributes.bold,
salutation: attributes.salutation
},
[
el(
'strong',
{},
__( 'Hi', 'text-domain' ) + ' ' + attributes.who
)
]
);
}
return el(
'p',
{
who: attributes.who,
bold: attributes.bold,
salutation: attributes.salutation
},
__( 'Hi', 'text-domain' ) + ' ' + attributes.who
);
}
} );
const { __ } = wp.i18n;
const { Component } = wp.element;
const el = wp.element.createElement;
export class Input extends Component {
constructor( props ) {
super( ...arguments );
}
render() {
return (
<div className={this.props.className}>
<label htmlFor={this.props.id}>{__( this.props.label + '?', 'text-domain' )}</label>
<input id={this.props.id} type="text" onChange={this.props.changeHandler} value={this.props.value} label={this.props.label} />
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment