| 1 |
import React, { useState, useEffect } from 'react'; |
| 2 |
import parse from 'html-react-parser'; |
| 3 |
import { Tooltip } from '@WizardFields'; |
| 4 |
import classnames from 'classnames'; |
| 5 |
|
| 6 |
function Checkbox( props ) { |
| 7 |
const { |
| 8 |
name, |
| 9 |
id, |
| 10 |
label, |
| 11 |
value, |
| 12 |
desc, |
| 13 |
backComp = false, |
| 14 |
tooltip, |
| 15 |
onClick, |
| 16 |
notice, |
| 17 |
isDisabled = false, |
| 18 |
} = props; |
| 19 |
|
| 20 |
const [ inputvalue, setInputvalue ] = useState( value ); |
| 21 |
|
| 22 |
useEffect( () => { |
| 23 |
setInputvalue( value ); |
| 24 |
}, [ value ] ); |
| 25 |
|
| 26 |
const checkedvalue = backComp ? 'enable' : 'yes'; |
| 27 |
const uncheckedvalue = backComp ? 'disable' : 'no'; |
| 28 |
|
| 29 |
function handleCheckboxClick( e ) { |
| 30 |
let current_value = 'no'; |
| 31 |
|
| 32 |
if ( e.target.checked ) { |
| 33 |
// Check is there any notice added in the checkbox. |
| 34 |
if ( notice && ! show_notice( notice ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
setInputvalue( checkedvalue ); |
| 39 |
current_value = checkedvalue; |
| 40 |
} else { |
| 41 |
setInputvalue( uncheckedvalue ); |
| 42 |
current_value = uncheckedvalue; |
| 43 |
} |
| 44 |
|
| 45 |
// Trigger change |
| 46 |
const changeEvent = new CustomEvent( 'wcf:checkbox:change', { |
| 47 |
bubbles: true, |
| 48 |
detail: { e, name, value: current_value }, |
| 49 |
} ); |
| 50 |
|
| 51 |
document.dispatchEvent( changeEvent ); |
| 52 |
} |
| 53 |
function onChangeHandle( e ) { |
| 54 |
if ( onClick ) { |
| 55 |
onClick( e ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Function to show desired alert box to get the confirmation from the user input. |
| 60 |
function show_notice() { |
| 61 |
switch ( notice.type ) { |
| 62 |
case 'alert': |
| 63 |
alert( notice.message ); |
| 64 |
return true; |
| 65 |
case 'confirm': |
| 66 |
const is_confirm = confirm( notice.message ); |
| 67 |
return is_confirm ? true : false; |
| 68 |
case 'prompt': |
| 69 |
const is_prompt = prompt( notice.message ); |
| 70 |
return is_prompt === notice.check.toUpperCase() ? true : false; |
| 71 |
default: |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return ( |
| 77 |
<div className="wcf-checkbox-field text-left"> |
| 78 |
<div className=""> |
| 79 |
<div className="flex items-center gap-2"> |
| 80 |
<input |
| 81 |
type="hidden" |
| 82 |
name={ name } |
| 83 |
defaultValue={ uncheckedvalue } |
| 84 |
/> |
| 85 |
<input |
| 86 |
type="checkbox" |
| 87 |
className={ classnames( |
| 88 |
props.class, |
| 89 |
'!h-5 !w-5 !rounded !border-gray-300 !text-primary-600 focus:!ring-primary-600 !shadow-none before:!content-none !outline-none !m-0' |
| 90 |
) } |
| 91 |
name={ name } |
| 92 |
value={ inputvalue } |
| 93 |
id={ id ? id : name } |
| 94 |
checked={ checkedvalue === inputvalue ? 'checked' : '' } |
| 95 |
onClick={ handleCheckboxClick } |
| 96 |
onChange={ onChangeHandle } |
| 97 |
disabled={ isDisabled } |
| 98 |
/> |
| 99 |
{ label && ( |
| 100 |
<div className="text-sm font-medium text-left"> |
| 101 |
<label htmlFor={ id ? id : name }> |
| 102 |
{ label } |
| 103 |
{ tooltip && <Tooltip text={ tooltip } /> } |
| 104 |
</label> |
| 105 |
</div> |
| 106 |
) } |
| 107 |
</div> |
| 108 |
</div> |
| 109 |
|
| 110 |
{ desc && ( |
| 111 |
<div className="wcf-field__desc text-[13px] font-normal text-gray-500 mt-2"> |
| 112 |
{ parse( desc ) } |
| 113 |
</div> |
| 114 |
) } |
| 115 |
</div> |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
export default Checkbox; |
| 120 |
|