function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 = [{
  value: 1,
  label: '1',
  disabled: false,
  type: 'text'
}, {
  value: 2,
  label: '2',
  disabled: false,
  type: 'text'
}, {
  value: 3,
  label: '3',
  disabled: false,
  type: 'text'
}, {
  value: 4,
  label: '4',
  disabled: false,
  type: 'text'
}, {
  value: 5,
  label: '5',
  disabled: false,
  type: 'text'
}, {
  value: 6,
  label: '6',
  disabled: false,
  type: 'text'
}, {
  value: 7,
  label: '7',
  disabled: false,
  type: 'text'
}, {
  value: 8,
  label: '8',
  disabled: false,
  type: 'text'
}, {
  value: 9,
  label: '9',
  disabled: false,
  type: 'text'
}, {
  value: '0',
  label: '0',
  disabled: false,
  type: 'text'
}, {
  value: '00',
  label: '00',
  disabled: false,
  type: 'text'
}, {
  value: '.',
  label: '.',
  disabled: false,
  type: 'text'
}];
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 (key === 'Backspace') {
      key = 'delete';
    }
    if (list.includes(key)) {
      onChange === null || onChange === 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", {
    id: props.id || '',
    className: classNames('pisell-virtual-keyboard-wrap', className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-virtual-keyboard-content'),
    ref: contentRef
  }, items.map(function (item) {
    var _item$style;
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-virtual-keyboard-item",
      style: item.style
    }, /*#__PURE__*/React.createElement(Button, {
      id: "pisell-virtual-keyboard-".concat(item.value),
      key: item.value,
      type: item.type,
      className: "pisell-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 || onChange(item.value);
      }
    }, item.label));
  })), rightItems && /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-virtual-keyboard-content', 'pisell-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: "pisell-virtual-keyboard-item",
      style: _objectSpread(_objectSpread({}, item.style || {}), {}, {
        height: getHeight(item.size)
      })
    }, /*#__PURE__*/React.createElement(Button, {
      id: "pisell-virtual-keyboard-".concat(item.value),
      key: item.value,
      type: item.type,
      className: "pisell-virtual-keyboard-item-button",
      style: _objectSpread({
        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
      }, item.type === 'primary' ? item.style || {} : {}),
      disabled: item.disabled,
      onClick: function onClick(e) {
        onChange === null || onChange === void 0 || onChange(item.value);
      }
    }, item.label));
  })));
};
export default /*#__PURE__*/React.memo(Keyboard);