Created
January 20, 2023 15:58
-
-
Save ryanwelcher/1b410009c5501b81d181e1201089f54a to your computer and use it in GitHub Desktop.
Term selector
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
/** | |
* 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