-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathPanel.tsx
163 lines (144 loc) · 4.13 KB
/
Panel.tsx
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
/* eslint-disable react/prop-types */
import * as React from 'react';
import classNames from 'classnames';
import CSSMotion from 'rc-motion';
import shallowEqual from 'shallowequal';
import PanelContent from './PanelContent';
import type { CollapsePanelProps } from './interface';
class CollapsePanel extends React.Component<CollapsePanelProps, any> {
static defaultProps = {
showArrow: true,
isActive: false,
onItemClick() {},
headerClass: '',
forceRender: false,
};
shouldComponentUpdate(nextProps: CollapsePanelProps) {
return !shallowEqual(this.props, nextProps);
}
onItemClick = () => {
const { onItemClick, panelKey } = this.props;
if (typeof onItemClick === 'function') {
onItemClick(panelKey);
}
};
handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' || e.keyCode === 13 || e.which === 13) {
this.onItemClick();
}
};
renderIcon = () => {
const { showArrow, expandIcon, prefixCls, collapsible } = this.props;
if (!showArrow) {
return null;
}
const iconNode =
typeof expandIcon === 'function' ? expandIcon(this.props) : <i className="arrow" />;
return (
iconNode && (
<div
className={`${prefixCls}-expand-icon`}
onClick={collapsible === 'header' ? this.onItemClick : null}
>
{iconNode}
</div>
)
);
};
renderTitle = () => {
const { header, prefixCls, collapsible } = this.props;
return (
<span
className={`${prefixCls}-header-text`}
onClick={collapsible === 'header' ? this.onItemClick : null}
>
{header}
</span>
);
};
render() {
const {
className,
id,
style,
prefixCls,
headerClass,
children,
isActive,
destroyInactivePanel,
accordion,
forceRender,
openMotion,
headerRender,
extra,
collapsible,
...rest
} = this.props;
const disabled = collapsible === 'disabled';
const collapsibleHeader = collapsible === 'header';
const itemCls = classNames(
{
[`${prefixCls}-item`]: true,
[`${prefixCls}-item-active`]: isActive,
[`${prefixCls}-item-disabled`]: disabled,
},
className,
);
const headerCls = classNames(`${prefixCls}-header`, {
[headerClass]: headerClass,
[`${prefixCls}-header-collapsible-only`]: collapsibleHeader,
});
/** header 节点属性 */
const headerProps: React.HTMLAttributes<HTMLDivElement> = {
className: headerCls,
'aria-expanded': isActive,
'aria-disabled': disabled,
onKeyPress: this.handleKeyPress,
};
if (!collapsibleHeader) {
headerProps.onClick = this.onItemClick;
headerProps.role = accordion ? 'tab' : 'button';
headerProps.tabIndex = disabled ? -1 : 0;
}
const ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean';
return (
<div {...rest} className={itemCls} style={style} id={id}>
<div {...headerProps}>
{headerRender && typeof headerRender === 'function' ? (
headerRender(this.props)
) : (
<>
{this.renderIcon()}
{this.renderTitle()}
{ifExtraExist && <div className={`${prefixCls}-extra`}>{extra}</div>}
</>
)}
</div>
<CSSMotion
visible={isActive}
leavedClassName={`${prefixCls}-content-hidden`}
{...openMotion}
forceRender={forceRender}
removeOnLeave={destroyInactivePanel}
>
{({ className: motionClassName, style: motionStyle }, ref) => {
return (
<PanelContent
ref={ref}
prefixCls={prefixCls}
className={motionClassName}
style={motionStyle}
isActive={isActive}
forceRender={forceRender}
role={accordion ? 'tabpanel' : null}
>
{children}
</PanelContent>
);
}}
</CSSMotion>
</div>
);
}
}
export default CollapsePanel;