{"size": 1712, "ext": "js", "max_stars_repo_path": "src/components/Checkbox/index.js", "max_stars_repo_name": "saarnilauri/fida-imu", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/Checkbox/index.js", "max_issues_repo_name": "saarnilauri/fida-imu", "max_issues_repo_issues_event_min_datetime": "2018-03-20T08:00:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T12:31:17.000Z", "max_forks_repo_path": "src/components/Checkbox/index.js", "max_forks_repo_name": "saarnilauri/fida-imu", "max_forks_repo_forks_event_min_datetime": "2017-10-26T11:28:57.000Z", "max_forks_repo_forks_event_max_datetime": "2017-10-26T11:28:57.000Z"}
{"max_stars_count": null, "max_issues_count": 69, "max_forks_count": 1, "avg_line_length": 23.4520547945, "max_line_length": 80, "alphanum_fraction": 0.6057242991}
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { FormGroup, Input, Label } from 'reactstrap'

class Checkbox extends Component {
  constructor(props) {
    super(props)

    this.state = {
      isChecked: props.isChecked,
    }
    this.toggleCheckboxChange = this.toggleCheckboxChange.bind(this)
  }

  toggleCheckboxChange() {
    const { handleCheckboxChange, label } = this.props

    this.setState(({ isChecked }) => ({
      isChecked: !isChecked,
    }))

    handleCheckboxChange(label)
  }

  render() {
    const { label, isSimple } = this.props
    const { isChecked } = this.state

    if (isSimple) {
      return (
        <Input
          className="form-check-input-list"
          type="checkbox"
          defaultChecked={isChecked}
          id={`checkbox_${label}`}
          name={`checkbox_${label}`}
          value={label}
          onChange={this.toggleCheckboxChange}
        />
      )
    }
    return (
      <FormGroup check inline className="checkbox" key={Math.random() * 10000}>
        <Input
          className="form-check-input"
          type="checkbox"
          defaultChecked={isChecked}
          id={`checkbox_${label}`}
          name={`checkbox_${label}`}
          value={label}
          onChange={this.toggleCheckboxChange}
        />
        <Label check className="form-check-label" htmlFor={`checkbox_${label}`}>
          {label}
        </Label>
      </FormGroup>
    )
  }
}

Checkbox.defaultProps = {
  isSimple: false,
}

Checkbox.propTypes = {
  label: PropTypes.string.isRequired,
  isChecked: PropTypes.bool,
  isSimple: PropTypes.bool,
  handleCheckboxChange: PropTypes.func.isRequired,
}

export default Checkbox
