/* eslint-disable @typescript-eslint/ban-ts-comment */
/**
* External dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { store as blockEditorStore, Warning } from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { Icon, search } from '@wordpress/icons';
import { getSettingWithCoercion } from '@woocommerce/settings';
import { isBoolean } from '@woocommerce/types';
import { Button } from '@wordpress/components';
import type { Block as BlockType } from '@wordpress/blocks';
import {
// @ts-ignore waiting for @types/wordpress__blocks update
registerBlockVariation,
registerBlockType,
createBlock,
} from '@wordpress/blocks';
/**
* Internal dependencies
*/
import './style.scss';
import './editor.scss';
import { withProductSearchControls } from './inspector-controls';
import Block from './block';
import Edit from './edit';
import { SEARCH_BLOCK_NAME, SEARCH_VARIATION_NAME } from './constants';
const isBlockVariationAvailable = getSettingWithCoercion(
'isBlockVariationAvailable',
false,
isBoolean
);
const attributes = {
/**
* Whether to show the field label.
*/
hasLabel: {
type: 'boolean',
default: true,
},
/**
* Search field label.
*/
label: {
type: 'string',
default: __( 'Search', 'woocommerce' ),
},
/**
* Search field placeholder.
*/
placeholder: {
type: 'string',
default: __( 'Search products…', 'woocommerce' ),
},
/**
* Store the instance ID.
*/
formId: {
type: 'string',
default: '',
},
};
const PRODUCT_SEARCH_ATTRIBUTES = {
label: attributes.label.default,
buttonText: attributes.label.default,
placeholder: attributes.placeholder.default,
query: {
post_type: 'product',
},
namespace: SEARCH_VARIATION_NAME,
};
const DeprecatedBlockEdit = ( { clientId }: { clientId: string } ) => {
// @ts-ignore @wordpress/block-editor/store types not provided
const { replaceBlocks } = useDispatch( blockEditorStore );
const currentBlockAttributes = useSelect(
( select ) =>
select( 'core/block-editor' ).getBlockAttributes( clientId ),
[ clientId ]
);
const updateBlock = () => {
replaceBlocks(
clientId,
createBlock( 'core/search', {
label:
currentBlockAttributes?.label ||
PRODUCT_SEARCH_ATTRIBUTES.label,
buttonText: PRODUCT_SEARCH_ATTRIBUTES.buttonText,
placeholder:
currentBlockAttributes?.placeholder ||
PRODUCT_SEARCH_ATTRIBUTES.placeholder,
query: PRODUCT_SEARCH_ATTRIBUTES.query,
} )
);
};
const actions = [
,
];
return (