I asked a previous question, and got a helpful answer that got me going quite a ways. The present problem is unrelated, but IMO reflects the dearth of documentation suitable for those of us who aren't familiar with some of the nuances of javascript, are completely new to Gutenberg development, and yet need to tweak the new Wordpress here and there. I'll state right up front that I'm new to javascript, and suspect that my issue is related to that fact.
As stated in my previous question, I'm working on modifying the featured image control in the admin edit screen of a custom post type. I want to add a checkbox, and a text input field to capture a user-supplied resource URL. I have a javascript file that loads on the page in question, along with all the requisite dependencies to work with Gutenberg, JSX, and all of the react-based components (react-devel, react-dom, babel, wp-blocks, wp-i18n, wp-element, wp-editor, wp-hooks). I've also got a filter in place that sets the script type of my .js file to 'text/babel' so JSX interprets correctly. I don't think I've missed anything.
Here's a version of my .js file that displays a heading and rich text field. It's not what I'm after but it does work and there aren't any complaints in the javascript console. At this point I'm still trying to figure out the building blocks I need, and how to use them.
window.addEventListener("load", function(event){
console.log("featured_image.js loaded and functional...");
});
const el = wp.element.createElement;
// const withState = wp.compose.withState;
const setState = wp.data.setState;
const withSelect = wp.data.withSelect;
const withDispatch = wp.data.withDispatch;
const { CheckboxControl } = wp.editor;
const { RichText } = wp.editor;
const { useState } = wp.element;
const { TextControl } = wp.components
const { withState } = wp.compose;
wp.hooks.addFilter(
'editor.PostFeaturedImage',
'dsplugin/featured-image-as-video',
wrapPostFeaturedImage
);
//this works
const MyRichTextField =
<RichText
placeholder="placeholder text"
/>
;
function wrapPostFeaturedImage( OriginalComponent ) {
return function( props ) {
return (
el(
wp.element.Fragment,
{},
// 'Prepend above',
el(
OriginalComponent,
props
),
<strong>this is a test</strong>,
// MyCheckboxControl
MyRichTextField
)
);
}
}
Here's what the output looks like...
When I try to add a checkboxControl though, using the same methodology I used for the RichText field, I run into problems. This code...
const MyCheckboxControl =
const [ isChecked, setChecked ] = useState( true );
<CheckboxControl
heading="User"
label="Is author"
help="Is the user a author or not?"
checked={ isChecked }
onChange={ setChecked }
/>
;
Doesn't work. And the javascript console says...
SyntaxError: ... Unexpected token (28:4)
27 | const MyCheckboxControl = () => (
> 28 | const [ isChecked, setChecked ] = useState( true );
| ^
29 | <CheckboxControl
30 | heading="User"
31 | label="Is author"
However, this is exactly how the Gutenberg documentation says we should implement a CheckboxControl. At least when creating blocks. Are things different in the sidebar? I tried a TextControl, as follows...
const MyTextControl = withState( {
className: '',
} )( ( { className, setState } ) => (
<TextControl
label="Additional CSS Class"
value={ className }
onChange={ ( className ) => setState( { className } ) }
/>
) );
No syntax error for the above definition, but when I try to use it down in the wrapPostFeaturedImage() function as follows:
function wrapPostFeaturedImage( OriginalComponent ) {
return function( props ) {
return (
el(
wp.element.Fragment,
{},
// 'Prepend above',
el(
OriginalComponent,
props
),
<strong>this is a test</strong>,
// MyCheckboxControl
MyTextControl
)
);
}
}
I get the following warning below in the console, and nothing shows up after the "this is a test" heading.
Warning: Functions are not valid as a React child.
This may happen if you return a Component instead of <Component /> from render.
Or maybe you meant to call this function rather than return it.
I feel like I have a fundamental disconnect with what's going on here. What am I missing?