var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React, { forwardRef } from 'react';

var Checkbox = forwardRef(function (_ref, ref) {
  var type = _ref.type,
      value = _ref.value,
      checked = _ref.checked,
      disabled = _ref.disabled,
      _ref$inputAttribute = _ref.inputAttribute,
      inputAttribute = _ref$inputAttribute === undefined ? {} : _ref$inputAttribute,
      caption = _ref.caption,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === undefined ? {} : _ref$captionAttribute,
      onClick = _ref.onClick,
      others = _objectWithoutProperties(_ref, ['type', 'value', 'checked', 'disabled', 'inputAttribute', 'caption', 'captionAttribute', 'onClick']);

  function clickHandler(e, isChecked) {
    if (disabled) return;
    if (onClick) onClick(e, isChecked);
  }
  var typeClassPrefix = type === 'radio' ? 'radio' : 'checkbox';
  return React.createElement(
    'div',
    _extends({
      ref: ref
    }, others, {
      onClick: function onClick(e) {
        clickHandler(e, e.target.getAttribute('data-checked') === 'true');
      },
      disabled: disabled,
      'data-checked': checked,
      'data-value': value,
      className: '' + typeClassPrefix + (others.className ? ' ' + others.className : '')
    }),
    React.createElement('span', _extends({}, inputAttribute, {
      className: typeClassPrefix + '-input' + (inputAttribute.className ? ' ' + inputAttribute.className : '')
    })),
    caption && React.createElement(
      'span',
      _extends({}, captionAttribute, {
        className: typeClassPrefix + '-caption' + (captionAttribute.className ? ' ' + captionAttribute.className : '')
      }),
      caption
    )
  );
});

export default Checkbox;