Skip to content

Instantly share code, notes, and snippets.

@ryanwelcher
Created January 20, 2023 15:58
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 ryanwelcher/1b410009c5501b81d181e1201089f54a to your computer and use it in GitHub Desktop.
Save ryanwelcher/1b410009c5501b81d181e1201089f54a to your computer and use it in GitHub Desktop.
Term selector
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, ComboboxControl } from '@wordpress/components';
import { useEntityRecords } from '@wordpress/core-data';
export default function Edit( {
attributes: { blockTaxonomyTerm },
setAttributes,
} ) {
const taxonomyRequest = useEntityRecords( 'taxonomy', 'category' );
return (
<>
<p { ...useBlockProps() }>
{ __(
'Term Selector – hello from the editor!',
'term-selector'
) }
</p>
<InspectorControls>
<PanelBody
title={ __( 'Term Selector Settings', 'term-selector' ) }
>
{ taxonomyRequest.isResolving ? (
<div>LOADING....</div>
) : (
<ComboboxControl
label="Choose a term"
value={ blockTaxonomyTerm }
onChange={ ( term_slug ) => {
console.log( term_slug );
setAttributes( {
blockTaxonomyTerm: term_slug,
} );
} }
options={ taxonomyRequest?.records?.map(
( term ) => {
return {
label: term.name,
value: term.slug,
};
}
) }
/>
) }
</PanelBody>
</InspectorControls>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment