mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
Refactor the Change buttons and the Watermark
This commit is contained in:
parent
c2831c2ab0
commit
522b27aea0
7 changed files with 207 additions and 147 deletions
|
@ -0,0 +1,77 @@
|
|||
import { createElement, useEffect, createRoot } from '@wordpress/element';
|
||||
|
||||
const ShippingChangeButton = ( { onChangeShippingAddressClick } ) =>
|
||||
createElement(
|
||||
'button',
|
||||
{
|
||||
className: 'wc-block-checkout-axo-block-card__edit',
|
||||
'aria-label': 'Change shipping details',
|
||||
type: 'button',
|
||||
onClick: ( event ) => {
|
||||
event.preventDefault();
|
||||
onChangeShippingAddressClick();
|
||||
},
|
||||
},
|
||||
'Change'
|
||||
);
|
||||
|
||||
const ShippingChangeButtonManager = ( { onChangeShippingAddressClick } ) => {
|
||||
useEffect( () => {
|
||||
const shippingTitle = document.querySelector(
|
||||
'#shipping-fields h2.wc-block-components-title'
|
||||
);
|
||||
|
||||
if ( shippingTitle ) {
|
||||
if (
|
||||
! shippingTitle.nextElementSibling?.classList?.contains(
|
||||
'wc-block-checkout-axo-block-card__edit'
|
||||
)
|
||||
) {
|
||||
const buttonContainer = document.createElement( 'span' );
|
||||
shippingTitle.parentNode.insertBefore(
|
||||
buttonContainer,
|
||||
shippingTitle.nextSibling
|
||||
);
|
||||
|
||||
const root = createRoot( buttonContainer );
|
||||
root.render(
|
||||
createElement( ShippingChangeButton, {
|
||||
onChangeShippingAddressClick,
|
||||
} )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
const button = document.querySelector(
|
||||
'#shipping-fields .wc-block-checkout-axo-block-card__edit'
|
||||
);
|
||||
if ( button && button.parentNode ) {
|
||||
button.parentNode.remove();
|
||||
}
|
||||
};
|
||||
}, [ onChangeShippingAddressClick ] );
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export const injectShippingChangeButton = ( onChangeShippingAddressClick ) => {
|
||||
const container = document.createElement( 'div' );
|
||||
document.body.appendChild( container );
|
||||
createRoot( container ).render(
|
||||
createElement( ShippingChangeButtonManager, {
|
||||
onChangeShippingAddressClick,
|
||||
} )
|
||||
);
|
||||
};
|
||||
|
||||
export const removeShippingChangeButton = () => {
|
||||
const button = document.querySelector(
|
||||
'#shipping-fields .wc-block-checkout-axo-block-card__edit'
|
||||
);
|
||||
if ( button && button.parentNode ) {
|
||||
button.parentNode.remove();
|
||||
}
|
||||
};
|
||||
|
||||
export default ShippingChangeButtonManager;
|
Loading…
Add table
Add a link
Reference in a new issue