"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectSpread4 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _miniStore = require("mini-store");

var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));

var _TableCell = _interopRequireDefault(require("./TableCell"));

var _warning = _interopRequireDefault(require("../../_util/warning"));

var _TableRowContext = _interopRequireWildcard(require("./TableRowContext"));

var TableRow = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(TableRow, _Component);

  var _super = (0, _createSuper2["default"])(TableRow);

  function TableRow(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, TableRow);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onRowClick", function (event) {
      var _this$props = _this.props,
          record = _this$props.record,
          index = _this$props.index,
          onRowClick = _this$props.onRowClick;

      if (onRowClick) {
        onRowClick(record, index, event);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onRowDoubleClick", function (event) {
      var _this$props2 = _this.props,
          record = _this$props2.record,
          index = _this$props2.index,
          onRowDoubleClick = _this$props2.onRowDoubleClick;

      if (onRowDoubleClick) {
        onRowDoubleClick(record, index, event);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onContextMenu", function (event) {
      var _this$props3 = _this.props,
          record = _this$props3.record,
          index = _this$props3.index,
          onRowContextMenu = _this$props3.onRowContextMenu;

      if (onRowContextMenu) {
        onRowContextMenu(record, index, event);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMouseEnter", function (event) {
      var _this$props4 = _this.props,
          record = _this$props4.record,
          index = _this$props4.index,
          onRowMouseEnter = _this$props4.onRowMouseEnter,
          onHover = _this$props4.onHover,
          rowKey = _this$props4.rowKey;
      onHover(true, rowKey);

      if (onRowMouseEnter) {
        onRowMouseEnter(record, index, event);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMouseLeave", function (event) {
      var _this$props5 = _this.props,
          record = _this$props5.record,
          index = _this$props5.index,
          onRowMouseLeave = _this$props5.onRowMouseLeave,
          onHover = _this$props5.onHover,
          rowKey = _this$props5.rowKey;
      onHover(false, rowKey);

      if (onRowMouseLeave) {
        onRowMouseLeave(record, index, event);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "syncRowHeight", function () {
      var _this$props6 = _this.props,
          isAnyColumnsFixed = _this$props6.isAnyColumnsFixed,
          fixed = _this$props6.fixed,
          expandedRow = _this$props6.expandedRow,
          ancestorKeys = _this$props6.ancestorKeys;

      if (!isAnyColumnsFixed) {
        return;
      }

      if (!fixed && expandedRow) {
        _this.setExpanedRowHeight();
      }

      if (!fixed && ancestorKeys.length >= 0) {
        _this.setRowHeight();
      }
    });
    _this.shouldRender = props.visible;
    _this.resizeObserver = null;
    _this.state = {};
    return _this;
  }

  (0, _createClass2["default"])(TableRow, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.state.shouldRender) {
        this.saveRowRef();

        if (this.rowRef) {
          this.resizeObserver = new _resizeObserverPolyfill["default"](this.syncRowHeight);

          var dom = _reactDom["default"].findDOMNode(this);

          this.resizeObserver.observe(dom);
        }
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      return !!(this.props.visible || nextProps.visible);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.state.shouldRender && !this.rowRef) {
        this.saveRowRef();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.resizeObserver) {
        this.resizeObserver.disconnect();
        delete this.resizeObserver;
      }
    }
  }, {
    key: "setExpanedRowHeight",
    value: function setExpanedRowHeight() {
      var _this$props7 = this.props,
          store = _this$props7.store,
          rowKey = _this$props7.rowKey;

      var _store$getState = store.getState(),
          expandedRowsHeight = _store$getState.expandedRowsHeight;

      var height = this.rowRef.getBoundingClientRect().height;
      var oldHeight = expandedRowsHeight && expandedRowsHeight[rowKey];

      if (height && oldHeight !== height) {
        expandedRowsHeight = (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, expandedRowsHeight), {}, (0, _defineProperty2["default"])({}, rowKey, height));
        store.setState({
          expandedRowsHeight: expandedRowsHeight
        });
      }
    }
  }, {
    key: "setRowHeight",
    value: function setRowHeight() {
      var _this$props8 = this.props,
          store = _this$props8.store,
          rowKey = _this$props8.rowKey;

      var _store$getState2 = store.getState(),
          fixedColumnsBodyRowsHeight = _store$getState2.fixedColumnsBodyRowsHeight;

      var height = this.rowRef.getBoundingClientRect().height;
      var oldHeight = fixedColumnsBodyRowsHeight && fixedColumnsBodyRowsHeight[rowKey];

      if (height && oldHeight !== height) {
        store.setState({
          fixedColumnsBodyRowsHeight: (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, fixedColumnsBodyRowsHeight), {}, (0, _defineProperty2["default"])({}, rowKey, height))
        });
      }
    }
  }, {
    key: "getStyle",
    value: function getStyle() {
      var _this$props9 = this.props,
          height = _this$props9.height,
          visible = _this$props9.visible;

      if (height && height !== this.style.height) {
        this.style = (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, this.style), {}, {
          height: height
        });
      }

      if (!visible && !this.style.display) {
        this.style = (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, this.style), {}, {
          display: 'none'
        });
      }

      return this.style;
    }
  }, {
    key: "saveRowRef",
    value: function saveRowRef() {
      this.rowRef = _reactDom["default"].findDOMNode(this);
      this.syncRowHeight();
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.state.shouldRender) {
        return null;
      }

      var _this$props10 = this.props,
          prefixCls = _this$props10.prefixCls,
          columns = _this$props10.columns,
          record = _this$props10.record,
          rowKey = _this$props10.rowKey,
          index = _this$props10.index,
          onRow = _this$props10.onRow,
          indent = _this$props10.indent,
          indentSize = _this$props10.indentSize,
          hovered = _this$props10.hovered,
          height = _this$props10.height,
          visible = _this$props10.visible,
          fixed = _this$props10.fixed,
          components = _this$props10.components,
          hasExpandIcon = _this$props10.hasExpandIcon,
          renderExpandIcon = _this$props10.renderExpandIcon,
          renderExpandIconCell = _this$props10.renderExpandIconCell;
      var BodyRow = components.body.row;
      var BodyCell = components.body.cell;
      var className = this.props.className;

      if (hovered) {
        className += " ".concat(prefixCls, "-hover");
      }

      var cells = [];
      renderExpandIconCell(cells);

      var _loop = function _loop(i) {
        var column = columns[i];
        (0, _warning["default"])(column.onCellClick === undefined, 'column[onCellClick] is deprecated, please use column[onCell] instead.');
        cells.push( /*#__PURE__*/_react["default"].createElement(_TableRowContext["default"].Consumer, {
          key: column.key || column.dataIndex
        }, function (form) {
          return /*#__PURE__*/_react["default"].createElement(_TableCell["default"], {
            prefixCls: prefixCls,
            record: record,
            indentSize: indentSize,
            indent: indent,
            form: form,
            index: index,
            column: column,
            key: column.key || column.dataIndex,
            expandIcon: hasExpandIcon(i) && renderExpandIcon(),
            component: BodyCell
          });
        }));
      };

      for (var i = 0; i < columns.length; i++) {
        _loop(i);
      }

      var rowClassName = "".concat(prefixCls, " ").concat(className, " ").concat(prefixCls, "-level-").concat(indent).trim();
      var rowProps = onRow(record, index);
      var customStyle = rowProps ? rowProps.style : {};
      var style = {
        height: height
      };

      if (!visible) {
        style.display = 'none';
      }

      style = (0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, style), customStyle);
      return /*#__PURE__*/_react["default"].createElement(_TableRowContext.InnerRowCtx.Provider, {
        value: {
          syncRowHeight: this.syncRowHeight
        }
      }, /*#__PURE__*/_react["default"].createElement(BodyRow, (0, _extends2["default"])({
        onClick: this.onRowClick,
        onDoubleClick: this.onRowDoubleClick,
        onMouseEnter: this.onMouseEnter,
        onMouseLeave: this.onMouseLeave,
        onContextMenu: this.onContextMenu,
        className: rowClassName,
        fixed: fixed,
        record: record
      }, rowProps, {
        style: style,
        "data-row-key": rowKey
      }), cells));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      if (prevState.visible || !prevState.visible && nextProps.visible) {
        return {
          shouldRender: true,
          visible: nextProps.visible
        };
      }

      return {
        visible: nextProps.visible
      };
    }
  }]);
  return TableRow;
}(_react.Component);

(0, _defineProperty2["default"])(TableRow, "defaultProps", {
  onRow: function onRow() {},
  expandIconColumnIndex: 0,
  expandRowByClick: false,
  onHover: function onHover() {},
  hasExpandIcon: function hasExpandIcon() {},
  renderExpandIcon: function renderExpandIcon() {},
  renderExpandIconCell: function renderExpandIconCell() {}
});

function getRowHeight(state, props) {
  var expandedRowsHeight = state.expandedRowsHeight,
      fixedColumnsBodyRowsHeight = state.fixedColumnsBodyRowsHeight;
  var fixed = props.fixed,
      rowKey = props.rowKey;

  if (!fixed) {
    return null;
  }

  if (expandedRowsHeight[rowKey]) {
    return expandedRowsHeight[rowKey];
  }

  if (fixedColumnsBodyRowsHeight[rowKey]) {
    return fixedColumnsBodyRowsHeight[rowKey];
  }

  return null;
}

var _default = (0, _miniStore.connect)(function (state, props) {
  var currentHoverKey = state.currentHoverKey,
      expandedRowKeys = state.expandedRowKeys;
  var rowKey = props.rowKey,
      ancestorKeys = props.ancestorKeys;
  var visible = ancestorKeys.length === 0 || ancestorKeys.every(function (k) {
    return ~expandedRowKeys.indexOf(k);
  });
  return {
    visible: visible,
    hovered: currentHoverKey === rowKey,
    height: getRowHeight(state, props)
  };
})(TableRow);

exports["default"] = _default;
//# sourceMappingURL=TableRow.js.map
