-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathchild-items-toggle.js
More file actions
54 lines (49 loc) · 1.47 KB
/
child-items-toggle.js
File metadata and controls
54 lines (49 loc) · 1.47 KB
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
/**
* WordPress dependencies
*/
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
export const ChildItemsToggle = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const { query: { post_parent: postParent } = {} } = attributes;
const { isHierarchial, postTypeName, postID } = useSelect( ( select ) => {
const post = select( editorStore ).getCurrentPost();
const postType = select( editorStore ).getCurrentPostType();
const postTypeObject = select( coreStore ).getPostType( postType );
return {
isHierarchial: postTypeObject?.hierarchical,
postTypeName: postType,
postID: post?.id,
};
}, [] );
// If the control is not allowed, return null.
if ( ! allowedControls.includes( 'child_items_only' ) ) {
return null;
}
return (
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show child items only', 'advanced-query-loop' ) }
help={ __(
'Only show child items of this item. This option is only available for hierarchical post types such as pages.',
'advanced-query-loop'
) }
disabled={ ! isHierarchial && postTypeName !== 'wp_template' }
checked={ !! postParent }
onChange={ ( value ) =>
setAttributes( {
query: {
...attributes.query,
post_parent: value ? postID : 0,
},
} )
}
/>
);
};