This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
block.tsx
74 lines (67 loc) · 1.78 KB
/
block.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { isEmpty } from 'lodash';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { HTMLAttributes } from 'react';
/**
* Internal dependencies
*/
import './style.scss';
import { Attributes } from './types';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';
type Props = Attributes & HTMLAttributes< HTMLDivElement >;
/**
* Product Category Block Component.
*
* @param {Object} props Incoming props.
* @param {string} [props.className] CSS Class name for the component.
* @return {*} The component.
*/
const Block = ( props: Props ): JSX.Element | null => {
const { className } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
const typographyProps = useTypographyProps( props );
if ( isEmpty( product.categories ) ) {
return null;
}
return (
<div
className={ classnames(
className,
'wc-block-components-product-category-list',
colorProps.className,
{
[ `${ parentClassName }__product-category-list` ]:
parentClassName,
}
) }
style={ { ...colorProps.style, ...typographyProps.style } }
>
{ __( 'Categories:', 'woo-gutenberg-products-block' ) }{ ' ' }
<ul>
{ Object.values( product.categories ).map(
( { name, link, slug } ) => {
return (
<li key={ `category-list-item-${ slug }` }>
<a href={ link }>{ name }</a>
</li>
);
}
) }
</ul>
</div>
);
};
export default withProductDataContext( Block );