woocommerce-paypal-payments/modules/ppcp-settings/resources/js/Components/ReusableComponents/SettingsToggleBlock.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

import { ToggleControl } from '@wordpress/components';
import { useRef } from '@wordpress/element';
const SettingsToggleBlock = ( {
isToggled,
setToggled,
disabled = false,
...props
} ) => {
const toggleRef = useRef( null );
const blockClasses = [ 'ppcp-r-toggle-block' ];
const handleLabelClick = () => {
if ( ! toggleRef.current || disabled ) {
return;
}
toggleRef.current.click();
toggleRef.current.focus();
};
2024-10-23 08:56:47 +02:00
return (
<div className={ blockClasses.join( ' ' ) }>
2024-10-23 08:56:47 +02:00
<div className="ppcp-r-toggle-block__wrapper">
<div className="ppcp-r-toggle-block__content">
{ props?.label && (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions -- keyboard element is ToggleControl
<div
className="ppcp-r-toggle-block__content-label"
onClick={ handleLabelClick }
>
2024-10-23 08:56:47 +02:00
{ props.label }
</div>
2024-10-23 08:56:47 +02:00
) }
{ props?.description && (
<p
className="ppcp-r-toggle-block__content-description"
dangerouslySetInnerHTML={ {
__html: props.description,
} }
></p>
) }
</div>
<div className="ppcp-r-toggle-block__switch">
<ToggleControl
ref={ toggleRef }
2024-10-23 08:56:47 +02:00
checked={ isToggled }
onChange={ ( newState ) => setToggled( newState ) }
disabled={ disabled }
2024-10-23 08:56:47 +02:00
/>
</div>
</div>
{ props.children && isToggled && (
<div className="ppcp-r-toggle-block__toggled-content">
{ props.children }
</div>
) }
</div>
);
};
export default SettingsToggleBlock;