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

Commit

Permalink
Add custom className support to Checkout Inner Blocks (#4978)
Browse files Browse the repository at this point in the history
* Pass classname from DOM to react components

* Actions Block ClassName support

* Form step field classname support

* Payment block className support

* Classname support for fields and totals

* Order summary classname support

* Classname support for order note block

* Express payment classname support

* Terms block custom class

* Let useBlockProps handle class in actions block
  • Loading branch information
mikejolley authored Oct 20, 2021
1 parent 2204176 commit d761be1
Show file tree
Hide file tree
Showing 35 changed files with 237 additions and 66 deletions.
1 change: 1 addition & 0 deletions assets/js/atomic/utils/render-parent-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const renderInnerBlocks = ( {
const { blockName = '', ...componentProps } = {
key: `${ block }_${ depth }_${ index }`,
...( element instanceof HTMLElement ? element.dataset : {} ),
className: element.className || '',
};

const InnerBlockComponent = getBlockComponentFromMap(
Expand Down
15 changes: 7 additions & 8 deletions assets/js/base/components/sidebar-layout/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';

const Main = ( { children, className } ) => {
const Main = forwardRef( ( { children, className = '' }, ref ) => {
return (
<div className={ classNames( 'wc-block-components-main', className ) }>
<div
ref={ ref }
className={ classNames( 'wc-block-components-main', className ) }
>
{ children }
</div>
);
};

Main.propTypes = {
className: PropTypes.string,
};
} );

export default Main;
11 changes: 4 additions & 7 deletions assets/js/base/components/sidebar-layout/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';

const Sidebar = ( { children, className } ) => {
const Sidebar = forwardRef( ( { children, className = '' }, ref ) => {
return (
<div
ref={ ref }
className={ classNames( 'wc-block-components-sidebar', className ) }
>
{ children }
</div>
);
};

Sidebar.propTypes = {
className: PropTypes.string,
};
} );

export default Sidebar;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default {
type: 'boolean',
default: true,
},
className: {
type: 'string',
default: '',
},
lock: {
type: 'object',
default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { getSetting } from '@woocommerce/settings';
import {
PlaceOrderButton,
Expand All @@ -15,12 +16,16 @@ import './style.scss';
const Block = ( {
cartPageId,
showReturnToCart,
className,
}: {
cartPageId: number;
showReturnToCart: boolean;
className?: string;
} ): JSX.Element => {
return (
<div className="wc-block-checkout__actions">
<div
className={ classnames( 'wc-block-checkout__actions', className ) }
>
{ showReturnToCart && (
<ReturnToCartButton
link={ getSetting( 'page-' + cartPageId, false ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default {
'woo-gutenberg-products-block'
),
} ),
className: {
type: 'string',
default: '',
},
lock: {
type: 'object',
default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
import { useCheckoutAddress } from '@woocommerce/base-context/hooks';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
Expand All @@ -27,6 +28,7 @@ export const Edit = ( {
title: string;
description: string;
showStepNumber: boolean;
className: string;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element | null => {
Expand All @@ -50,7 +52,10 @@ export const Edit = ( {
<FormStepBlock
setAttributes={ setAttributes }
attributes={ attributes }
className="wc-block-checkout__billing-fields"
className={ classnames(
'wc-block-checkout__billing-fields',
attributes?.className
) }
>
<Controls />
<Block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import withFilteredAttributes from '@woocommerce/base-hocs/with-filtered-attributes';
import { FormStep } from '@woocommerce/base-components/cart-checkout';
import { useCheckoutContext } from '@woocommerce/base-context';
Expand All @@ -18,11 +19,13 @@ const FrontendBlock = ( {
description,
showStepNumber,
children,
className,
}: {
title: string;
description: string;
showStepNumber: boolean;
children: JSX.Element;
className?: string;
} ): JSX.Element | null => {
const { isProcessing: checkoutIsProcessing } = useCheckoutContext();
const { showBillingFields } = useCheckoutAddress();
Expand All @@ -42,7 +45,10 @@ const FrontendBlock = ( {
<FormStep
id="billing-fields"
disabled={ checkoutIsProcessing }
className="wc-block-checkout__billing-fields"
className={ classnames(
'wc-block-checkout__billing-fields',
className
) }
title={ title }
description={ description }
showStepNumber={ showStepNumber }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default {
'woo-gutenberg-products-block'
),
} ),
className: {
type: 'string',
default: '',
},
lock: {
type: 'object',
default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
import { Disabled } from '@wordpress/components';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
Expand All @@ -27,6 +28,7 @@ export const Edit = ( {
title: string;
description: string;
showStepNumber: boolean;
className: string;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => {
Expand All @@ -36,6 +38,10 @@ export const Edit = ( {
<FormStepBlock
attributes={ attributes }
setAttributes={ setAttributes }
className={ classnames(
'wc-block-checkout__contact-fields',
attributes?.className
) }
>
<Controls />
<Disabled>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import withFilteredAttributes from '@woocommerce/base-hocs/with-filtered-attributes';
import { FormStep } from '@woocommerce/base-components/cart-checkout';
import { useCheckoutContext } from '@woocommerce/base-context';
Expand All @@ -18,12 +19,14 @@ const FrontendBlock = ( {
description,
showStepNumber,
children,
className,
}: {
title: string;
description: string;
allowCreateAccount: boolean;
showStepNumber: boolean;
children: JSX.Element;
className?: string;
} ) => {
const { isProcessing: checkoutIsProcessing } = useCheckoutContext();
const { allowCreateAccount } = useCheckoutBlockContext();
Expand All @@ -32,7 +35,10 @@ const FrontendBlock = ( {
<FormStep
id="contact-fields"
disabled={ checkoutIsProcessing }
className="wc-block-checkout__contact-fields"
className={ classnames(
'wc-block-checkout__contact-fields',
className
) }
title={ title }
description={ description }
showStepNumber={ showStepNumber }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"inserter": false
},
"attributes": {
"className": {
"type": "string",
"default": ""
},
"lock": {
"type": "object",
"default": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import { useStoreCart } from '@woocommerce/base-context/hooks';
*/
import { CheckoutExpressPayment } from '../../../payment-methods';

const Block = (): JSX.Element | null => {
const Block = ( { className }: { className?: string } ): JSX.Element | null => {
const { cartNeedsPayment } = useStoreCart();

if ( ! cartNeedsPayment ) {
return null;
}

return <CheckoutExpressPayment />;
return (
<div className={ className }>
<CheckoutExpressPayment />
</div>
);
};

export default Block;
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const Edit = ( {
attributes,
}: {
attributes: {
className?: string;
lock: {
move: boolean;
remove: boolean;
Expand All @@ -60,9 +61,12 @@ export const Edit = ( {
const { paymentMethods, isInitialized } = useExpressPaymentMethods();
const hasExpressPaymentMethods = Object.keys( paymentMethods ).length > 0;
const blockProps = useBlockProps( {
className: classnames( {
'wp-block-woocommerce-checkout-express-payment-block--has-express-payment-methods': hasExpressPaymentMethods,
} ),
className: classnames(
{
'wp-block-woocommerce-checkout-express-payment-block--has-express-payment-methods': hasExpressPaymentMethods,
},
attributes?.className
),
attributes,
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"inserter": false
},
"attributes": {
"className": {
"type": "string",
"default": ""
},
"lock": {
"type": "object",
"default": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
import { Main } from '@woocommerce/base-components/sidebar-layout';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
Expand All @@ -16,8 +17,21 @@ import {
import { useForcedLayout, getAllowedBlocks } from '../../../shared';
import './style.scss';

export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
const blockProps = useBlockProps();
export const Edit = ( {
clientId,
attributes,
}: {
clientId: string;
attributes: {
className?: string;
};
} ): JSX.Element => {
const blockProps = useBlockProps( {
className: classnames(
'wc-block-checkout__main',
attributes?.className
),
} );
const {
showOrderNotes,
showPolicyLinks,
Expand Down Expand Up @@ -60,18 +74,16 @@ export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
} );

return (
<Main className="wc-block-checkout__main">
<div { ...blockProps }>
<Controls />
<form className="wc-block-components-form wc-block-checkout__form">
<InnerBlocks
allowedBlocks={ allowedBlocks }
templateLock={ false }
template={ defaultTemplate }
renderAppender={ InnerBlocks.ButtonBlockAppender }
/>
</form>
</div>
<Main { ...blockProps }>
<Controls />
<form className="wc-block-components-form wc-block-checkout__form">
<InnerBlocks
allowedBlocks={ allowedBlocks }
templateLock={ false }
template={ defaultTemplate }
renderAppender={ InnerBlocks.ButtonBlockAppender }
/>
</form>
</Main>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { Main } from '@woocommerce/base-components/sidebar-layout';

/**
Expand All @@ -10,11 +11,13 @@ import './style.scss';

const FrontendBlock = ( {
children,
className,
}: {
children: JSX.Element;
className?: string;
} ): JSX.Element => {
return (
<Main className="wc-block-checkout__main">
<Main className={ classnames( 'wc-block-checkout__main', className ) }>
<form className="wc-block-components-form wc-block-checkout__form">
{ children }
</form>
Expand Down
Loading

0 comments on commit d761be1

Please sign in to comment.