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, useRef, useImperativeHandle, useEffect } from 'react';
import Instance from './instance.js';

// 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref
var FixTable = 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 = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });

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

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

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

export default FixTable;