2018-02-06 18:01:44 +01:00
|
|
|
/**
|
2019-08-13 11:31:10 +02:00
|
|
|
*
|
|
|
|
* InputCheckboxPlugin
|
|
|
|
*
|
|
|
|
*/
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-10-07 13:42:39 +02:00
|
|
|
import React, { useState } from 'react';
|
2018-02-06 18:01:44 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2019-10-03 12:58:20 +02:00
|
|
|
import { useEditPageContext } from '../../contexts/EditPage';
|
2019-09-17 15:24:22 +02:00
|
|
|
import { Label, Wrapper } from './Components';
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-10-03 12:58:20 +02:00
|
|
|
function InputCheckboxPlugin({
|
|
|
|
inputSelected,
|
|
|
|
label,
|
|
|
|
name,
|
|
|
|
setNewInputSelected,
|
|
|
|
value,
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
onChange,
|
|
|
|
resetShouldDisplayPoliciesHint,
|
|
|
|
setInputPoliciesPath,
|
|
|
|
setShouldDisplayPolicieshint,
|
|
|
|
} = useEditPageContext();
|
2019-09-18 17:39:54 +02:00
|
|
|
const isSelected = inputSelected === name;
|
2019-10-07 13:42:39 +02:00
|
|
|
const [policiesShown, setPoliciesShow] = useState(false);
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-09-18 17:39:54 +02:00
|
|
|
const handleChange = () => {
|
2018-02-06 18:01:44 +01:00
|
|
|
const target = {
|
|
|
|
type: 'checkbox',
|
2019-09-18 17:39:54 +02:00
|
|
|
name: name,
|
|
|
|
value: !value,
|
2018-02-06 18:01:44 +01:00
|
|
|
};
|
|
|
|
|
2019-09-18 17:39:54 +02:00
|
|
|
if (!value) {
|
2019-10-07 13:42:39 +02:00
|
|
|
setPoliciesShow(true);
|
2019-09-18 17:39:54 +02:00
|
|
|
setNewInputSelected(name);
|
|
|
|
|
2019-10-03 12:58:20 +02:00
|
|
|
setShouldDisplayPolicieshint();
|
|
|
|
setInputPoliciesPath(name);
|
2018-02-06 18:01:44 +01:00
|
|
|
} else {
|
2019-09-18 17:39:54 +02:00
|
|
|
setNewInputSelected('');
|
2018-02-06 18:01:44 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 12:58:20 +02:00
|
|
|
onChange({ target });
|
2019-08-13 11:31:10 +02:00
|
|
|
};
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-09-18 17:39:54 +02:00
|
|
|
const handleClick = () => {
|
|
|
|
setNewInputSelected(name);
|
2019-10-03 12:58:20 +02:00
|
|
|
setInputPoliciesPath(name);
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-10-07 13:42:39 +02:00
|
|
|
if (policiesShown && isSelected) {
|
2019-10-03 12:58:20 +02:00
|
|
|
resetShouldDisplayPoliciesHint();
|
2019-10-07 13:42:39 +02:00
|
|
|
setPoliciesShow(false);
|
2018-02-06 18:01:44 +01:00
|
|
|
} else {
|
2019-10-03 12:58:20 +02:00
|
|
|
setShouldDisplayPolicieshint();
|
2019-10-07 13:42:39 +02:00
|
|
|
setPoliciesShow(true);
|
2018-02-06 18:01:44 +01:00
|
|
|
}
|
2019-08-13 11:31:10 +02:00
|
|
|
};
|
2018-02-06 18:01:44 +01:00
|
|
|
|
2019-09-18 17:39:54 +02:00
|
|
|
return (
|
2020-06-14 05:05:09 +02:00
|
|
|
<Wrapper className="col-md-4" onClick={handleClick}>
|
2019-10-07 13:42:39 +02:00
|
|
|
<div
|
2019-10-08 10:21:27 +02:00
|
|
|
className={`form-check ${
|
|
|
|
isSelected && policiesShown ? 'highlighted' : ''
|
|
|
|
} ${value ? 'is-checked' : ''}`}
|
2019-10-07 13:42:39 +02:00
|
|
|
>
|
2019-09-18 17:39:54 +02:00
|
|
|
<Label
|
|
|
|
className={`form-check-label ${value ? 'checked' : ''}`}
|
|
|
|
htmlFor={name}
|
2019-08-13 11:31:10 +02:00
|
|
|
>
|
2019-09-18 17:39:54 +02:00
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
defaultChecked={value}
|
|
|
|
id={name}
|
|
|
|
name={name}
|
|
|
|
onChange={handleChange}
|
|
|
|
type="checkbox"
|
|
|
|
/>
|
|
|
|
{label}
|
|
|
|
</Label>
|
2020-06-14 05:05:09 +02:00
|
|
|
<i className="fa fa-cog" />
|
2019-09-18 17:39:54 +02:00
|
|
|
</div>
|
|
|
|
</Wrapper>
|
|
|
|
);
|
2018-02-06 18:01:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InputCheckboxPlugin.defaultProps = {
|
|
|
|
label: '',
|
|
|
|
value: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
InputCheckboxPlugin.propTypes = {
|
|
|
|
inputSelected: PropTypes.string.isRequired,
|
|
|
|
isOpen: PropTypes.bool.isRequired,
|
|
|
|
label: PropTypes.string,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
setNewInputSelected: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default InputCheckboxPlugin;
|