-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathCollapse.tsx
More file actions
164 lines (143 loc) 路 4.35 KB
/
Copy pathCollapse.tsx
File metadata and controls
164 lines (143 loc) 路 4.35 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
/* eslint-disable react/prop-types */
import * as React from 'react';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import toArray from 'rc-util/lib/Children/toArray';
import CollapsePanel from './Panel';
import type { CollapseProps, CollapsibleType } from './interface';
function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
let currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
const activeKeyType = typeof currentActiveKey;
currentActiveKey =
activeKeyType === 'number' || activeKeyType === 'string' ? [currentActiveKey] : [];
}
return currentActiveKey.map((key) => String(key));
}
export interface CollapseState {
activeKey: React.Key[];
}
class Collapse extends React.Component<CollapseProps, CollapseState> {
static defaultProps = {
prefixCls: 'rc-collapse',
onChange() {},
accordion: false,
destroyInactivePanel: false,
};
static Panel = CollapsePanel;
constructor(props: CollapseProps) {
super(props);
const { activeKey, defaultActiveKey } = props;
let currentActiveKey = defaultActiveKey;
if ('activeKey' in props) {
currentActiveKey = activeKey;
}
this.state = {
activeKey: getActiveKeysArray(currentActiveKey),
};
}
shouldComponentUpdate(nextProps: CollapseProps, nextState: CollapseState) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
}
onClickItem = (key: React.Key) => {
let { activeKey } = this.state;
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);
};
static getDerivedStateFromProps(nextProps: CollapseProps) {
const newState: Partial<CollapseState> = {};
if ('activeKey' in nextProps) {
newState.activeKey = getActiveKeysArray(nextProps.activeKey);
}
return newState;
}
getNewChild = (child: React.ReactElement, index: number) => {
if (!child) return null;
const { activeKey } = this.state;
const {
prefixCls,
openMotion,
accordion,
destroyInactivePanel: rootDestroyInactivePanel,
expandIcon,
headerRender,
collapsible,
} = this.props;
// If there is no key provide, use the panel order as default key
const key = child.key || String(index);
const {
header,
headerClass,
destroyInactivePanel,
collapsible: childCollapsible,
} = child.props;
let isActive = false;
if (accordion) {
isActive = activeKey[0] === key;
} else {
isActive = activeKey.indexOf(key) > -1;
}
const mergeCollapsible: CollapsibleType = childCollapsible ?? collapsible;
const props = {
key,
panelKey: key,
header,
headerClass,
isActive,
prefixCls,
destroyInactivePanel: destroyInactivePanel ?? rootDestroyInactivePanel,
openMotion,
accordion,
children: child.props.children,
onItemClick: mergeCollapsible === 'disabled' ? null : this.onClickItem,
expandIcon,
headerRender,
collapsible: mergeCollapsible,
};
// https://github.com/ant-design/ant-design/issues/20479
if (typeof child.type === 'string') {
return child;
}
Object.keys(props).forEach((propName: keyof typeof props) => {
if (typeof props[propName] === 'undefined') {
delete props[propName];
}
});
return React.cloneElement(child, props);
};
getItems = () => {
const { children } = this.props;
return toArray(children).map(this.getNewChild);
};
setActiveKey = (activeKey: React.Key[]) => {
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>
);
}
}
export default Collapse;