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
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2019-08-13 11:31:10 +02:00
|
|
|
class InputCheckboxPlugin extends React.Component {
|
|
|
|
// eslint-disable-line react/prefer-stateless-function
|
2018-02-06 18:01:44 +01:00
|
|
|
state = { showBackground: false, showCog: false };
|
|
|
|
|
2019-08-13 11:31:10 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2018-02-06 18:01:44 +01:00
|
|
|
// Remove background if another input is selected
|
2019-08-13 11:31:10 +02:00
|
|
|
if (
|
|
|
|
nextProps.inputSelected !== this.props.inputSelected &&
|
|
|
|
nextProps.inputSelected !== this.props.name
|
|
|
|
) {
|
2018-02-06 18:01:44 +01:00
|
|
|
this.setState({ showBackground: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextProps.isOpen) {
|
|
|
|
this.setState({ showBackground: false, showCog: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = () => {
|
|
|
|
const target = {
|
|
|
|
type: 'checkbox',
|
|
|
|
name: this.props.name,
|
|
|
|
value: !this.props.value,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't show the label background if the user unselects the input
|
|
|
|
if (!this.props.value) {
|
|
|
|
this.setState({ showBackground: true });
|
|
|
|
// Tell the Parent component that another input has been selected
|
|
|
|
this.props.setNewInputSelected(this.props.name);
|
|
|
|
// Tell the policies component to show the associated routes
|
|
|
|
this.context.setShouldDisplayPolicieshint();
|
|
|
|
this.context.setInputPoliciesPath(this.props.name);
|
|
|
|
} else {
|
|
|
|
this.setState({ showBackground: false, showCog: false });
|
|
|
|
this.props.setNewInputSelected('');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.onChange({ target });
|
2019-08-13 11:31:10 +02:00
|
|
|
};
|
2018-02-06 18:01:44 +01:00
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
this.setState({ showBackground: !this.state.showBackground });
|
|
|
|
this.props.setNewInputSelected(this.props.name);
|
|
|
|
this.context.setInputPoliciesPath(this.props.name);
|
|
|
|
|
|
|
|
if (this.state.showBackground) {
|
|
|
|
this.context.resetShouldDisplayPoliciesHint();
|
|
|
|
} else {
|
|
|
|
this.context.setShouldDisplayPolicieshint();
|
|
|
|
}
|
2019-08-13 11:31:10 +02:00
|
|
|
};
|
2018-02-06 18:01:44 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={cn(styles.inputCheckbox, 'col-md-4')}
|
|
|
|
onMouseEnter={() => {
|
|
|
|
if (this.props.value) {
|
|
|
|
this.setState({ showCog: true });
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onMouseLeave={() => this.setState({ showCog: false })}
|
|
|
|
>
|
2019-08-13 11:31:10 +02:00
|
|
|
<div
|
|
|
|
className={cn(
|
|
|
|
'form-check',
|
|
|
|
this.state.showBackground ? styles.highlighted : ''
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<label
|
|
|
|
className={cn(
|
|
|
|
'form-check-label',
|
|
|
|
styles.label,
|
|
|
|
this.props.value ? styles.checked : ''
|
|
|
|
)}
|
|
|
|
htmlFor={this.props.name}
|
|
|
|
>
|
2018-02-06 18:01:44 +01:00
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
defaultChecked={this.props.value}
|
|
|
|
id={this.props.name}
|
|
|
|
name={this.props.name}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
type="checkbox"
|
|
|
|
/>
|
|
|
|
{this.props.label}
|
|
|
|
</label>
|
|
|
|
{this.state.showCog || this.state.showBackground ? (
|
|
|
|
<i className="fa fa-cog" onClick={this.handleClick} />
|
2019-08-13 11:31:10 +02:00
|
|
|
) : (
|
|
|
|
''
|
|
|
|
)}
|
2018-02-06 18:01:44 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputCheckboxPlugin.contextTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
resetShouldDisplayPoliciesHint: PropTypes.func.isRequired,
|
|
|
|
setInputPoliciesPath: PropTypes.func.isRequired,
|
|
|
|
setShouldDisplayPolicieshint: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|