choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
237 lines (202 loc) • 7.09 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Children, cloneElement, Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import CollapsePanel from './Panel';
import openAnimationFactory from './openAnimationFactory';
import ConfigContext from '../../config-provider/ConfigContext';
function toArray(activeKey) {
var currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
}
return currentActiveKey;
}
var Collapse =
/*#__PURE__*/
function (_Component) {
_inherits(Collapse, _Component);
var _super = _createSuper(Collapse);
_createClass(Collapse, null, [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}]);
function Collapse(props) {
var _this;
_classCallCheck(this, Collapse);
_this = _super.call(this, props);
var _this$props = _this.props,
activeKey = _this$props.activeKey,
defaultActiveKey = _this$props.defaultActiveKey;
var currentActiveKey = defaultActiveKey;
if ('activeKey' in _this.props) {
currentActiveKey = activeKey;
}
_this.state = {
openAnimation: _this.props.openAnimation || openAnimationFactory(_this.props.prefixCls),
activeKey: toArray(currentActiveKey)
};
return _this;
}
_createClass(Collapse, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('activeKey' in nextProps) {
this.setState({
activeKey: toArray(nextProps.activeKey)
});
}
if ('openAnimation' in nextProps) {
this.setState({
openAnimation: nextProps.openAnimation
});
}
}
}, {
key: "onClickItem",
value: function onClickItem(key) {
var activeKey = this.state.activeKey;
if (this.props.accordion) {
activeKey = activeKey[0] === key ? [] : [key];
} else {
activeKey = _toConsumableArray(activeKey);
var index = activeKey.indexOf(key);
var isActive = index > -1;
if (isActive) {
// remove active state
activeKey.splice(index, 1);
} else {
activeKey.push(key);
}
}
this.setActiveKey(activeKey);
}
}, {
key: "getItems",
value: function getItems() {
var _this2 = this;
var activeKey = this.state.activeKey;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
accordion = _this$props2.accordion,
destroyInactivePanel = _this$props2.destroyInactivePanel,
expandIcon = _this$props2.expandIcon,
expandIconPosition = _this$props2.expandIconPosition;
var newChildren = [];
var getConfig = this.context.getConfig;
Children.forEach(this.props.children, function (child, index) {
if (!child) return; // If there is no key provide, use the panel order as default key
var key = child.key || String(index);
var _child$props = child.props,
header = _child$props.header,
headerClass = _child$props.headerClass,
disabled = _child$props.disabled;
var isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
var trigger = _this2.props.trigger || getConfig('collapseTrigger');
var props = {
key: key,
header: header,
headerClass: headerClass,
isActive: isActive,
prefixCls: prefixCls,
destroyInactivePanel: destroyInactivePanel,
openAnimation: _this2.state.openAnimation,
children: child.props.children,
onItemClick: disabled ? null : function () {
return _this2.onClickItem(key);
},
expandIcon: expandIcon,
expandIconPosition: expandIconPosition,
trigger: trigger
};
newChildren.push(cloneElement(child, props));
});
return newChildren;
}
}, {
key: "setActiveKey",
value: function setActiveKey(activeKey) {
if (!('activeKey' in this.props)) {
this.setState({
activeKey: activeKey
});
}
this.props.onChange(this.props.accordion ? activeKey[0] : activeKey);
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
className = _this$props3.className,
style = _this$props3.style,
accordion = _this$props3.accordion;
var collapseClassName = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, className, !!className), _classNames));
return React.createElement("div", {
className: collapseClassName,
style: style,
role: accordion ? 'tablist' : null
}, this.getItems());
}
}]);
return Collapse;
}(Component);
_defineProperty(Collapse, "propTypes", {
children: PropTypes.any,
prefixCls: PropTypes.string,
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
openAnimation: PropTypes.object,
onChange: PropTypes.func,
accordion: PropTypes.bool,
className: PropTypes.string,
style: PropTypes.object,
destroyInactivePanel: PropTypes.bool
});
_defineProperty(Collapse, "defaultProps", {
prefixCls: 'rc-collapse',
onChange: function onChange() {},
accordion: false,
destroyInactivePanel: false
});
_defineProperty(Collapse, "Panel", CollapsePanel);
export { Collapse as default };
//# sourceMappingURL=Collapse.js.map