"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"] = createTableRow;

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

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

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

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

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

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

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

var _classnames2 = _interopRequireDefault(require("classnames"));

var _omit = _interopRequireDefault(require("lodash/omit"));

function createTableRow() {
  var Cmp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'tr';

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

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

    function BodyRow(props) {
      var _this;

      (0, _classCallCheck2["default"])(this, BodyRow);
      _this = _super.call(this, props);
      _this.store = props.store;

      var _this$store$getState = _this.store.getState(),
          selectedRowKeys = _this$store$getState.selectedRowKeys;

      _this.state = {
        selected: selectedRowKeys.indexOf(props.rowKey) >= 0
      };
      return _this;
    }

    (0, _createClass2["default"])(BodyRow, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        this.subscribe();
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (this.unsubscribe) {
          this.unsubscribe();
        }
      }
    }, {
      key: "subscribe",
      value: function subscribe() {
        var _this2 = this;

        var _this$props = this.props,
            store = _this$props.store,
            rowKey = _this$props.rowKey;
        this.unsubscribe = store.subscribe(function () {
          var state = _this2.state;

          var _this2$store$getState = _this2.store.getState(),
              selectedRowKeys = _this2$store$getState.selectedRowKeys;

          var selected = selectedRowKeys.indexOf(rowKey) >= 0;

          if (selected !== state.selected) {
            _this2.setState({
              selected: selected
            });
          }
        });
      }
    }, {
      key: "render",
      value: function render() {
        var props = this.props;
        var selected = this.state.selected;
        var className = props.className,
            prefixCls = props.prefixCls,
            children = props.children;
        var omits = ['prefixCls', 'rowKey', 'store', 'children'];

        if (Cmp === 'tr') {
          omits.push('record');
        }

        var otherProps = (0, _omit["default"])(props, omits);
        var classString = (0, _classnames2["default"])(className, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-row-selected"), selected));
        return /*#__PURE__*/_react["default"].createElement(Cmp, (0, _extends2["default"])({}, otherProps, {
          className: classString
        }), children);
      }
    }]);
    return BodyRow;
  }(_react.Component);

  return BodyRow;
}
//# sourceMappingURL=createBodyRow.js.map
