import React, { Component } from 'react';

export default () => (
  class extends Component {
    isChecked() {
      const { option: { id }, value = [] } = this.props;
      return (value.indexOf(id) > -1);
    }
    toggle() {
      const { option: { id } } = this.props;
      const value = [...(this.props.value || [])];
      if (this.isChecked()) {
        value.splice(value.indexOf(id), 1);
      } else {
        value.push(id);
      }
      this.props.onChange(value);
      this.props.onBlur(value);
    }
    render() {
      return React.cloneElement(this.props.checkbox, { checked: ::this.isChecked(), onChange: () => this.toggle()});
    }
  }
);
