forked from react-component/collapse
/
Collapse.jsx
147 lines (130 loc) · 3.74 KB
/
Collapse.jsx
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
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import CollapsePanel from './Panel';
import openAnimationFactory from './openAnimationFactory';
import classNames from 'classnames';
function toArray(activeKey) {
let currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
}
return currentActiveKey;
}
class Collapse extends Component {
constructor(props) {
super(props);
const { activeKey, defaultActiveKey } = this.props;
let currentActiveKey = defaultActiveKey;
if ('activeKey' in this.props) {
currentActiveKey = activeKey;
}
this.state = {
openAnimation: this.props.openAnimation || openAnimationFactory(this.props.prefixCls),
activeKey: toArray(currentActiveKey),
};
}
componentWillReceiveProps(nextProps) {
if ('activeKey' in nextProps) {
this.setState({
activeKey: toArray(nextProps.activeKey),
});
}
if ('openAnimation' in nextProps) {
this.setState({
openAnimation: nextProps.openAnimation,
});
}
}
onClickItem(key) {
let activeKey = this.state.activeKey;
if (this.props.accordion) {
activeKey = activeKey[0] === key ? [] : [key];
} else {
activeKey = [...activeKey];
const index = activeKey.indexOf(key);
const isActive = index > -1;
if (isActive) {
// remove active state
activeKey.splice(index, 1);
} else {
activeKey.push(key);
}
}
this.setActiveKey(activeKey);
}
getItems() {
const activeKey = this.state.activeKey;
const { prefixCls, accordion, destroyInactivePanel } = this.props;
const newChildren = [];
Children.forEach(this.props.children, (child, index) => {
if (!child) return;
// If there is no key provide, use the panel order as default key
const key = child.key || String(index);
const { header, headerClass, disabled } = child.props;
let isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
const props = {
key,
header,
headerClass,
isActive,
prefixCls,
destroyInactivePanel,
openAnimation: this.state.openAnimation,
accordion,
children: child.props.children,
onItemClick: disabled ? null : () => this.onClickItem(key),
};
newChildren.push(React.cloneElement(child, props));
});
return newChildren;
}
setActiveKey(activeKey) {
if (!('activeKey' in this.props)) {
this.setState({ activeKey });
}
this.props.onChange(this.props.accordion ? activeKey[0] : activeKey);
}
render() {
const { prefixCls, className, style, accordion } = this.props;
const collapseClassName = classNames({
[prefixCls]: true,
[className]: !!className,
});
return (
<div className={collapseClassName} style={style} role={accordion ? 'tablist' : null}>
{this.getItems()}
</div>
);
}
}
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,
};
Collapse.defaultProps = {
prefixCls: 'rc-collapse',
onChange() {},
accordion: false,
destroyInactivePanel: false,
};
Collapse.Panel = CollapsePanel;
export default Collapse;