From eb1c1c5201d2ec007e5c8466e39230fc32bdac86 Mon Sep 17 00:00:00 2001 From: Wojtek Naruniec Date: Fri, 12 Aug 2022 17:46:00 +0200 Subject: [PATCH] Refactor Product Categories block to use block.json (https://github.com/woocommerce/woocommerce-blocks/pull/6875) I refactor block to use block.json schema. The block schema internally extends the main block.json schema and it fixes an issue that occurs on WPCOM. --- .../js/blocks/product-categories/block.json | 41 ++++++++++ .../js/blocks/product-categories/edit.tsx | 20 +++++ .../{index.js => index.tsx} | 79 ++----------------- 3 files changed, 66 insertions(+), 74 deletions(-) create mode 100644 plugins/woocommerce-blocks/assets/js/blocks/product-categories/block.json create mode 100644 plugins/woocommerce-blocks/assets/js/blocks/product-categories/edit.tsx rename plugins/woocommerce-blocks/assets/js/blocks/product-categories/{index.js => index.tsx} (68%) diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/block.json b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/block.json new file mode 100644 index 00000000000..215429cd908 --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/block.json @@ -0,0 +1,41 @@ +{ + "name": "woocommerce/product-categories", + "title": "Product Categories List", + "category": "woocommerce", + "description": "Show all product categories as a list or dropdown.", + "keywords": [ "WooCommerce" ], + "attributes": { + "align": { + "type": "string" + }, + "hasCount": { + "type": "boolean", + "default": true + }, + "hasImage": { + "type": "boolean", + "default": false + }, + "hasEmpty": { + "type": "boolean", + "default": false + }, + "isDropdown": { + "type": "boolean", + "default": false + }, + "isHierarchical": { + "type": "boolean", + "default": true + } + }, + "example": { + "attributes": { + "hasCount": true, + "hasImage": false + } + }, + "textdomain": "woo-gutenberg-products-block", + "apiVersion": 2, + "$schema": "https://schemas.wp.org/trunk/block.json" +} diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/edit.tsx b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/edit.tsx new file mode 100644 index 00000000000..d8fe566c42f --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/edit.tsx @@ -0,0 +1,20 @@ +/** + * External dependencies + */ +import { useBlockProps } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import Block from './block'; +import './editor.scss'; + +export const Edit = ( props: unknown ): JSX.Element => { + const blockProps = useBlockProps(); + + return ( +
+ +
+ ); +}; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.tsx similarity index 68% rename from plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js rename to plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.tsx index 9715d879d34..4cbf55dabc9 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.tsx @@ -1,21 +1,19 @@ /** * External dependencies */ -import { __ } from '@wordpress/i18n'; import { createBlock, registerBlockType } from '@wordpress/blocks'; import { Icon, listView } from '@wordpress/icons'; - import { isFeaturePluginBuild } from '@woocommerce/block-settings'; + /** * Internal dependencies */ import './editor.scss'; +import metadata from './block.json'; import './style.scss'; -import Block from './block.js'; +import { Edit } from './edit'; -registerBlockType( 'woocommerce/product-categories', { - apiVersion: 2, - title: __( 'Product Categories List', 'woo-gutenberg-products-block' ), +registerBlockType( metadata, { icon: { src: ( ), }, - category: 'woocommerce', - keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ], - description: __( - 'Show all product categories as a list or dropdown.', - 'woo-gutenberg-products-block' - ), supports: { align: [ 'wide', 'full' ], html: false, @@ -44,60 +36,6 @@ registerBlockType( 'woocommerce/product-categories', { }, } ), }, - example: { - attributes: { - hasCount: true, - hasImage: false, - }, - }, - attributes: { - /** - * Alignment of the block. - */ - align: { - type: 'string', - }, - - /** - * Whether to show the product count in each category. - */ - hasCount: { - type: 'boolean', - default: true, - }, - - /** - * Whether to show the category image in each category. - */ - hasImage: { - type: 'boolean', - default: false, - }, - - /** - * Whether to show empty categories in the list. - */ - hasEmpty: { - type: 'boolean', - default: false, - }, - - /** - * Whether to display product categories as a dropdown (true) or list (false). - */ - isDropdown: { - type: 'boolean', - default: false, - }, - - /** - * Whether the product categories should display with hierarchy. - */ - isHierarchical: { - type: 'boolean', - default: true, - }, - }, transforms: { from: [ @@ -197,14 +135,7 @@ registerBlockType( 'woocommerce/product-categories', { }, ], - /** - * Renders and manages the block. - * - * @param {Object} props Props to pass to block. - */ - edit( props ) { - return ; - }, + edit: Edit, /** * Save nothing; rendered by server.