generateblocks
/
src
/
blocks
/
query-loop
/
components
/
inspector-controls
/
controls
/
TaxonomyParameterControl.js
generateblocks
/
src
/
blocks
/
query-loop
/
components
/
inspector-controls
/
controls
Last commit date
DateTimePicker.js
3 years ago
TaxonomyParameterControl.js
3 years ago
TaxonomyParameterControl.js
98 lines
| 1 | import SimpleSelect from '../../../../../components/simple-select'; |
| 2 | import TaxonomiesSelect from '../../../../../components/taxonomies-select'; |
| 3 | import { useEffect, useMemo, useState } from '@wordpress/element'; |
| 4 | import { useTaxonomies } from '../../../../../hooks'; |
| 5 | import { ToggleControl } from '@wordpress/components'; |
| 6 | import { __ } from '@wordpress/i18n'; |
| 7 | |
| 8 | export default function TaxonomyParameterControl( props ) { |
| 9 | const { label, placeholder, value, onChange } = props; |
| 10 | const [ taxonomy, setTaxonomy ] = useState( value.taxonomy ); |
| 11 | const [ terms, setTerms ] = useState( value.terms ); |
| 12 | const [ includeChildren, setIncludeChildren ] = useState( false !== value.includeChildren ); |
| 13 | |
| 14 | const taxonomies = useTaxonomies(); |
| 15 | |
| 16 | const isHierarchical = useMemo( () => { |
| 17 | const tax = taxonomies.filter( ( record ) => ( record.slug === taxonomy ) ); |
| 18 | |
| 19 | return !! tax[ 0 ] ? tax[ 0 ].hierarchical : false; |
| 20 | }, [ JSON.stringify( taxonomies ), taxonomy ] ); |
| 21 | |
| 22 | useEffect( () => { |
| 23 | if ( value.taxonomy !== taxonomy ) { |
| 24 | setTaxonomy( value.taxonomy ); |
| 25 | } |
| 26 | |
| 27 | if ( JSON.stringify( value.terms ) !== JSON.stringify( terms ) ) { |
| 28 | setTerms( value.terms ); |
| 29 | } |
| 30 | }, [ value ] ); |
| 31 | |
| 32 | useEffect( () => { |
| 33 | if ( !! taxonomy ) { |
| 34 | const tax = taxonomies.filter( ( record ) => ( record.slug === taxonomy ) ); |
| 35 | const rest = !! tax[ 0 ] ? tax[ 0 ].rest_base : undefined; |
| 36 | const hierarchical = !! tax[ 0 ] ? tax[ 0 ].hierarchical : false; |
| 37 | |
| 38 | onChange( { |
| 39 | taxonomy, |
| 40 | terms, |
| 41 | rest, |
| 42 | includeChildren: hierarchical ? includeChildren : undefined, |
| 43 | } ); |
| 44 | } |
| 45 | }, [ taxonomy, JSON.stringify( terms ), includeChildren ] ); |
| 46 | |
| 47 | const taxonomiesOptions = useMemo( () => ( |
| 48 | taxonomies |
| 49 | .filter( ( tax ) => ( 'nav_menu' !== tax.slug ) ) |
| 50 | .map( ( tax ) => ( { value: tax.slug, label: tax.name } ) ) |
| 51 | ), [ JSON.stringify( taxonomies ) ] ); |
| 52 | |
| 53 | return ( |
| 54 | <> |
| 55 | <SimpleSelect |
| 56 | wrapperStyles={ { marginBottom: '8px' } } |
| 57 | label={ label } |
| 58 | placeholder={ placeholder || __( 'Select taxonomy', 'generateblocks' ) } |
| 59 | options={ taxonomiesOptions } |
| 60 | value={ taxonomy } |
| 61 | onChange={ ( option ) => { |
| 62 | setTaxonomy( option.value ); |
| 63 | setTerms( [] ); |
| 64 | } } |
| 65 | /> |
| 66 | |
| 67 | { taxonomy && |
| 68 | <> |
| 69 | <TaxonomiesSelect |
| 70 | taxonomy={ taxonomy } |
| 71 | value={ terms } |
| 72 | filterName={ 'generateblocks.editor.taxonomy-parameter-control.' + props.id } |
| 73 | placeholder={ placeholder || __( 'Search terms…', 'generateblocks' ) } |
| 74 | onChange={ ( newValue ) => { |
| 75 | const newTerms = newValue.reduce( ( result, option ) => { |
| 76 | result.push( option.value ); |
| 77 | |
| 78 | return result; |
| 79 | }, [] ); |
| 80 | |
| 81 | setTerms( newTerms ); |
| 82 | } } |
| 83 | help={ terms.length === 0 ? __( 'You must select at least one term.', 'generateblocks' ) : '' } |
| 84 | /> |
| 85 | |
| 86 | { isHierarchical && |
| 87 | <ToggleControl |
| 88 | checked={ includeChildren } |
| 89 | label={ __( 'Include child terms', 'generateblocks' ) } |
| 90 | onChange={ setIncludeChildren } |
| 91 | /> |
| 92 | } |
| 93 | </> |
| 94 | } |
| 95 | </> |
| 96 | ); |
| 97 | } |
| 98 |