-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (65 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { SelectControl, Spinner } from '@wordpress/components';
import { useEntityRecords } from '@wordpress/core-data';
/**
* Term picker from wanted taxonomy.
*
* @param {Object} props Props for component.
*/
function TermSelect( props ) {
const {
attributes: { termId },
taxonomyName,
queryArgs = { per_page: 99 },
queryOptions = { enabled: true },
setAttributes,
useMultiple = false,
} = props;
const options = [];
// See: https://make.wordpress.org/core/2022/10/11/simplified-data-access-with-new-react-hooks-in-wordpress-6-1/
// See: https://developer.wordpress.org/block-editor/reference-guides/packages/packages-core-data/#useentityrecords
// See: https://github.com/WordPress/gutenberg/tree/1f47e41d4ce5987f9acf07fab58a7c39ea919c05/packages/core-data/src/hooks
const { records, isResolving } = useEntityRecords(
'taxonomy',
taxonomyName,
queryArgs,
queryOptions
);
options.push( {
value: 0,
label: __( 'From all categories', 'meom-block-components' ),
} );
if ( ! isResolving && records && records.length > 0 ) {
records.forEach( ( term ) => {
options.push( {
value: term.id,
label: term.name,
} );
} );
}
return (
<>
{ isResolving && <Spinner /> }
{ ! isResolving && (
<>
<SelectControl
label={ __(
'Choose category',
'meom-block-components'
) }
options={ options }
onChange={ ( newTermId ) => {
setAttributes( { termId: newTermId } );
} }
value={ termId }
{ ...( useMultiple ? { multiple: true } : {} ) }
/>
</>
) }
</>
);
}
export default TermSelect;