2020-03-02 13:46:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
2020-05-12 20:31:49 +00:00
|
|
|
import { InspectorControls, PlainText } from '@wordpress/block-editor';
|
2020-08-18 11:25:48 +00:00
|
|
|
import { PanelBody, ToggleControl, TextControl } from '@wordpress/components';
|
2020-03-02 13:46:56 +00:00
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2020-12-21 15:56:58 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2020-03-02 13:46:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './editor.scss';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component displaying a product search form.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {Object} props.attributes Incoming block attributes.
|
|
|
|
* @param {string} props.attributes.label
|
|
|
|
* @param {string} props.attributes.placeholder
|
|
|
|
* @param {string} props.attributes.formId
|
|
|
|
* @param {string} props.attributes.className
|
|
|
|
* @param {boolean} props.attributes.hasLabel
|
|
|
|
* @param {string} props.attributes.align
|
|
|
|
* @param {string} props.instanceId
|
|
|
|
* @param {function(any):any} props.setAttributes Setter for block attributes.
|
2020-03-02 13:46:56 +00:00
|
|
|
*/
|
2023-05-24 12:24:08 +00:00
|
|
|
interface EditProps {
|
|
|
|
attributes: {
|
|
|
|
label: string;
|
|
|
|
placeholder: string;
|
|
|
|
formId: string;
|
|
|
|
className: string;
|
|
|
|
hasLabel: boolean;
|
|
|
|
align: string;
|
|
|
|
};
|
|
|
|
instanceId: number;
|
|
|
|
setAttributes: ( attributes: {
|
|
|
|
label?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
formId?: string;
|
|
|
|
className?: string;
|
|
|
|
hasLabel?: boolean;
|
|
|
|
align?: string;
|
|
|
|
} ) => void;
|
|
|
|
}
|
2020-03-02 13:46:56 +00:00
|
|
|
const Edit = ( {
|
|
|
|
attributes: { label, placeholder, formId, className, hasLabel, align },
|
|
|
|
instanceId,
|
|
|
|
setAttributes,
|
2023-05-24 12:24:08 +00:00
|
|
|
}: EditProps ) => {
|
2020-03-02 13:46:56 +00:00
|
|
|
const classes = classnames(
|
|
|
|
'wc-block-product-search',
|
|
|
|
align ? 'align' + align : '',
|
|
|
|
className
|
|
|
|
);
|
|
|
|
|
2020-12-21 15:56:58 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( ! formId ) {
|
|
|
|
setAttributes( {
|
|
|
|
formId: `wc-block-product-search-${ instanceId }`,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}, [ formId, setAttributes, instanceId ] );
|
2020-03-02 13:46:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Content', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show search field label',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ hasLabel }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( { hasLabel: ! hasLabel } )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
<div className={ classes }>
|
|
|
|
{ !! hasLabel && (
|
2021-10-14 10:27:05 +00:00
|
|
|
<>
|
|
|
|
<label
|
|
|
|
className="screen-reader-text"
|
|
|
|
htmlFor="wc-block-product-search__label"
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Search Label',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</label>
|
|
|
|
<PlainText
|
|
|
|
className="wc-block-product-search__label"
|
|
|
|
id="wc-block-product-search__label"
|
|
|
|
value={ label }
|
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( { label: value } )
|
|
|
|
}
|
2023-02-07 15:47:13 +00:00
|
|
|
style={ { backgroundColor: 'transparent' } }
|
2021-10-14 10:27:05 +00:00
|
|
|
/>
|
|
|
|
</>
|
2020-03-02 13:46:56 +00:00
|
|
|
) }
|
|
|
|
<div className="wc-block-product-search__fields">
|
2020-08-18 11:25:48 +00:00
|
|
|
<TextControl
|
2020-03-02 13:46:56 +00:00
|
|
|
className="wc-block-product-search__field input-control"
|
|
|
|
value={ placeholder }
|
2021-11-12 14:58:30 +00:00
|
|
|
placeholder={ __(
|
|
|
|
'Enter search placeholder text',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-02 13:46:56 +00:00
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( { placeholder: value } )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="wc-block-product-search__button"
|
2023-05-24 12:24:08 +00:00
|
|
|
aria-label={ __(
|
|
|
|
'Search',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-02 13:46:56 +00:00
|
|
|
onClick={ ( e ) => e.preventDefault() }
|
2023-05-24 12:24:08 +00:00
|
|
|
tabIndex={ -1 }
|
2020-03-02 13:46:56 +00:00
|
|
|
>
|
|
|
|
<svg
|
|
|
|
aria-hidden="true"
|
|
|
|
role="img"
|
|
|
|
focusable="false"
|
|
|
|
className="dashicon dashicons-arrow-right-alt2"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
width="20"
|
|
|
|
height="20"
|
|
|
|
viewBox="0 0 20 20"
|
|
|
|
>
|
|
|
|
<path d="M6 15l5-5-5-5 1-2 7 7-7 7z" />
|
|
|
|
</svg>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withInstanceId( Edit );
|