Collapse.js
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
/* eslint-disable react/prop-types */
import * as React from 'react';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import toArray from "rc-util/es/Children/toArray";
import CollapsePanel from './Panel';
function getActiveKeysArray(activeKey) {
var currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
}
return currentActiveKey.map(function (key) {
return String(key);
});
}
var Collapse = /*#__PURE__*/function (_React$Component) {
_inherits(Collapse, _React$Component);
var _super = _createSuper(Collapse);
function Collapse(props) {
var _this;
_classCallCheck(this, Collapse);
_this = _super.call(this, props);
_this.onClickItem = function (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);
};
_this.getNewChild = function (child, index) {
if (!child) return null;
var activeKey = _this.state.activeKey;
var _this$props = _this.props,
prefixCls = _this$props.prefixCls,
openMotion = _this$props.openMotion,
accordion = _this$props.accordion,
rootDestroyInactivePanel = _this$props.destroyInactivePanel,
expandIcon = _this$props.expandIcon,
collapsible = _this$props.collapsible; // 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,
destroyInactivePanel = _child$props.destroyInactivePanel,
childCollapsible = _child$props.collapsible;
var isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
var mergeCollapsible = childCollapsible !== null && childCollapsible !== void 0 ? childCollapsible : collapsible;
var props = {
key: key,
panelKey: key,
header: header,
headerClass: headerClass,
isActive: isActive,
prefixCls: prefixCls,
destroyInactivePanel: destroyInactivePanel !== null && destroyInactivePanel !== void 0 ? destroyInactivePanel : rootDestroyInactivePanel,
openMotion: openMotion,
accordion: accordion,
children: child.props.children,
onItemClick: mergeCollapsible === 'disabled' ? null : _this.onClickItem,
expandIcon: expandIcon,
collapsible: mergeCollapsible
}; // https://github.com/ant-design/ant-design/issues/20479
if (typeof child.type === 'string') {
return child;
}
return React.cloneElement(child, props);
};
_this.getItems = function () {
var children = _this.props.children;
return toArray(children).map(_this.getNewChild);
};
_this.setActiveKey = function (activeKey) {
if (!('activeKey' in _this.props)) {
_this.setState({
activeKey: activeKey
});
}
_this.props.onChange(_this.props.accordion ? activeKey[0] : activeKey);
};
var activeKey = props.activeKey,
defaultActiveKey = props.defaultActiveKey;
var currentActiveKey = defaultActiveKey;
if ('activeKey' in props) {
currentActiveKey = activeKey;
}
_this.state = {
activeKey: getActiveKeysArray(currentActiveKey)
};
return _this;
}
_createClass(Collapse, [{
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
className = _this$props2.className,
style = _this$props2.style,
accordion = _this$props2.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());
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps) {
var newState = {};
if ('activeKey' in nextProps) {
newState.activeKey = getActiveKeysArray(nextProps.activeKey);
}
return newState;
}
}]);
return Collapse;
}(React.Component);
Collapse.defaultProps = {
prefixCls: 'rc-collapse',
onChange: function onChange() {},
accordion: false,
destroyInactivePanel: false
};
Collapse.Panel = CollapsePanel;
export default Collapse;