Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active July 3, 2019 13:24
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/102944c6e33e1038e3fa7234e89cb3c4 to your computer and use it in GitHub Desktop.
Save Shelob9/102944c6e33e1038e3fa7234e89cb3c4 to your computer and use it in GitHub Desktop.
const attributes = {
defaultValue : {
type: 'string',
required: false,
default: 'Default Default Value'
},
blockId: {
type: 'string'
}
};
/**
* All DOM elements with block
* @type {NodeListOf<Element>}
*/
const elements = document.querySelectorAll('.wp-block-josh-isoblock');
if( ! elements.length ){
//no blocks found
return;
}
//do stuff to blocks
import React, { useState } from "react";
import Form from "./Form";
/**
* The Form component
*/
export const FormApp = props => {
const [value, onChange] = useState(props.defaultValue);
return <Form {...props} defaultValue={value} onChange={onChange} />;
};
/**
* The Form component
*/
export const Form = ({defaultValue,id,onChange} ) => {
return (
<form >
<div>
<label htmlFor={id}>
Form Field
</label>
<input
value={defaultValue}
id={id}
onChange={onChange}
className={'isoblock-field'}
/>
</div>
</form>
);
};
//Block registration API
import {registerBlockType} from '@wordpress/blocks';
//block attributes
const attributes = {
defaultValue : {
type: 'string',
required: false,
default: 'Default Default Value'
},
blockId: {
type: 'string'
}
};
//block name
const name = 'josh/isoblock';
registerBlockType(name,{
title: 'Hydrate Demo',
attributes,
category: 'common',
edit({attributes,setAttributes,className,clientId}){
return (
<div>
<p>Edit</p>
</div>
)
},
save({attributes,className,clientId}){
return (
<div>
<p>Save</p>
</div>
)
}
});
//Current attribute values
const {defaultValue,blockId} = attributes;
//Change handler for default value attribute
const setDefaultValue = defaultValue => setAttributes({defaultValue});
//Change handler for block ID attribute
const setBlockId = blockId => setAttributes({blockId});
{
edit({attributes,setAttributes,className,clientId}){
const {defaultValue,blockId} = attributes;
const setDefaultValue = defaultValue => setAttributes({defaultValue});
const setBlockId = blockId => setAttributes({blockId});
if( clientId !== blockId ){
setBlockId(clientId);
}
return (
<div>
<Form
defaultValue={defaultValue}
className={className}
id={clientId}
onChange={(event) => {
console.log(event);
setDefaultValue(event.target.value);
}}
/>
</div>
)
},
}
{
edit({attributes,setAttributes,className,clientId}){
const {defaultValue,blockId} = attributes;
const setDefaultValue = defaultValue => setAttributes({defaultValue});
const setBlockId = blockId => setAttributes({blockId});
if( clientId !== blockId ){
setBlockId(clientId);
}
return (
<div>
<InspectorControls>
<TextControl
onChange={setDefaultValue}
label={'Default Value For Form Field'}
value={defaultValue}
/>
</InspectorControls>
<Form
defaultValue={defaultValue}
className={className}
id={clientId}
onChange={(event) => {
console.log(event);
setDefaultValue(event.target.value);
}}
/>
</div>
)
}
}
{
save(props){
const {attributes,className,clientId} = props;
const {defaultValue,blockId} = attributes;
return <Form defaultValue={defaultValue} className={className} id={blockId} />
}
}
//Get WordPress default webpack config
const defaultConfig = require("./node_modules/@wordpress/scripts/config/webpack.config");
const path = require('path');
//Merge our entry point with default
module.exports = {
...defaultConfig,
//https://webpack.js.org/concepts/entry-points/
entry: {
//Block editor, what WordPress does by default
index: path.resolve( process.cwd(), 'src', 'index.js' ),
//Create a front-end JavaScript bundle
front: path.resolve( process.cwd(), 'src', 'front.js' ),
},
};
<p>
Vegan Tacos
</p>
<img src="http://placekitten.com/g/200/300" />
import {render} from 'react-dom'
render( <div>The App</div>, document.getElementById( 'root' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment