Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { Col, Row, FormGroup, Input, Label } from 'reactstrap';
  3. import PropTypes from 'prop-types';
  4. import PopoverHelper from './PopoverHelper';
  5.  
  6.  
  7. class HiddenBodyCheckbox extends Component {
  8.   constructor(props) {
  9.     super(props);
  10.     this.state = {
  11.       openBody: this.props.defaultValue
  12.     };
  13.   }
  14.  
  15.   componentWillReceiveProps(nextProps) {
  16.     if (nextProps.defaultValue !== this.props.defaultValue)
  17.       this.setState({ openBody: nextProps.defaultValue });
  18.   }
  19.  
  20.   toggleCheckbox = (e) => {
  21.     if (this.props.onChange)
  22.       this.props.onChange(this.props.id, e.target.checked);
  23.     this.setState({ openBody: e.target.checked });
  24.   };
  25.  
  26.   render() {
  27.     return (
  28.       <FormGroup check className="checkbox" id={this.props.id}>
  29.         {this.props.title &&
  30.           <Row>
  31.             <Col>
  32.               <Input className="form-check-input" type="checkbox" onChange={this.toggleCheckbox} checked={this.state.openBody}/>
  33.               <Label check className="form-check-label">
  34.                 {this.props.title} <PopoverHelper description={this.props.description} />
  35.               </Label>
  36.             </Col>
  37.           </Row>
  38.         }
  39.         {this.state.openBody &&
  40.           <Row>
  41.             {this.props.children}
  42.           </Row>
  43.         }
  44.       </FormGroup>
  45.     );
  46.   }
  47. }
  48.  
  49. HiddenBodyCheckbox.propTypes = {
  50.   id: PropTypes.oneOfType([
  51.     PropTypes.string,
  52.     PropTypes.number
  53.   ]),
  54.   title: PropTypes.string,
  55.   description: PropTypes.string,
  56.   defaultValue: PropTypes.bool,
  57.   onChange: PropTypes.func
  58. };
  59.  
  60. HiddenBodyCheckbox.defaultProps = {
  61.   id: null,
  62.   description: null,
  63.   defaultValue: false,
  64.   onChange: null
  65. };
  66.  
  67. export default HiddenBodyCheckbox;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement