Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 2eb3b63

Browse files
authored
Refactor Product Categories block to use block.json (#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.
1 parent c5bdcff commit 2eb3b63

File tree

3 files changed

+66
-74
lines changed

3 files changed

+66
-74
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "woocommerce/product-categories",
3+
"title": "Product Categories List",
4+
"category": "woocommerce",
5+
"description": "Show all product categories as a list or dropdown.",
6+
"keywords": [ "WooCommerce" ],
7+
"attributes": {
8+
"align": {
9+
"type": "string"
10+
},
11+
"hasCount": {
12+
"type": "boolean",
13+
"default": true
14+
},
15+
"hasImage": {
16+
"type": "boolean",
17+
"default": false
18+
},
19+
"hasEmpty": {
20+
"type": "boolean",
21+
"default": false
22+
},
23+
"isDropdown": {
24+
"type": "boolean",
25+
"default": false
26+
},
27+
"isHierarchical": {
28+
"type": "boolean",
29+
"default": true
30+
}
31+
},
32+
"example": {
33+
"attributes": {
34+
"hasCount": true,
35+
"hasImage": false
36+
}
37+
},
38+
"textdomain": "woo-gutenberg-products-block",
39+
"apiVersion": 2,
40+
"$schema": "https://schemas.wp.org/trunk/block.json"
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { useBlockProps } from '@wordpress/block-editor';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import Block from './block';
10+
import './editor.scss';
11+
12+
export const Edit = ( props: unknown ): JSX.Element => {
13+
const blockProps = useBlockProps();
14+
15+
return (
16+
<div { ...blockProps }>
17+
<Block { ...props } />
18+
</div>
19+
);
20+
};

assets/js/blocks/product-categories/index.js renamed to assets/js/blocks/product-categories/index.tsx

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/**
22
* External dependencies
33
*/
4-
import { __ } from '@wordpress/i18n';
54
import { createBlock, registerBlockType } from '@wordpress/blocks';
65
import { Icon, listView } from '@wordpress/icons';
7-
86
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
7+
98
/**
109
* Internal dependencies
1110
*/
1211
import './editor.scss';
12+
import metadata from './block.json';
1313
import './style.scss';
14-
import Block from './block.js';
14+
import { Edit } from './edit';
1515

16-
registerBlockType( 'woocommerce/product-categories', {
17-
apiVersion: 2,
18-
title: __( 'Product Categories List', 'woo-gutenberg-products-block' ),
16+
registerBlockType( metadata, {
1917
icon: {
2018
src: (
2119
<Icon
@@ -24,12 +22,6 @@ registerBlockType( 'woocommerce/product-categories', {
2422
/>
2523
),
2624
},
27-
category: 'woocommerce',
28-
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
29-
description: __(
30-
'Show all product categories as a list or dropdown.',
31-
'woo-gutenberg-products-block'
32-
),
3325
supports: {
3426
align: [ 'wide', 'full' ],
3527
html: false,
@@ -44,60 +36,6 @@ registerBlockType( 'woocommerce/product-categories', {
4436
},
4537
} ),
4638
},
47-
example: {
48-
attributes: {
49-
hasCount: true,
50-
hasImage: false,
51-
},
52-
},
53-
attributes: {
54-
/**
55-
* Alignment of the block.
56-
*/
57-
align: {
58-
type: 'string',
59-
},
60-
61-
/**
62-
* Whether to show the product count in each category.
63-
*/
64-
hasCount: {
65-
type: 'boolean',
66-
default: true,
67-
},
68-
69-
/**
70-
* Whether to show the category image in each category.
71-
*/
72-
hasImage: {
73-
type: 'boolean',
74-
default: false,
75-
},
76-
77-
/**
78-
* Whether to show empty categories in the list.
79-
*/
80-
hasEmpty: {
81-
type: 'boolean',
82-
default: false,
83-
},
84-
85-
/**
86-
* Whether to display product categories as a dropdown (true) or list (false).
87-
*/
88-
isDropdown: {
89-
type: 'boolean',
90-
default: false,
91-
},
92-
93-
/**
94-
* Whether the product categories should display with hierarchy.
95-
*/
96-
isHierarchical: {
97-
type: 'boolean',
98-
default: true,
99-
},
100-
},
10139

10240
transforms: {
10341
from: [
@@ -197,14 +135,7 @@ registerBlockType( 'woocommerce/product-categories', {
197135
},
198136
],
199137

200-
/**
201-
* Renders and manages the block.
202-
*
203-
* @param {Object} props Props to pass to block.
204-
*/
205-
edit( props ) {
206-
return <Block { ...props } />;
207-
},
138+
edit: Edit,
208139

209140
/**
210141
* Save nothing; rendered by server.

0 commit comments

Comments
 (0)