/* * The tooltip can't be included in the native toggleControl, so we have to build our own. */ import { useState, useRef, useEffect } from "@wordpress/element"; import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components'; import hoverTooltip from "../utils/hoverTooltip"; import {__} from '@wordpress/i18n'; const CheckboxControl = (props) => { const checkboxRef = useRef(null); let disabledCheckboxPropBoolean = (props.disabled === true); let disabledCheckboxViaFieldConfig = (props.field.disabled === true); let checkboxDisabled = ( disabledCheckboxViaFieldConfig || disabledCheckboxPropBoolean ); let tooltipText = ''; let emptyValues = [undefined, null, '']; if (checkboxDisabled && props.field.hasOwnProperty('disabledTooltipHoverText') && !emptyValues.includes(props.field.disabledTooltipHoverText) ) { tooltipText = props.field.disabledTooltipHoverText; } hoverTooltip( checkboxRef, (checkboxDisabled && (tooltipText !== '')), tooltipText ); // const tooltipText = __("404 errors detected on your home page. 404 blocking is unavailable, to prevent blocking of legitimate visitors. It is strongly recommended to resolve these errors.", "really-simple-ssl"); // // Pass props.disabled as the condition // hoverTooltip(checkboxRef, props.disabled, tooltipText); const [ isOpen, setIsOpen ] = useState( false ); const onChangeHandler = (e) => { // WordPress <6.0 does not have the confirmdialog component if ( !ConfirmDialog ) { executeAction(); return; } if (props.field.warning && props.field.warning.length>0 && !props.field.value) { setIsOpen( true ); } else { executeAction(); } } const handleConfirm = async () => { setIsOpen( false ); executeAction(); }; const handleCancel = () => { setIsOpen( false ); }; const executeAction = (e) => { let fieldValue = !props.field.value; props.onChangeHandler(fieldValue) } const handleKeyDown = (e) => { if (e.key === 'Enter') { e.preventDefault(); onChangeHandler(true); } } let field = props.field; let is_checked = field.value ? 'is-checked' : ''; let is_disabled = props.disabled ? 'is-disabled' : ''; return ( <> {ConfirmDialog && {field.warning} }
handleKeyDown(e)} checked={props.value} className="components-form-toggle__input" onChange={ ( e ) => onChangeHandler(e) } id={props.id} type="checkbox" disabled={props.disabled} />
); } export default CheckboxControl