"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _dropdown = _interopRequireDefault(require("../../dropdown"));

var _menu = _interopRequireDefault(require("../../menu"));

var _button = _interopRequireDefault(require("../../button"));

var _icon = _interopRequireDefault(require("../../icon"));

var _util = require("../../util");

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// 共享状态的组件需要变成非受控组件
var Filter =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Filter, _React$Component);

  function Filter(props) {
    var _this;

    _classCallCheck(this, Filter);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Filter).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "filterKeydown", function (e) {
      e.preventDefault();
      e.stopPropagation();

      if (e.keyCode === _util.KEYCODE.ENTER) {
        _this.setState({
          visible: !_this.state.visible
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onFilterVisible", function (visible) {
      _this.setState({
        visible: visible
      });

      if (visible && _this.filterRef) {
        _util.dom.addClass(_this.filterRef, "".concat(_this.props.prefix, "table-filter-active"));
      }

      if (!visible && _this.filterRef) {
        _util.dom.removeClass(_this.filterRef, "".concat(_this.props.prefix, "table-filter-active"));
      }

      if (!visible) {
        var selectedKeys = _toConsumableArray(_this._selectedKeys);

        _this.setState({
          selectedKeys: selectedKeys
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onFilterSelect", function (selectedKeys) {
      _this.setState({
        visible: true,
        selectedKeys: selectedKeys
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onFilterConfirm", function () {
      var selectedKeys = _this.state.selectedKeys;
      var filterParams = {};
      var dataIndex = _this.props.dataIndex;
      filterParams[dataIndex] = {
        visible: false,
        selectedKeys: selectedKeys
      };
      _this._selectedKeys = _toConsumableArray(selectedKeys);

      _this.setState({
        visible: false
      });

      if (_this.filterRef) {
        _util.dom.removeClass(_this.filterRef, "".concat(_this.props.prefix, "table-filter-active"));
      } // 兼容之前的格式


      _this.props.onFilter(filterParams);
    });

    _defineProperty(_assertThisInitialized(_this), "onFilterClear", function () {
      var filterParams = {};
      var dataIndex = _this.props.dataIndex;
      filterParams[dataIndex] = {
        visible: false,
        selectedKeys: []
      };
      _this._selectedKeys = [];

      _this.setState({
        selectedKeys: [],
        visible: false
      });

      if (_this.filterRef) {
        _util.dom.removeClass(_this.filterRef, "".concat(_this.props.prefix, "table-filter-active"));
      } // 兼容之前的格式


      _this.props.onFilter(filterParams);
    });

    var _filterParams = props.filterParams || {};

    var filterConfig = _filterParams[props.dataIndex] || {};
    _this.state = {
      visible: filterConfig.visible || false,
      selectedKeys: filterConfig.selectedKeys || []
    };
    _this._selectedKeys = _toConsumableArray(_this.state.selectedKeys);
    return _this;
  }

  _createClass(Filter, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (nextProps.hasOwnProperty('filterParams') && typeof nextProps.filterParams !== 'undefined') {
        var dataIndex = nextProps.dataIndex || this.props.dataIndex;
        var filterParams = nextProps.filterParams || {};
        var filterConfig = filterParams[dataIndex] || {};
        var selectedKeys = filterConfig.selectedKeys || [];
        this.setState({
          selectedKeys: selectedKeys
        });
        this._selectedKeys = _toConsumableArray(selectedKeys);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          filters = _this$props.filters,
          prefix = _this$props.prefix,
          locale = _this$props.locale,
          filterMode = _this$props.filterMode,
          filterMenuProps = _this$props.filterMenuProps,
          rtl = _this$props.rtl;
      var _this$state = this.state,
          visible = _this$state.visible,
          selectedKeys = _this$state.selectedKeys;

      var subMenuSelectable = filterMenuProps.subMenuSelectable,
          others = _objectWithoutProperties(filterMenuProps, ["subMenuSelectable"]);

      function renderMenuItem(item) {
        return _react["default"].createElement(_menu["default"].Item, {
          key: item.value
        }, item.label);
      }

      function renderSubMenu(parent, children) {
        return _react["default"].createElement(_menu["default"].SubMenu, {
          label: parent.label,
          key: parent.value,
          selectable: subMenuSelectable
        }, renderMenuContent(children));
      }

      function renderMenuContent(list) {
        return list.map(function (item) {
          if (item.children) {
            return renderSubMenu(item, item.children);
          } else {
            return renderMenuItem(item);
          }
        });
      }

      var content = renderMenuContent(filters);

      var footer = _react["default"].createElement("div", {
        className: "".concat(prefix, "table-filter-footer")
      }, _react["default"].createElement(_button["default"], {
        type: "primary",
        size: "small",
        onClick: this.onFilterConfirm
      }, locale.ok), _react["default"].createElement(_button["default"], {
        onClick: this.onFilterClear,
        size: "small"
      }, locale.reset));

      return _react["default"].createElement(_dropdown["default"], {
        trigger: _react["default"].createElement("span", {
          role: "button",
          "aria-label": locale.filter,
          onKeyDown: this.filterKeydown,
          tabIndex: "0",
          className: "".concat(prefix, "table-filter"),
          ref: function ref(el) {
            _this2.filterRef = el;
          }
        }, _react["default"].createElement(_icon["default"], {
          type: "filter",
          size: "small"
        })),
        triggerType: "click",
        visible: visible,
        autoFocus: true,
        rtl: rtl,
        container: function container(node) {
          return node.parentNode;
        },
        onVisibleChange: this.onFilterVisible
      }, _react["default"].createElement(_menu["default"], _extends({
        footer: footer,
        rtl: rtl,
        mode: "inline",
        selectedKeys: selectedKeys,
        selectMode: filterMode,
        onSelect: this.onFilterSelect
      }, others), content));
    }
  }]);

  return Filter;
}(_react["default"].Component);

exports["default"] = Filter;

_defineProperty(Filter, "propTypes", {
  dataIndex: _propTypes["default"].string,
  filters: _propTypes["default"].array,
  filterMode: _propTypes["default"].string,
  filterParams: _propTypes["default"].object,
  filterMenuProps: _propTypes["default"].object,
  locale: _propTypes["default"].object,
  onFilter: _propTypes["default"].func,
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool
});

_defineProperty(Filter, "defaultProps", {
  onFilter: function onFilter() {}
});