@txdfe/at
Version:
一个设计体系组件库
861 lines (856 loc) • 34.8 kB
JavaScript
"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(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 = exports["default"] = /*#__PURE__*/function (_Component) {
function Cascader(props, context) {
var _this;
_classCallCheck(this, Cascader);
_this = _callSuper(this, Cascader, [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(_this, ['handleMouseLeave', 'handleFocus', 'handleFold', 'getCascaderNode', 'onBlur']);
return _this;
}
_inherits(Cascader, _Component);
return _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(_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 /*#__PURE__*/_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 /*#__PURE__*/_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 /*#__PURE__*/_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 /*#__PURE__*/_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 _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"])(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "cascader"), true), "multiple", multiple), className, !!className)),
ref: 'cascader'
}, others);
if (expandTriggerType === 'hover') {
props.onMouseLeave = this.handleMouseLeave;
}
if (multiple && !checkStrictly && !canOnlyCheckLeaf) {
this.indeterminate = this.getIndeterminate(value);
}
return /*#__PURE__*/_react["default"].createElement("div", _extends({}, props, {
ref: this.getCascaderNode
}), !searchValue ? /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "cascader-inner")
}, dataSource && dataSource.length ? this.renderMenus() : null) : this.renderFilteredList());
}
}]);
}(_react.Component);
_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;
}
});