Convert product-elements/summary to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7566)

* Convert product-elements/summary to TypeScript

* bot: update checkstyle.xml

* Update assets/js/atomic/blocks/product-elements/summary/index.ts

Co-authored-by: Tung Du <dinhtungdu@gmail.com>

* Resolve introduced TS error

* bot: update checkstyle.xml

* Remove default subproperties

* Add TODO to refactor this part in the future

* Make attribute type more strict

* Add more context to the todo regarding removing the HOC

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tung Du <dinhtungdu@gmail.com>
This commit is contained in:
Niels Lange 2022-11-07 16:50:15 +07:00 committed by GitHub
parent 2a3a39bdcd
commit 7b84be1156
10 changed files with 63 additions and 77 deletions

View File

@ -1,8 +0,0 @@
export const blockAttributes = {
productId: {
type: 'number',
default: 0,
},
};
export default blockAttributes;

View File

@ -0,0 +1,15 @@
type BlockAttributes = {
productId: {
type: string;
default: number;
};
};
export const blockAttributes: BlockAttributes = {
productId: {
type: 'number',
default: 0,
},
};
export default blockAttributes;

View File

@ -1,7 +1,6 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Summary from '@woocommerce/base-components/summary';
import { blocksConfig } from '@woocommerce/block-settings';
@ -12,22 +11,18 @@ import {
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import type { HTMLAttributes } from 'react';
/**
* Internal dependencies
*/
import './style.scss';
import type { BlockAttributes } from './types';
/**
* Product Summary Block Component.
*
* @param {Object} props Incoming props.
* @param {string} [props.className] CSS Class name for the component.
* @return {*} The component.
*/
const Block = ( props ) => {
type Props = BlockAttributes & HTMLAttributes< HTMLDivElement >;
const Block = ( props: Props ): JSX.Element | null => {
const { className } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
@ -78,8 +73,4 @@ const Block = ( props ) => {
);
};
Block.propTypes = {
className: PropTypes.string,
};
export default withProductDataContext( Block );

View File

@ -4,14 +4,14 @@
import { __ } from '@wordpress/i18n';
import { page, Icon } from '@wordpress/icons';
export const BLOCK_TITLE = __(
export const BLOCK_TITLE: string = __(
'Product Summary',
'woo-gutenberg-products-block'
);
export const BLOCK_ICON = (
export const BLOCK_ICON: JSX.Element = (
<Icon icon={ page } className="wc-block-editor-components-block-icon" />
);
export const BLOCK_DESCRIPTION = __(
export const BLOCK_DESCRIPTION: string = __(
'Display a short description about a product.',
'woo-gutenberg-products-block'
);

View File

@ -1,31 +0,0 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import Block from './block';
import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
import './editor.scss';
const Edit = ( { attributes } ) => {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Block { ...attributes } />
</div>
);
};
export default withProductSelector( {
icon: BLOCK_ICON,
label: BLOCK_TITLE,
description: __(
'Choose a product to display its short description.',
'woo-gutenberg-products-block'
),
} )( Edit );

View File

@ -0,0 +1,33 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import Block from './block';
import withProductSelector from '../shared/with-product-selector';
import {
BLOCK_TITLE as label,
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import './editor.scss';
import type { BlockAttributes } from './types';
interface Props {
attributes: BlockAttributes;
}
const Edit = ( { attributes }: Props ): JSX.Element => {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Block { ...attributes } />
</div>
);
};
// @todo: Refactor this to remove the HOC 'withProductSelector()' component as users will not see this block in the inserter. Therefore, we can export the Edit component by default. The HOC 'withProductSelector()' component should also be removed from other `product-elements` components. See also https://github.com/woocommerce/woocommerce-blocks/pull/7566#pullrequestreview-1168635469.
export default withProductSelector( { icon, label, description } )( Edit );

View File

@ -2,6 +2,7 @@
* External dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import type { BlockConfiguration } from '@wordpress/blocks';
/**
* Internal dependencies
@ -17,7 +18,8 @@ import {
} from './constants';
import { Save } from './save';
const blockConfig = {
const blockConfig: BlockConfiguration = {
...sharedConfig,
apiVersion: 2,
title,
description,
@ -28,7 +30,4 @@ const blockConfig = {
save: Save,
};
registerBlockType( 'woocommerce/product-summary', {
...sharedConfig,
...blockConfig,
} );
registerBlockType( 'woocommerce/product-summary', blockConfig );

View File

@ -6,9 +6,7 @@ import { isFeaturePluginBuild } from '@woocommerce/block-settings';
export const supports = {
...( isFeaturePluginBuild() && {
color: {
text: true,
background: false,
link: false,
},
typography: {
fontSize: true,

View File

@ -0,0 +1,3 @@
export interface BlockAttributes {
productId: number;
}

View File

@ -1539,20 +1539,6 @@
Types of parameters &apos;__0&apos; and &apos;props&apos; are incompatible.
Property &apos;context&apos; is missing in type &apos;BlockEditProps&lt;{}&gt; &amp; { children?: ReactNode; }&apos; but required in type &apos;{ attributes: any; setAttributes: any; context: any; }&apos;." source="TS2769" />
</file>
<file name="assets/js/atomic/blocks/product-elements/summary/edit.js">
<error line="15" column="18" severity="error" message="Binding element &apos;attributes&apos; implicitly has an &apos;any&apos; type." source="TS7031" />
</file>
<file name="assets/js/atomic/blocks/product-elements/summary/index.js">
<error line="31" column="1" severity="error" message="No overload matches this call.
Overload 1 of 2, &apos;(metadata: BlockConfiguration&lt;{}&gt;, settings?: Partial&lt;BlockConfiguration&lt;{}&gt;&gt; | undefined): Block&lt;{}&gt; | undefined&apos;, gave the following error.
Argument of type &apos;string&apos; is not assignable to parameter of type &apos;BlockConfiguration&lt;{}&gt;&apos;.
Type &apos;string&apos; is not assignable to type &apos;Pick&lt;Block&lt;{}&gt;, &quot;title&quot; | &quot;category&quot; | &quot;attributes&quot;&gt;&apos;.
Overload 2 of 2, &apos;(name: string, settings: BlockConfiguration&lt;{}&gt;): Block&lt;{}&gt; | undefined&apos;, gave the following error.
Argument of type &apos;{ apiVersion: number; title: string; description: string; icon: { src: JSX.Element; }; attributes: { productId: { type: string; default: number; }; }; supports: { color?: { text: boolean; background: boolean; link: boolean; }; typography?: { ...; }; __experimentalSelector?: string; }; ... 19 more ...; merge?: ((attr...&apos; is not assignable to parameter of type &apos;BlockConfiguration&lt;{}&gt;&apos; with &apos;exactOptionalPropertyTypes: true&apos;. Consider adding &apos;undefined&apos; to the types of the target&apos;s properties.
Type &apos;{ apiVersion: number; title: string; description: string; icon: { src: JSX.Element; }; attributes: { productId: { type: string; default: number; }; }; supports: { color?: { text: boolean; background: boolean; link: boolean; }; typography?: { ...; }; __experimentalSelector?: string; }; ... 19 more ...; merge?: ((attr...&apos; is not assignable to type &apos;Partial&lt;Omit&lt;Block&lt;{}&gt;, &quot;icon&quot;&gt;&gt;&apos; with &apos;exactOptionalPropertyTypes: true&apos;. Consider adding &apos;undefined&apos; to the types of the target&apos;s properties.
The types of &apos;supports.color.text&apos; are incompatible between these types.
Type &apos;boolean&apos; is not assignable to type &apos;string&apos;." source="TS2769" />
</file>
<file name="assets/js/editor-components/edit-product-link/index.js">
<error line="18" column="40" severity="error" message="Property &apos;productId&apos; does not exist on type &apos;Object&apos;." source="TS2339" />
</file>