var _excluded = ["icon", "iconPosition", "checked", "readOnly", "disabled", "children", "onChange"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useRef, useImperativeHandle } from 'react';

// 复选框
var Checkbox = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var icon = _ref.icon,
    _ref$iconPosition = _ref.iconPosition,
    iconPosition = _ref$iconPosition === void 0 ? 'left' : _ref$iconPosition,
    checked = _ref.checked,
    readOnly = _ref.readOnly,
    disabled = _ref.disabled,
    children = _ref.children,
    onChange = _ref.onChange,
    props = _objectWithoutProperties(_ref, _excluded);
  // Expose
  var rootRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      }
    };
  });

  // 点击回调
  function handleClick() {
    if (disabled || readOnly) return;
    if (onChange) onChange(!checked);
  }

  // 获取选中状态的Node
  function getIconNode(checked) {
    if (typeof icon === 'function') {
      var newIcon = icon({
        checked: checked
      });
      if (newIcon !== undefined) return newIcon;
    }
    if ( /*#__PURE__*/React.isValidElement(icon)) {
      return icon;
    }
    if (typeof icon === 'string') {
      return /*#__PURE__*/React.createElement("span", {
        className: "checkbox-icon ".concat(icon)
      });
    }
    return /*#__PURE__*/React.createElement("span", {
      className: "checkbox-icon default"
    });
  }
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    onClick: handleClick,
    disabled: disabled,
    readOnly: readOnly,
    className: "checkbox".concat(props.className ? ' ' + props.className : '').concat(checked ? ' checked' : ''),
    ref: rootRef
  }), iconPosition !== 'right' && getIconNode(checked), children && /*#__PURE__*/React.createElement("span", {
    className: "checkbox-content"
  }, children), iconPosition === 'right' && getIconNode(checked));
});
export default Checkbox;