/
TabContent.js
executable file
·91 lines (85 loc) · 2.2 KB
/
TabContent.js
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
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {
getTransformByIndex,
getActiveIndex,
getTransformPropValue,
getMarginStyle,
} from './utils';
export default class TabContent extends React.Component {
getTabPanes() {
const props = this.props;
const activeKey = props.activeKey;
const children = props.children;
const newChildren = [];
React.Children.forEach(children, (child) => {
if (!child) {
return;
}
const key = child.key;
const active = activeKey === key;
newChildren.push(React.cloneElement(child, {
active,
destroyInactiveTabPane: props.destroyInactiveTabPane,
rootPrefixCls: props.prefixCls,
id: props.id
}));
});
return newChildren;
}
render() {
const { props } = this;
const {
prefixCls, children, activeKey, className,
tabBarPosition, animated, animatedWithMargin,
} = props;
let { style } = props;
const classes = classnames({
[`${prefixCls}-content`]: true,
[animated ?
`${prefixCls}-content-animated` :
`${prefixCls}-content-no-animated`]: true,
}, className);
if (animated) {
const activeIndex = getActiveIndex(children, activeKey);
if (activeIndex !== -1) {
const animatedStyle = animatedWithMargin ?
getMarginStyle(activeIndex, tabBarPosition) :
getTransformPropValue(getTransformByIndex(activeIndex, tabBarPosition));
style = {
...style,
...animatedStyle,
};
} else {
style = {
...style,
display: 'none',
};
}
}
return (
<div
className={classes}
style={style}
>
{this.getTabPanes()}
</div>
);
}
}
TabContent.propTypes = {
animated: PropTypes.bool,
animatedWithMargin: PropTypes.bool,
prefixCls: PropTypes.string,
children: PropTypes.node,
activeKey: PropTypes.string,
style: PropTypes.any,
tabBarPosition: PropTypes.string,
className: PropTypes.string,
destroyInactiveTabPane: PropTypes.bool,
id: PropTypes.string
};
TabContent.defaultProps = {
animated: true,
};