2019-08-05 10:25:57 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { registerBlockType } from '@wordpress/blocks';
|
|
|
|
import { InspectorControls } from '@wordpress/editor';
|
|
|
|
import { PanelBody, ToggleControl } from '@wordpress/components';
|
|
|
|
import { Fragment } from '@wordpress/element';
|
2020-01-31 18:20:33 +00:00
|
|
|
import { Icon, search } from '@woocommerce/icons';
|
2019-08-05 10:25:57 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
import './editor.scss';
|
|
|
|
import Block from './block.js';
|
|
|
|
|
|
|
|
registerBlockType( 'woocommerce/product-search', {
|
|
|
|
title: __( 'Product Search', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: {
|
2020-01-31 18:20:33 +00:00
|
|
|
src: <Icon srcElement={ search } />,
|
2019-08-05 10:25:57 +00:00
|
|
|
foreground: '#96588a',
|
|
|
|
},
|
|
|
|
category: 'woocommerce',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
2020-02-17 22:02:59 +00:00
|
|
|
'A search box to allow customers to search for products by keyword.',
|
2019-08-05 10:25:57 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
supports: {
|
|
|
|
align: [ 'wide', 'full' ],
|
|
|
|
},
|
2019-11-05 10:32:53 +00:00
|
|
|
example: {
|
|
|
|
attributes: {
|
|
|
|
hasLabel: true,
|
|
|
|
},
|
|
|
|
},
|
2019-08-05 10:25:57 +00:00
|
|
|
attributes: {
|
|
|
|
/**
|
|
|
|
* Whether to show the field label.
|
|
|
|
*/
|
|
|
|
hasLabel: {
|
|
|
|
type: 'boolean',
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search field label.
|
|
|
|
*/
|
|
|
|
label: {
|
|
|
|
type: 'string',
|
|
|
|
default: __( 'Search', 'woo-gutenberg-products-block' ),
|
|
|
|
source: 'text',
|
|
|
|
selector: 'label',
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search field placeholder.
|
|
|
|
*/
|
|
|
|
placeholder: {
|
|
|
|
type: 'string',
|
2020-01-30 09:28:17 +00:00
|
|
|
default: __( 'Search products…', 'woo-gutenberg-products-block' ),
|
2019-08-05 10:25:57 +00:00
|
|
|
source: 'attribute',
|
|
|
|
selector: 'input.wc-block-product-search__field',
|
|
|
|
attribute: 'placeholder',
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the instance ID.
|
|
|
|
*/
|
|
|
|
formId: {
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props Props to pass to block.
|
2019-08-05 10:25:57 +00:00
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
const { attributes, setAttributes } = props;
|
|
|
|
const { hasLabel } = attributes;
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
2019-09-09 10:52:48 +00:00
|
|
|
title={ __(
|
|
|
|
'Content',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-08-05 10:25:57 +00:00
|
|
|
initialOpen
|
|
|
|
>
|
|
|
|
<ToggleControl
|
2019-09-05 15:09:31 +00:00
|
|
|
label={ __(
|
|
|
|
'Show search field label',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-08-05 10:25:57 +00:00
|
|
|
help={
|
2019-09-05 15:09:31 +00:00
|
|
|
hasLabel
|
2019-09-09 10:52:48 +00:00
|
|
|
? __(
|
|
|
|
'Label is visible.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
: __(
|
|
|
|
'Label is hidden.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
2019-08-05 10:25:57 +00:00
|
|
|
}
|
|
|
|
checked={ hasLabel }
|
2019-09-09 10:52:48 +00:00
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( { hasLabel: ! hasLabel } )
|
|
|
|
}
|
2019-08-05 10:25:57 +00:00
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
2019-12-02 10:27:44 +00:00
|
|
|
<Block { ...props } isEditor={ true } />
|
2019-08-05 10:25:57 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the props to post content.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
|
|
|
* @param {Object} attributes Props to pass to block.
|
2019-08-05 10:25:57 +00:00
|
|
|
*/
|
|
|
|
save( attributes ) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Block { ...attributes } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|