PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.2
GenerateBlocks v1.5.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
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 4 years ago TaxonomyParameterControl.js 4 years ago
TaxonomyParameterControl.js
79 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
6 export default function TaxonomyParameterControl( { label, value, onChange } ) {
7 const [ taxonomy, setTaxonomy ] = useState();
8 const [ terms, setTerms ] = useState( [] );
9
10 const taxonomies = useTaxonomies();
11
12 useEffect( () => {
13 if ( !! value.taxonomy ) {
14 setTaxonomy( value.taxonomy );
15 }
16
17 if ( !! value.terms ) {
18 setTerms( value.terms );
19 }
20 }, [] );
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
37 onChange( { taxonomy, terms, rest } );
38 }
39 }, [ taxonomy, terms ] );
40
41 const taxonomiesOptions = useMemo( () => (
42 taxonomies
43 .filter( ( tax ) => ( 'nav_menu' !== tax.slug ) )
44 .map( ( tax ) => ( { value: tax.slug, label: tax.name } ) )
45 ), [ taxonomies ] );
46
47 const labelStyles = { marginBottom: '8px', display: 'inline-block' };
48
49 return (
50 <>
51 { label && <label htmlFor={ 'tax-label' } style={ labelStyles }>{ label }</label> }
52
53 <SimpleSelect
54 wrapperStyles={ { marginBottom: '8px' } }
55 options={ taxonomiesOptions }
56 value={ taxonomy }
57 onChange={ ( option ) => {
58 setTaxonomy( option.value );
59 setTerms( [] );
60 } }
61 />
62
63 <TaxonomiesSelect
64 taxonomy={ taxonomy }
65 value={ terms }
66 onChange={ ( newValue ) => {
67 const newTerms = newValue.reduce( ( result, option ) => {
68 result.push( option.value );
69
70 return result;
71 }, [] );
72
73 setTerms( newTerms );
74 } }
75 />
76 </>
77 );
78 }
79