2024-10-23 15:24:52 +02:00
|
|
|
import data from '../../utils/data';
|
|
|
|
|
|
|
|
const SelectBox = ( props ) => {
|
2024-10-24 13:54:50 +02:00
|
|
|
const handleCheckboxState = ( checked ) => {
|
|
|
|
let newValue = null;
|
|
|
|
if ( checked ) {
|
|
|
|
newValue = [ ...props.currentValue, props.value ];
|
|
|
|
props.changeCallback( newValue );
|
|
|
|
} else {
|
|
|
|
newValue = props.currentValue.filter(
|
|
|
|
( value ) => value !== props.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
props.changeCallback( newValue );
|
|
|
|
};
|
|
|
|
|
2024-10-24 06:35:48 +02:00
|
|
|
let boxClassName = 'ppcp-r-select-box';
|
|
|
|
|
2024-10-24 13:54:50 +02:00
|
|
|
if (
|
|
|
|
props.value === props.currentValue ||
|
|
|
|
( Array.isArray( props.currentValue ) &&
|
|
|
|
props.currentValue.includes( props.value ) )
|
|
|
|
) {
|
2024-10-24 06:35:48 +02:00
|
|
|
boxClassName += ' selected';
|
|
|
|
}
|
2024-10-24 13:54:50 +02:00
|
|
|
|
2024-10-23 15:24:52 +02:00
|
|
|
return (
|
2024-10-24 06:35:48 +02:00
|
|
|
<div className={ boxClassName }>
|
2024-10-24 13:54:50 +02:00
|
|
|
{ props.type === 'radio' && (
|
|
|
|
<div className="ppcp-r-select-box__radio">
|
|
|
|
<input
|
|
|
|
className="ppcp-r-select-box__radio-value"
|
|
|
|
type="radio"
|
|
|
|
checked={ props.value === props.currentValue }
|
|
|
|
name={ props.name }
|
|
|
|
value={ props.value }
|
|
|
|
onChange={ () => props.changeCallback( props.value ) }
|
|
|
|
/>
|
|
|
|
<span className="ppcp-r-select-box__radio-presentation"></span>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
{ props.type === 'checkbox' && (
|
|
|
|
<div className="ppcp-r-select-box__checkbox">
|
|
|
|
<input
|
|
|
|
className="ppcp-r-select-box__checkbox-value"
|
|
|
|
type="checkbox"
|
|
|
|
checked={ props.currentValue.includes( props.value ) }
|
|
|
|
name={ props.name }
|
|
|
|
value={ props.value }
|
|
|
|
onChange={ ( e ) =>
|
|
|
|
handleCheckboxState( e.target.checked )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<span className="ppcp-r-select-box__checkbox-presentation">
|
|
|
|
{ data().getImage( 'icon-checkbox.svg' ) }
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
) }
|
2024-10-23 15:24:52 +02:00
|
|
|
<div className="ppcp-r-select-box__content">
|
|
|
|
{ data().getImage( props.icon ) }
|
|
|
|
<div className="ppcp-r-select-box__content-inner">
|
|
|
|
<span className="ppcp-r-select-box__title">
|
|
|
|
{ props.title }
|
|
|
|
</span>
|
|
|
|
<p className="ppcp-r-select-box__description">
|
|
|
|
{ props.description }
|
|
|
|
</p>
|
|
|
|
{ props.children && (
|
|
|
|
<div className="ppcp-r-select-box__additional-content">
|
|
|
|
{ props.children }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectBox;
|