This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathinspector-controls.tsx
61 lines (56 loc) · 1.63 KB
/
inspector-controls.tsx
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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { addFilter } from '@wordpress/hooks';
import { EditorBlock } from '@woocommerce/types';
import { ElementType } from 'react';
/**
* Internal dependencies
*/
import { ProductQueryBlock } from './types';
import {
isWooQueryBlockVariation,
setCustomQueryAttribute,
useAllowedControls,
} from './utils';
export const INSPECTOR_CONTROLS = {
onSale: ( props: ProductQueryBlock ) => (
<ToggleControl
label={ __(
'Show only products on sale',
'woo-gutenberg-products-block'
) }
checked={ props.attributes.query.__woocommerceOnSale || false }
onChange={ ( __woocommerceOnSale ) => {
setCustomQueryAttribute( props, { __woocommerceOnSale } );
} }
/>
),
};
export const withProductQueryControls =
< T extends EditorBlock< T > >( BlockEdit: ElementType ) =>
( props: ProductQueryBlock ) => {
const allowedControls = useAllowedControls( props.attributes );
const availableControls = Object.entries( INSPECTOR_CONTROLS ).filter(
( [ key ] ) => allowedControls?.includes( key )
);
return isWooQueryBlockVariation( props ) &&
availableControls.length > 0 ? (
<>
<BlockEdit { ...props } />
<InspectorControls>
<PanelBody>
{ availableControls.map( ( [ key, Control ] ) => (
<Control key={ key } { ...props } />
) ) }
</PanelBody>
</InspectorControls>
</>
) : (
<BlockEdit { ...props } />
);
};
addFilter( 'editor.BlockEdit', 'core/query', withProductQueryControls );