"use strict";

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

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

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

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

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

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

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

var _item = _interopRequireDefault(require("./item"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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 bindCtx = _util.func.bindCtx;
var pickOthers = _util.obj.pickOthers;
var addClass = _util.dom.addClass,
    removeClass = _util.dom.removeClass,
    setStyle = _util.dom.setStyle,
    getStyle = _util.dom.getStyle;
/**
 * Cascader
 */

var Cascader =
/*#__PURE__*/
function (_Component) {
  _inherits(Cascader, _Component);

  function Cascader(props, context) {
    var _this;

    _classCallCheck(this, Cascader);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Cascader).call(this, props, context));
    var defaultValue = props.defaultValue,
        value = props.value,
        defaultExpandedValue = props.defaultExpandedValue,
        expandedValue = props.expandedValue,
        dataSource = props.dataSource,
        multiple = props.multiple,
        checkStrictly = props.checkStrictly,
        canOnlyCheckLeaf = props.canOnlyCheckLeaf,
        loadData = props.loadData;

    _this.updateCache(dataSource);

    var normalizedValue = _this.normalizeValue(typeof value === 'undefined' ? defaultValue : value);

    if (!loadData) {
      normalizedValue = normalizedValue.filter(function (v) {
        return _this._v2n[v];
      });
    } // TODO loadData


    var realExpandedValue = typeof expandedValue === 'undefined' ? typeof defaultExpandedValue === 'undefined' ? _this.getExpandedValue(normalizedValue[0]) : _this.normalizeValue(defaultExpandedValue) : _this.normalizeValue(expandedValue);
    var st = {
      value: normalizedValue,
      expandedValue: realExpandedValue
    };

    if (multiple && !checkStrictly && !canOnlyCheckLeaf) {
      st.value = _this.completeValue(props.dataSource, st.value);
    }

    _this.state = st;
    _this.lastExpandedValue = _toConsumableArray(_this.state.expandedValue);
    bindCtx(_assertThisInitialized(_this), ['handleMouseLeave', 'handleFocus', 'handleFold', 'getCascaderNode', 'onBlur']);
    return _this;
  }

  _createClass(Cascader, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setCascaderInnerWidth();
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var _this2 = this;

      this.updateCache(nextProps.dataSource);
      var state = {};

      if ('value' in nextProps) {
        state.value = this.normalizeValue(nextProps.value);

        if (!nextProps.loadData) {
          state.value = state.value.filter(function (v) {
            return _this2._v2n[v];
          });
        }

        var multiple = nextProps.multiple,
            checkStrictly = nextProps.checkStrictly,
            canOnlyCheckLeaf = nextProps.canOnlyCheckLeaf;

        if (multiple && !checkStrictly && !canOnlyCheckLeaf) {
          state.value = this.completeValue(nextProps.dataSource, state.value);
        }

        if (!this.state.expandedValue.length && !('expandedValue' in nextProps)) {
          state.expandedValue = this.getExpandedValue(state.value[0]);
        }
      }

      if ('expandedValue' in nextProps) {
        state.expandedValue = this.normalizeValue(nextProps.expandedValue);
      }

      if (Object.keys(state).length) {
        this.setState(state);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.setCascaderInnerWidth();
    }
  }, {
    key: "getCascaderNode",
    value: function getCascaderNode(ref) {
      this.cascader = ref;

      if (this.cascader) {
        this.cascaderInner = this.cascader.querySelector(".".concat(this.props.prefix, "cascader-inner"));
      }
    }
  }, {
    key: "setCascaderInnerWidth",
    value: function setCascaderInnerWidth() {
      if (!this.cascaderInner) {
        return;
      }

      var menus = [].slice.call(this.cascaderInner.querySelectorAll(".".concat(this.props.prefix, "cascader-menu-wrapper")));

      if (menus.length === 0) {
        return;
      }

      var menusWidth = Math.ceil(menus.reduce(function (ret, menu) {
        return ret + menu.offsetWidth;
      }, 0));

      if (getStyle(this.cascaderInner, 'width') !== menusWidth) {
        setStyle(this.cascaderInner, 'width', menusWidth);
      }

      if (getStyle(this.cascader, 'display') === 'inline-block') {
        var hasRightBorderClass = "".concat(this.props.prefix, "has-right-border");
        menus.forEach(function (menu) {
          return removeClass(menu, hasRightBorderClass);
        });

        if (this.cascader.clientWidth > menusWidth) {
          addClass(menus[menus.length - 1], hasRightBorderClass);
        }
      }
    }
  }, {
    key: "setCache",
    value: function setCache(data) {
      var _this3 = this;

      var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0';
      data.forEach(function (item, index) {
        var value = item.value,
            children = item.children;
        var pos = "".concat(prefix, "-").concat(index);
        var newValue = String(value);
        item.value = newValue;
        _this3._v2n[newValue] = _this3._p2n[pos] = _objectSpread({}, item, {
          pos: pos,
          _source: item
        });

        if (children && children.length) {
          _this3.setCache(children, pos);
        }
      });
    }
  }, {
    key: "updateCache",
    value: function updateCache(dataSource) {
      this._v2n = {};
      this._p2n = {};
      this.setCache(dataSource);
    }
  }, {
    key: "normalizeValue",
    value: function normalizeValue(value) {
      if (value) {
        if (Array.isArray(value)) {
          return value;
        }

        return [value];
      }

      return [];
    }
  }, {
    key: "getExpandedValue",
    value: function getExpandedValue(v) {
      var _this4 = this;

      if (!v || !this._v2n[v]) {
        return [];
      }

      var pos = this._v2n[v].pos;

      if (pos.split('-').length === 2) {
        return [];
      }

      var expandedMap = {};
      Object.keys(this._p2n).forEach(function (p) {
        if (_this4.isDescendantOrSelf(p, pos) && p !== pos) {
          expandedMap[_this4._p2n[p].value] = p;
        }
      });
      return Object.keys(expandedMap).sort(function (prev, next) {
        return expandedMap[prev].split('-').length - expandedMap[next].split('-').length;
      });
    }
    /* eslint-disable max-statements */

  }, {
    key: "completeValue",
    value: function completeValue(dataSource, value) {
      var _this5 = this;

      var filterValue = value.filter(function (v) {
        return typeof _this5._v2n[v] !== 'undefined';
      });
      var flatValue = this.flatValue(filterValue);

      var childChecked = function childChecked(child) {
        return flatValue.indexOf(child.value) > -1;
      };

      var removeValue = function removeValue(child) {
        return flatValue.splice(flatValue.indexOf(child.value), 1);
      };

      var addParentValue = function addParentValue(i, parent) {
        return flatValue.splice(i, 0, parent.value);
      };

      for (var i = 0; i < flatValue.length; i++) {
        var pos = this.getPos(flatValue[i]);
        var nums = pos.split('-');

        if (nums.length === 2) {
          break;
        }

        for (var j = nums.length - 2; j > 0; j--) {
          var parent = nums.slice(1, j + 1).reduce(function (ret, num) {
            return ret.children[num];
          }, {
            children: dataSource
          });
          var parentChecked = parent.children.every(childChecked);

          if (parentChecked) {
            parent.children.forEach(removeValue);
            addParentValue(i, parent);
          } else {
            break;
          }
        }
      }

      var newValue = [];

      if (flatValue.length) {
        // flatValue = flatValue.reverse(); TODO 后续检测大数据性能问题
        var ps = Object.keys(this._p2n);

        for (var _i = 0; _i < flatValue.length; _i++) {
          var _pos = this.getPos(flatValue[_i]);

          for (var _j = 0; _j < ps.length; _j++) {
            if (this.isDescendantOrSelf(_pos, ps[_j])) {
              newValue.push(this.getValue(ps[_j]));
              ps.splice(_j, 1);
              _j--;
            }
          }
        }
      }

      return newValue;
    }
    /* eslint-enable */

  }, {
    key: "flatValue",
    value: function flatValue(value) {
      var _this6 = this;

      var getDepth = function getDepth(v) {
        return _this6.getPos(v).split('-').length;
      };

      var newValue = value.slice(0).sort(function (prev, next) {
        return getDepth(prev) - getDepth(next);
      });

      for (var i = 0; i < newValue.length; i++) {
        for (var j = 0; j < newValue.length; j++) {
          if (i !== j && this.isDescendantOrSelf(this.getPos(newValue[i]), this.getPos(newValue[j]))) {
            newValue.splice(j, 1);
            j--;
          }
        }
      }

      return newValue;
    }
  }, {
    key: "getValue",
    value: function getValue(pos) {
      return this._p2n[pos] ? this._p2n[pos].value : null;
    }
  }, {
    key: "getPos",
    value: function getPos(value) {
      return this._v2n[value] ? this._v2n[value].pos : null;
    }
  }, {
    key: "getData",
    value: function getData(value) {
      var _this7 = this;

      return value.map(function (v) {
        return _this7._v2n[v];
      });
    }
  }, {
    key: "isDescendantOrSelf",
    value: function isDescendantOrSelf(currentPos, targetPos) {
      if (!currentPos || !targetPos) {
        return false;
      }

      var currentNums = currentPos.split('-');
      var targetNums = targetPos.split('-');
      return currentNums.length <= targetNums.length && currentNums.every(function (num, index) {
        return num === targetNums[index];
      });
    }
  }, {
    key: "isSiblingOrSelf",
    value: function isSiblingOrSelf(currentPos, targetPos) {
      var currentNums = currentPos.split('-').slice(0, -1);
      var targetNums = targetPos.split('-').slice(0, -1);
      return currentNums.length === targetNums.length && currentNums.every(function (num, index) {
        return num === targetNums[index];
      });
    }
  }, {
    key: "processValue",
    value: function processValue(value, v, checked) {
      var index = value.indexOf(v);

      if (checked && index === -1) {
        value.push(v);
      } else if (!checked && index > -1) {
        value.splice(index, 1);
      }
    }
  }, {
    key: "handleSelect",
    value: function handleSelect(v, canExpand) {
      var _this8 = this;

      if (!(this.props.canOnlySelectLeaf && canExpand) && this.state.value[0] !== v) {
        if (!('value' in this.props)) {
          this.setState({
            value: [v]
          });
        }

        if ('onChange' in this.props) {
          var data = this._v2n[v];
          var nums = data.pos.split('-');
          var selectedPath = nums.slice(1).reduce(function (ret, num, index) {
            var p = nums.slice(0, index + 2).join('-');
            ret.push(_this8._p2n[p]);
            return ret;
          }, []);
          this.props.onChange(v, data, {
            selectedPath: selectedPath
          });
        }
      }

      if (canExpand) {
        if (!this.props.canOnlySelectLeaf) {
          this.lastExpandedValue = this.state.expandedValue.slice(0, -1);
        }
      } else {
        this.lastExpandedValue = _toConsumableArray(this.state.expandedValue);
      }
    }
    /* eslint-disable max-statements */

  }, {
    key: "handleCheck",
    value: function handleCheck(v, checked) {
      var _this9 = this;

      var _this$props = this.props,
          checkStrictly = _this$props.checkStrictly,
          canOnlyCheckLeaf = _this$props.canOnlyCheckLeaf;

      var value = _toConsumableArray(this.state.value);

      if (checkStrictly || canOnlyCheckLeaf) {
        this.processValue(value, v, checked);
      } else {
        var pos = this.getPos(v);
        var ps = Object.keys(this._p2n);
        ps.forEach(function (p) {
          if (_this9.isDescendantOrSelf(pos, p)) {
            _this9.processValue(value, _this9.getValue(p), checked);
          }
        });
        var currentPos = pos;
        var nums = pos.split('-');

        for (var i = nums.length; i > 2; i--) {
          var parentChecked = true;

          for (var j = 0; j < ps.length; j++) {
            var p = ps[j];

            if (!this.isSiblingOrSelf(currentPos, p)) {
              continue;
            }

            var _v = this.getValue(p);

            if (value.indexOf(_v) === -1) {
              parentChecked = false;
              break;
            }
          }

          var parentPos = nums.slice(0, i - 1).join('-');
          this.processValue(value, this.getValue(parentPos), parentChecked);
          currentPos = parentPos;
        }
      }

      if (!('value' in this.props)) {
        this.setState({
          value: value
        });
      }

      if ('onChange' in this.props) {
        if (checkStrictly || canOnlyCheckLeaf) {
          var data = this.getData(value);
          this.props.onChange(value, data, {
            checked: checked,
            currentData: this._v2n[v],
            checkedData: data
          });
        } else {
          var flatValue = this.flatValue(value);
          var flatData = this.getData(flatValue);
          var checkedData = this.getData(value);
          var indeterminateValue = this.getIndeterminate(value);
          var indeterminateData = this.getData(indeterminateValue);
          this.props.onChange(flatValue, flatData, {
            checked: checked,
            currentData: this._v2n[v],
            checkedData: checkedData,
            indeterminateData: indeterminateData
          });
        }
      }

      this.lastExpandedValue = _toConsumableArray(this.state.expandedValue);
    }
  }, {
    key: "handleExpand",
    value: function handleExpand(value, level, canExpand, focusedFirstChild) {
      var _this10 = this;

      var expandedValue = this.state.expandedValue;

      if (canExpand || expandedValue.length > level) {
        if (canExpand) {
          expandedValue.splice(level, expandedValue.length - level, value);
        } else {
          expandedValue.splice(level);
        }

        var callback = function callback() {
          _this10.setExpandValue(expandedValue);

          if (focusedFirstChild) {
            var endExpandedValue = expandedValue[expandedValue.length - 1];

            _this10.setState({
              focusedValue: _this10._v2n[endExpandedValue].children[0].value
            });
          }
        };

        var loadData = this.props.loadData;

        if (canExpand && loadData) {
          var data = this._v2n[value];
          return loadData(data, data._source).then(callback);
        } else {
          return callback();
        }
      }
    }
  }, {
    key: "handleMouseLeave",
    value: function handleMouseLeave() {
      this.setExpandValue(_toConsumableArray(this.lastExpandedValue));
    }
  }, {
    key: "setExpandValue",
    value: function setExpandValue(expandedValue) {
      if (!('expandedValue' in this.props)) {
        this.setState({
          expandedValue: expandedValue
        });
      }

      if ('onExpand' in this.props) {
        this.props.onExpand(expandedValue);
      }
    }
  }, {
    key: "getFirstFocusKeyByDataSource",
    value: function getFirstFocusKeyByDataSource(dataSource) {
      if (!dataSource || dataSource.length === 0) {
        return '';
      }

      for (var i = 0; i < dataSource.length; i++) {
        if (dataSource[i] && !dataSource[i].disabled) {
          return dataSource[i].value;
        }
      }

      return '';
    }
  }, {
    key: "getFirstFocusKeyByFilteredPaths",
    value: function getFirstFocusKeyByFilteredPaths(filteredPaths) {
      if (!filteredPaths || filteredPaths.length === 0) {
        return '';
      }

      for (var i = 0; i < filteredPaths.length; i++) {
        var path = filteredPaths[i];

        if (!path.some(function (item) {
          return item.disabled;
        })) {
          var lastItem = path[path.length - 1];
          return lastItem.value;
        }
      }

      return '';
    }
  }, {
    key: "getFirstFocusKey",
    value: function getFirstFocusKey() {
      var _this$props2 = this.props,
          dataSource = _this$props2.dataSource,
          searchValue = _this$props2.searchValue,
          filteredPaths = _this$props2.filteredPaths;
      return !searchValue ? this.getFirstFocusKeyByDataSource(dataSource) : this.getFirstFocusKeyByFilteredPaths(filteredPaths);
    }
  }, {
    key: "setFocusValue",
    value: function setFocusValue() {
      this.setState({
        focusedValue: this.getFirstFocusKey()
      });
    }
  }, {
    key: "handleFocus",
    value: function handleFocus(focusedValue) {
      this.setState({
        focusedValue: focusedValue
      });
    }
  }, {
    key: "handleFold",
    value: function handleFold() {
      var expandedValue = this.state.expandedValue;

      if (expandedValue.length > 0) {
        this.setExpandValue(expandedValue.slice(0, -1));
      }

      this.setState({
        focusedValue: expandedValue[expandedValue.length - 1]
      });
    }
  }, {
    key: "getIndeterminate",
    value: function getIndeterminate(value) {
      var _this11 = this;

      var indeterminate = [];
      var positions = this.flatValue(value).map(function (value) {
        return _this11.getPos(value);
      });
      positions.forEach(function (pos) {
        var nums = pos.split('-');

        for (var i = nums.length; i > 2; i--) {
          var parentPos = nums.slice(0, i - 1).join('-');

          var parentValue = _this11.getValue(parentPos);

          if (indeterminate.indexOf(parentValue) === -1) {
            indeterminate.push(parentValue);
          }
        }
      });
      return indeterminate;
    }
  }, {
    key: "onBlur",
    value: function onBlur(e) {
      this.setState({
        focusedValue: ''
      });
      this.props.onBlur && this.props.onBlur(e);
    }
  }, {
    key: "renderMenu",
    value: function renderMenu(data, level) {
      var _this12 = this;

      var _this$props3 = this.props,
          prefix = _this$props3.prefix,
          multiple = _this$props3.multiple,
          useVirtual = _this$props3.useVirtual,
          checkStrictly = _this$props3.checkStrictly,
          expandTriggerType = _this$props3.expandTriggerType,
          loadData = _this$props3.loadData,
          canOnlyCheckLeaf = _this$props3.canOnlyCheckLeaf,
          listClassName = _this$props3.listClassName,
          listStyle = _this$props3.listStyle,
          itemRender = _this$props3.itemRender;
      var _this$state = this.state,
          value = _this$state.value,
          expandedValue = _this$state.expandedValue,
          focusedValue = _this$state.focusedValue;
      return _react["default"].createElement(_menu2["default"], {
        key: level,
        prefix: prefix,
        useVirtual: useVirtual,
        className: listClassName,
        style: listStyle,
        ref: this.saveMenuRef,
        focusedKey: focusedValue,
        onItemFocus: this.handleFocus,
        onBlur: this.onBlur
      }, data.map(function (item) {
        var disabled = !!item.disabled;
        var canExpand = !!item.children && !!item.children.length || !!loadData && !item.isLeaf;
        var expanded = expandedValue[level] === item.value;
        var props = {
          prefix: prefix,
          disabled: disabled,
          canExpand: canExpand,
          expanded: expanded,
          expandTriggerType: expandTriggerType,
          onExpand: _this12.handleExpand.bind(_this12, item.value, level, canExpand),
          onFold: _this12.handleFold
        };

        if (multiple) {
          props.checkable = !(canOnlyCheckLeaf && canExpand);
          props.checked = value.indexOf(item.value) > -1;
          props.indeterminate = checkStrictly || canOnlyCheckLeaf ? false : _this12.indeterminate.indexOf(item.value) > -1;
          props.checkboxDisabled = !!item.checkboxDisabled;
          props.onCheck = _this12.handleCheck.bind(_this12, item.value);
        } else {
          props.selected = value[0] === item.value;
          props.onSelect = _this12.handleSelect.bind(_this12, item.value, canExpand);
        }

        return _react["default"].createElement(_item["default"], _extends({
          key: item.value
        }, props), itemRender(item));
      }));
    }
  }, {
    key: "renderMenus",
    value: function renderMenus() {
      var dataSource = this.props.dataSource;
      var expandedValue = this.state.expandedValue;
      var menus = [];
      var data = dataSource;

      for (var i = 0; i <= expandedValue.length; i++) {
        if (!data) {
          break;
        }

        menus.push(this.renderMenu(data, i));
        var expandedItem = void 0;

        for (var j = 0; j < data.length; j++) {
          if (data[j].value === expandedValue[i]) {
            expandedItem = data[j];
            break;
          }
        }

        data = expandedItem ? expandedItem.children : null;
      }

      return menus;
    }
  }, {
    key: "renderFilteredItem",
    value: function renderFilteredItem(path) {
      var _this$props4 = this.props,
          prefix = _this$props4.prefix,
          resultRender = _this$props4.resultRender,
          searchValue = _this$props4.searchValue,
          multiple = _this$props4.multiple;
      var value = this.state.value;
      var lastItem = path[path.length - 1];
      var Item;
      var props = {
        key: lastItem.value,
        className: "".concat(prefix, "cascader-filtered-item"),
        disabled: path.some(function (item) {
          return item.disabled;
        }),
        children: resultRender(searchValue, path)
      };

      if (multiple) {
        Item = _menu["default"].CheckboxItem;
        var _this$props5 = this.props,
            checkStrictly = _this$props5.checkStrictly,
            canOnlyCheckLeaf = _this$props5.canOnlyCheckLeaf;
        props.checked = value.indexOf(lastItem.value) > -1;
        props.indeterminate = !checkStrictly && !canOnlyCheckLeaf && this.indeterminate.indexOf(lastItem.value) > -1;
        props.checkboxDisabled = lastItem.checkboxDisabled;
        props.onChange = this.handleCheck.bind(this, lastItem.value);
      } else {
        Item = _menu["default"].Item;
        props.selected = value[0] === lastItem.value;
        props.onSelect = this.handleSelect.bind(this, lastItem.value, false);
      }

      return _react["default"].createElement(Item, props);
    }
  }, {
    key: "renderFilteredList",
    value: function renderFilteredList() {
      var _this13 = this;

      var _this$props6 = this.props,
          prefix = _this$props6.prefix,
          filteredListStyle = _this$props6.filteredListStyle,
          filteredPaths = _this$props6.filteredPaths;
      var focusedValue = this.state.focusedValue;
      return _react["default"].createElement(_menu["default"], {
        focusedKey: focusedValue,
        onItemFocus: this.handleFocus,
        className: "".concat(prefix, "cascader-filtered-list"),
        style: filteredListStyle
      }, filteredPaths.map(function (path) {
        return _this13.renderFilteredItem(path);
      }));
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props7 = this.props,
          prefix = _this$props7.prefix,
          rtl = _this$props7.rtl,
          className = _this$props7.className,
          expandTriggerType = _this$props7.expandTriggerType,
          multiple = _this$props7.multiple,
          dataSource = _this$props7.dataSource,
          checkStrictly = _this$props7.checkStrictly,
          canOnlyCheckLeaf = _this$props7.canOnlyCheckLeaf,
          searchValue = _this$props7.searchValue;
      var others = pickOthers(Object.keys(Cascader.propTypes), this.props);
      var value = this.state.value;

      if (rtl) {
        others.dir = 'rtl';
      }

      var props = _objectSpread({
        className: (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(prefix, "cascader"), true), _defineProperty(_cx, "multiple", multiple), _defineProperty(_cx, className, !!className), _cx)),
        ref: 'cascader'
      }, others);

      if (expandTriggerType === 'hover') {
        props.onMouseLeave = this.handleMouseLeave;
      }

      if (multiple && !checkStrictly && !canOnlyCheckLeaf) {
        this.indeterminate = this.getIndeterminate(value);
      }

      return _react["default"].createElement("div", _extends({}, props, {
        ref: this.getCascaderNode
      }), !searchValue ? _react["default"].createElement("div", {
        className: "".concat(prefix, "cascader-inner")
      }, dataSource && dataSource.length ? this.renderMenus() : null) : this.renderFilteredList());
    }
  }]);

  return Cascader;
}(_react.Component);

exports["default"] = Cascader;

_defineProperty(Cascader, "propTypes", {
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  pure: _propTypes["default"].bool,
  className: _propTypes["default"].string,

  /**
   * 数据源，结构可参考下方说明
   */
  dataSource: _propTypes["default"].arrayOf(_propTypes["default"].object),

  /**
   * （非受控）默认值
   */
  defaultValue: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].arrayOf(_propTypes["default"].string)]),

  /**
   * （受控）当前值
   */
  value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].arrayOf(_propTypes["default"].string)]),

  /**
   * 选中值改变时触发的回调函数
   * @param {String|Array} value 选中的值，单选时返回单个值，多选时返回数组
   * @param {Object|Array} data 选中的数据，包括 value 和 label，单选时返回单个值，多选时返回数组，父子节点选中关联时，同时选中，只返回父节点
   * @param {Object} extra 额外参数
   * @param {Array} extra.selectedPath 单选时选中的数据的路径
   * @param {Boolean} extra.checked 多选时当前的操作是选中还是取消选中
   * @param {Object} extra.currentData 多选时当前操作的数据
   * @param {Array} extra.checkedData 多选时所有被选中的数据
   * @param {Array} extra.indeterminateData 多选时半选的数据
   */
  onChange: _propTypes["default"].func,

  /**
   * （非受控）默认展开值，如果不设置，组件内部会根据 defaultValue/value 进行自动设置
   */
  defaultExpandedValue: _propTypes["default"].arrayOf(_propTypes["default"].string),

  /**
   * （受控）当前展开值
   */
  expandedValue: _propTypes["default"].arrayOf(_propTypes["default"].string),

  /**
   * 展开触发的方式
   */
  expandTriggerType: _propTypes["default"].oneOf(['click', 'hover']),

  /**
   * 展开时触发的回调函数
   * @param {Array} expandedValue 各列展开值的数组
   */
  onExpand: _propTypes["default"].func,

  /**
   * 是否开启虚拟滚动
   */
  useVirtual: _propTypes["default"].bool,

  /**
   * 是否多选
   */
  multiple: _propTypes["default"].bool,

  /**
   * 单选时是否只能选中叶子节点
   */
  canOnlySelectLeaf: _propTypes["default"].bool,

  /**
   * 多选时是否只能选中叶子节点
   */
  canOnlyCheckLeaf: _propTypes["default"].bool,

  /**
   * 父子节点是否选中不关联
   */
  checkStrictly: _propTypes["default"].bool,

  /**
   * 每列列表样式对象
   */
  listStyle: _propTypes["default"].object,

  /**
   * 每列列表类名
   */
  listClassName: _propTypes["default"].string,

  /**
   * 每列列表项渲染函数
   * @param {Object} data 数据
   * @return {ReactNode} 列表项内容
   */
  itemRender: _propTypes["default"].func,

  /**
   * 异步加载数据函数
   * @param {Object} data 当前点击异步加载的数据
   * @param {Object} source 当前点击数据
   */
  loadData: _propTypes["default"].func,
  searchValue: _propTypes["default"].string,
  onBlur: _propTypes["default"].func,
  filteredPaths: _propTypes["default"].array,
  filteredListStyle: _propTypes["default"].object,
  resultRender: _propTypes["default"].func
});

_defineProperty(Cascader, "defaultProps", {
  prefix: 'next-',
  rtl: false,
  pure: false,
  dataSource: [],
  defaultValue: null,
  canOnlySelectLeaf: false,
  canOnlyCheckLeaf: false,
  expandTriggerType: 'click',
  multiple: false,
  useVirtual: false,
  checkStrictly: false,
  itemRender: function itemRender(item) {
    return item.label;
  }
});