-
-
Save Shelob9/67de1bc6d0b22a114661c51c618993b4 to your computer and use it in GitHub Desktop.
Gutenberg React Component
This file contains 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
const inputChangeHandler = ( event ) => { | |
let newAttr = {}; | |
newAttr[event.target.id] = event.target.value; | |
setAttributes( newAttr ); | |
}; | |
const changeBold = ( event ) => { | |
setAttributes({bold: event.target.value}); | |
}; |
This file contains 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
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 | |
); | |
} | |
} ); | |
This file contains 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
constructor( props ) { | |
super( ...props ); | |
this.selectCallback = this.selectCallback.bind(this); | |
this.state = { | |
current_editing: 'The bold attribute has NOT been changed', | |
change_time: 0 | |
} | |
} |
This file contains 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 __ = 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 | |
} | |
) | |
] | |
) | |
} |
This file contains 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
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 | |
} | |
) | |
] | |
) | |
} |
This file contains 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
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 | |
} | |
) | |
] | |
) | |
} |
This file contains 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
el( | |
'label', | |
{ | |
htmlFor: this.props.id | |
}, | |
this.props.label | |
); |
This file contains 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
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' ) | |
) | |
] | |
) | |
], | |
); | |
} | |
} |
This file contains 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
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' ) | |
) | |
] | |
) | |
], | |
); | |
} | |
} |
This file contains 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
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