103 lines
2.2 KiB
JavaScript
Raw Normal View History

/**
2019-08-13 11:31:10 +02:00
*
* InputCheckboxPlugin
*
*/
import React, { useState } from 'react';
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';
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;
const [policiesShown, setPoliciesShow] = useState(false);
2019-09-18 17:39:54 +02:00
const handleChange = () => {
const target = {
type: 'checkbox',
2019-09-18 17:39:54 +02:00
name: name,
value: !value,
};
2019-09-18 17:39:54 +02:00
if (!value) {
setPoliciesShow(true);
2019-09-18 17:39:54 +02:00
setNewInputSelected(name);
2019-10-03 12:58:20 +02:00
setShouldDisplayPolicieshint();
setInputPoliciesPath(name);
} else {
2019-09-18 17:39:54 +02:00
setNewInputSelected('');
}
2019-10-03 12:58:20 +02:00
onChange({ target });
2019-08-13 11:31:10 +02:00
};
2019-09-18 17:39:54 +02:00
const handleClick = () => {
setNewInputSelected(name);
2019-10-03 12:58:20 +02:00
setInputPoliciesPath(name);
if (policiesShown && isSelected) {
2019-10-03 12:58:20 +02:00
resetShouldDisplayPoliciesHint();
setPoliciesShow(false);
} else {
2019-10-03 12:58:20 +02:00
setShouldDisplayPolicieshint();
setPoliciesShow(true);
}
2019-08-13 11:31:10 +02:00
};
2019-09-18 17:39:54 +02:00
return (
<Wrapper className="col-md-4">
<div
className={`form-check ${isSelected ? 'highlighted' : ''} ${
value ? 'is-checked' : ''
}`}
>
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>
<i className="fa fa-cog" onClick={handleClick} />
</div>
</Wrapper>
);
}
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;