Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Forked from royboy789/Input.es6.js
Last active January 24, 2018 22:06
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 Shelob9/67de1bc6d0b22a114661c51c618993b4 to your computer and use it in GitHub Desktop.
Save Shelob9/67de1bc6d0b22a114661c51c618993b4 to your computer and use it in GitHub Desktop.
Gutenberg React Component
const inputChangeHandler = ( event ) => {
let newAttr = {};
newAttr[event.target.id] = event.target.value;
setAttributes( newAttr );
};
const changeBold = ( event ) => {
setAttributes({bold: event.target.value});
};
import Input from './components/Input';
import Select from './components/Select';
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 el(
'div',
{ className: className },
[
el(
'p',
{},
attributes.salutation + ' ' + attributes.who
),
el(
'p',
{},
attributes.bold
),
el(
'div',
{},
[
el(
Select,
{
id: 'bold',
changeHandler: changeBold
}
)
]
),
el(
'div',
{},
Input( className, 'salutation', attributes.salutation, 'Salutation', inputChangeHandler )
),
el(
'div',
{},
Input( className, 'who', attributes.who, 'Who', inputChangeHandler )
)
]
);
},
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
);
}
} );
constructor( props ) {
super( ...props );
this.selectCallback = this.selectCallback.bind(this);
this.state = {
current_editing: 'The bold attribute has NOT been changed',
change_time: 0
}
}
var __ = wp.i18n;
var el = wp.element.createElement;
function Input( className, id, value, label, changeHandler ) {
return el(
'div',
{
className: className
},
[
el(
'label',
{
htmlFor: id
},
__( label + '?', 'text-domain' )
),
el(
'input',
{
id: id,
type: "text",
onChange: changeHandler,
value: value,
label: label
}
)
]
)
}
const { __ } = wp.i18n;
const { Component } = wp.element;
const el = wp.element.createElement;
export default function Input( className, id, value, label, changeHandler ) {
return el(
'div',
{
className: className
},
[
el(
'label',
{
htmlFor: id
},
__( label + '?', 'text-domain' )
),
el(
'input',
{
id: id,
type: "text",
onChange: changeHandler,
value: value,
label: label
}
)
]
)
}
const { __ } = wp.i18n;
import React from 'react';
const el = React.createElement;
export default function Input( className, id, value, label, changeHandler ) {
return el(
'div',
{
className: className
},
[
el(
'label',
{
htmlFor: id
},
__( label + '?', 'text-domain' )
),
el(
'input',
{
id: id,
type: "text",
onChange: changeHandler,
value: value,
label: label
}
)
]
)
}
el(
'label',
{
htmlFor: this.props.id
},
this.props.label
);
const { __ } = wp.i18n;
import React from 'react';
const el = React.createElement;
export default class Select extends React.Component {
constructor( props ) {
super( ...arguments );
}
render() {
return el(
'div',
{
className: this.props.className,
},
[
el(
'label',
{
htmlFor: this.props.id
},
this.props.label
),
el(
'select',
{
onChange: this.props.changeHandler,
id: this.props.id,
value: this.props.value
},
[
el(
'option',
{
value: 'true'
},
__( 'Bold On', 'text-domain' )
),
el(
'option',
{
value: 'false'
},
__( 'Bold Off', 'text-domain' )
)
]
)
],
);
}
}
const { __ } = wp.i18n;
const { Component } = wp.element;
const el = wp.element.createElement;
export default class Select extends Component {
constructor( props ) {
super( ...arguments );
}
render() {
return el(
'div',
{
className: this.props.className,
value: this.props.value
},
[
el(
'label',
{
htmlFor: this.props.id
},
this.props.label
),
el(
'select',
{
onChange: this.props.changeHandler,
id: this.props.id
},
[
el(
'option',
{
value: 'true'
},
__( 'Bold On', 'text-domain' )
),
el(
'option',
{
value: 'false'
},
__( 'Bold Off', 'text-domain' )
)
]
)
],
);
}
}
el(
'select',
{
onChange: this.props.changeHandler,
id: this.props.id,
value: this.props.value
},
[
el(
'option',
{
value: 'true'
},
__( 'Bold On', 'text-domain' )
),
el(
'option',
{
value: 'false'
},
__( 'Bold Off', 'text-domain' )
)
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment