function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React, { useRef } from 'react';
import { Button } from 'antd';
import "./index.less";
import classNames from 'classnames';
import { useEventListener, useMemoizedFn, useSize } from 'ahooks';
var list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', 'ok'];
var defaultArr = [];
var Keyboard = function Keyboard(props) {
  var _props$items = props.items,
    items = _props$items === void 0 ? defaultArr : _props$items,
    className = props.className,
    style = props.style,
    onChange = props.onChange,
    rightItems = props.rightItems,
    _props$keydown = props.keydown,
    keydown = _props$keydown === void 0 ? true : _props$keydown;
  var contentRef = useRef(null);
  var size = useSize(contentRef);
  useEventListener('keydown', function (ev) {
    console.log('ev', ev);
    if (!keydown) {
      return;
    }
    var key = ev.key;
    if (key === 'Enter') {
      key = 'ok';
    }
    if (key === 'keydown') {
      key = '+';
    }
    if (list.includes(key)) {
      // let dom = document.querySelector(`#virtual-keyboard-${ev.key}`);
      // if (dom) {
      // 	dom.click();
      // }

      onChange === null || onChange === void 0 ? void 0 : onChange(key);
    }
  });
  var getHeight = useMemoizedFn(function () {
    var itemSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
    // 默认高度为25% 的容器宽度 * size大小 + 间距
    return ((size === null || size === void 0 ? void 0 : size.width) || 0) / 4 * itemSize + (itemSize - 1) * 7;
  });
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('virtual-keyboard-wrap', className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('virtual-keyboard-content'),
    ref: contentRef
  }, items.map(function (item) {
    var _item$style;
    return /*#__PURE__*/React.createElement("div", {
      className: "virtual-keyboard-item",
      style: item.style
    }, /*#__PURE__*/React.createElement(Button, {
      id: "virtual-keyboard-".concat(item.value),
      key: item.value,
      type: item.type,
      className: "virtual-keyboard-item-button",
      style: {
        color: item.type === 'primary' ? '#fff' : ((_item$style = item.style) === null || _item$style === void 0 ? void 0 : _item$style.color) || undefined
      },
      disabled: item.disabled,
      onClick: function onClick(e) {
        onChange === null || onChange === void 0 ? void 0 : onChange(item.value);
      }
    }, item.label));
  })), rightItems && /*#__PURE__*/React.createElement("div", {
    className: classNames('virtual-keyboard-content', 'virtual-keyboard-right'),
    style: {
      flex: "0 0 calc((100% - 21px) / 4 * 1)"
    }
  }, rightItems === null || rightItems === void 0 ? void 0 : rightItems.map(function (item) {
    var _item$style2, _item$style3;
    return /*#__PURE__*/React.createElement("div", {
      className: "virtual-keyboard-item",
      style: _objectSpread(_objectSpread({}, item.style || {}), {}, {
        height: getHeight(item.size)
      })
    }, /*#__PURE__*/React.createElement(Button, {
      id: "virtual-keyboard-".concat(item.value),
      key: item.value,
      type: item.type,
      className: "virtual-keyboard-item-button",
      style: {
        fontSize: ((_item$style2 = item.style) === null || _item$style2 === void 0 ? void 0 : _item$style2.fontSize) || 25,
        color: item.type === 'primary' ? '#fff' : ((_item$style3 = item.style) === null || _item$style3 === void 0 ? void 0 : _item$style3.color) || undefined
      },
      disabled: item.disabled,
      onClick: function onClick(e) {
        onChange === null || onChange === void 0 ? void 0 : onChange(item.value);
      }
    }, item.label));
  })));
};
export default /*#__PURE__*/React.memo(Keyboard);