'use strict';

exports.__esModule = true;

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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

// 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref
var FixTable = (0, _react.forwardRef)(function (_ref, ref) {
  var thead = _ref.thead,
      tbody = _ref.tbody,
      _ref$theadFixed = _ref.theadFixed,
      theadFixed = _ref$theadFixed === undefined ? false : _ref$theadFixed,
      _ref$leftFixed = _ref.leftFixed,
      leftFixed = _ref$leftFixed === undefined ? [] : _ref$leftFixed,
      _ref$rightFixed = _ref.rightFixed,
      rightFixed = _ref$rightFixed === undefined ? [] : _ref$rightFixed,
      onBottomRefresh = _ref.onBottomRefresh,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['thead', 'tbody', 'theadFixed', 'leftFixed', 'rightFixed', 'onBottomRefresh', 'children']);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });

  (0, _react.useEffect)(function () {
    if (!refEl || !refEl.current) return;
    console.log('计算尺寸');
    _instance2.default.updateContainerSize(refEl.current, leftFixed, rightFixed);
    _instance2.default.onScroll(refEl.current, onBottomRefresh);
  }, [leftFixed, rightFixed, tbody]);

  function scroll(e) {
    _instance2.default.onScroll(e.target, onBottomRefresh);
  }

  if (!thead || !tbody) return null;
  return _react2.default.createElement(
    'div',
    _extends({ ref: refEl }, others, { className: 'fixtable' + (others.className ? ' ' + others.className : ''), onScroll: scroll }),
    _react2.default.createElement(
      'table',
      { className: 'fixtable-thead' + (theadFixed ? ' fixed' : '') },
      _react2.default.createElement('colgroup', null),
      thead
    ),
    _react2.default.createElement(
      'table',
      { className: 'fixtable-tbody' },
      _react2.default.createElement('colgroup', null),
      tbody
    ),
    children
  );
});

exports.default = FixTable;
module.exports = exports['default'];